Class: ScrapeCreators::Client
- Inherits:
-
Object
- Object
- ScrapeCreators::Client
- Defined in:
- lib/scrape_creators/client.rb
Constant Summary collapse
- TIKTOK =
'tiktok'- INSTAGRAM =
'instagram'- YOUTUBE =
'youtube'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #comments(url_or_code, options = {}) ⇒ Object
- #comments_url_for(source:) ⇒ Object
- #get(endpoint, params = {}, options = {}) ⇒ Object
-
#initialize(config = nil, api_key: nil) ⇒ Client
constructor
A new instance of Client.
- #post(url_or_code, options = {}) ⇒ Object
- #post_url_for(source:) ⇒ Object
- #posts(handle, options = {}) ⇒ Object
- #posts_key_for(source:) ⇒ Object
- #posts_url_for(source:) ⇒ Object
- #request(endpoint, method: :get, params: {}, options: {}) ⇒ Object
Constructor Details
#initialize(config = nil, api_key: nil) ⇒ Client
Returns a new instance of Client.
17 18 19 20 |
# File 'lib/scrape_creators/client.rb', line 17 def initialize(config = nil, api_key: nil) @config = config || ScrapeCreators.configuration @api_key = api_key || @config.api_key end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/scrape_creators/client.rb', line 15 def api_key @api_key end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/scrape_creators/client.rb', line 15 def config @config end |
Instance Method Details
#comments(url_or_code, options = {}) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/scrape_creators/client.rb', line 75 def comments(url_or_code, = {}) source = ([:source] || 'instagram').to_s url = normalize_url(url_or_code) params = { url: url }.merge(.reject { |k, _| k.to_s == 'source' }) comments_url = comments_url_for(source: source) res = get(comments_url, params) res.is_a?(Hash) ? (res["comments"] || []) : [] end |
#comments_url_for(source:) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/scrape_creators/client.rb', line 40 def comments_url_for(source:) case source.to_s when TIKTOK then '/v1/tiktok/video/comments' when INSTAGRAM then '/v2/instagram/post/comments' when YOUTUBE then '/v1/youtube/video/comments' else '/404' end end |
#get(endpoint, params = {}, options = {}) ⇒ Object
84 85 86 |
# File 'lib/scrape_creators/client.rb', line 84 def get(endpoint, params = {}, = {}) request(endpoint, method: :get, params: params, options: ) end |
#post(url_or_code, options = {}) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/scrape_creators/client.rb', line 67 def post(url_or_code, = {}) source = ([:source] || 'instagram').to_s url = normalize_url(url_or_code) post_url = post_url_for(source: source) params = { url: url }.merge(.reject { |k, _| k.to_s == 'source' }) get(post_url, params) end |
#post_url_for(source:) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/scrape_creators/client.rb', line 31 def post_url_for(source:) case source.to_s when TIKTOK then '/v2/tiktok/video' when INSTAGRAM then '/v1/instagram/post' when YOUTUBE then '/v1/youtube/video' else '/404' end end |
#posts(handle, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/scrape_creators/client.rb', line 58 def posts(handle, = {}) source = ([:source] || 'instagram').to_s params = { handle: handle }.merge(.reject { |k, _| k.to_s == 'source' }) posts_url = posts_url_for(source: source) res = get(posts_url, params) key = posts_key_for(source: source) res.is_a?(Hash) ? (res[key] || []) : [] end |
#posts_key_for(source:) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/scrape_creators/client.rb', line 49 def posts_key_for(source:) case source.to_s when TIKTOK then 'aweme_list' when INSTAGRAM then 'items' when YOUTUBE then 'shorts' else 'items' end end |
#posts_url_for(source:) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/scrape_creators/client.rb', line 22 def posts_url_for(source:) case source.to_s when TIKTOK then '/v3/tiktok/profile/videos' when INSTAGRAM then '/v2/instagram/user/posts' when YOUTUBE then '/v1/youtube/channel/shorts' else '/404' end end |
#request(endpoint, method: :get, params: {}, options: {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/scrape_creators/client.rb', line 88 def request(endpoint, method: :get, params: {}, options: {}) current_key = [:api_key] || api_key if current_key.nil? || current_key.to_s.strip.empty? raise ConfigurationError, "Scrape Creators API key is missing. Set ENV['SCRAPE_CREATORS_API_KEY'] or configure via ScrapeCreators.configure { |c| c.api_key = '...' }" end clean_endpoint = endpoint.to_s.start_with?('/') ? endpoint.to_s : "/#{endpoint}" base_url = [:api_base_url] || config.api_base_url url_str = "#{base_url}#{clean_endpoint}" uri = URI.parse(url_str) if method == :get uri.query = URI.encode_www_form(params) if params && !params.empty? req = Net::HTTP::Get.new(uri.request_uri) else req = Net::HTTP::Post.new(uri.request_uri) req['Content-Type'] = 'application/json' req.body = JSON.generate(params) if params && !params.empty? end req['x-api-key'] = current_key req['Accept'] = 'application/json' req['Accept-Encoding'] = 'gzip, deflate' http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') http.open_timeout = [:open_timeout] || config.open_timeout http.read_timeout = [:read_timeout] || config.read_timeout res = http.request(req) parse_response(res) end |