Class: ScrapeCreators::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/scrape_creators/client.rb

Constant Summary collapse

TIKTOK =
'tiktok'
INSTAGRAM =
'instagram'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, api_key: nil) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/scrape_creators/client.rb', line 16

def initialize(config = nil, api_key: nil)
  @config = config || ScrapeCreators.configuration
  @api_key = api_key || @config.api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



14
15
16
# File 'lib/scrape_creators/client.rb', line 14

def api_key
  @api_key
end

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/scrape_creators/client.rb', line 14

def config
  @config
end

Instance Method Details

#comments(url_or_code, options = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/scrape_creators/client.rb', line 70

def comments(url_or_code, options = {})
  url = normalize_url(url_or_code)
  params = { url: url }.merge(options)
  source = options.dig(:source) || 'instagram'
  comments_url = comments_url_for(source:)
  res = get(comments_url, params)
  res.is_a?(Hash) ? (res["comments"] || []) : []
end

#comments_url_for(source:) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/scrape_creators/client.rb', line 37

def comments_url_for(source:)
  case source
  when TIKTOK then '/v1/tiktok/video/comments'
  when INSTAGRAM then '/v2/instagram/post/comments'
  else '/404'
  end
end

#get(endpoint, params = {}, options = {}) ⇒ Object



79
80
81
# File 'lib/scrape_creators/client.rb', line 79

def get(endpoint, params = {}, options = {})
  request(endpoint, method: :get, params: params, options: options)
end

#post(url_or_code, options = {}) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/scrape_creators/client.rb', line 62

def post(url_or_code, options = {})
  url = normalize_url(url_or_code)
  source = options.dig(:source) || 'instagram'
  post_url = post_url_for(source:)
  params = { url: url }.merge(options)
  get(post_url, params)
end

#post_url_for(source:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/scrape_creators/client.rb', line 29

def post_url_for(source:)
  case source
  when TIKTOK then '/v2/tiktok/video'
  when INSTAGRAM then '/v1/instagram/post'
  else '/404'
  end
end

#posts(handle, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/scrape_creators/client.rb', line 53

def posts(handle, options = {})
  params = { handle: }.merge(options)
  source = options.dig(:source) || 'instagram'
  posts_url = posts_url_for(source:)
  res = get(posts_url, params)
  key = posts_key_for(source:)
  res.is_a?(Hash) ? (res[key] || []) : []
end

#posts_key_for(source:) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/scrape_creators/client.rb', line 45

def posts_key_for(source:)
  case source
  when TIKTOK then 'aweme_list'
  when INSTAGRAM then 'items'
  else 'items'
  end
end

#posts_url_for(source:) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/scrape_creators/client.rb', line 21

def posts_url_for(source:)
  case source
  when TIKTOK then '/v3/tiktok/profile/videos'
  when INSTAGRAM then '/v2/instagram/user/posts'
  else '/404'
  end
end

#request(endpoint, method: :get, params: {}, options: {}) ⇒ Object



83
84
85
86
87
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
# File 'lib/scrape_creators/client.rb', line 83

def request(endpoint, method: :get, params: {}, options: {})
  current_key = options[: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 = options[: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 = options[:open_timeout] || config.open_timeout
  http.read_timeout = options[:read_timeout] || config.read_timeout

  res = http.request(req)
  parse_response(res)
end