Class: ScrapeCreators::Client

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

Constant Summary collapse

TIKTOK =
'tiktok'
INSTAGRAM =
'instagram'
YOUTUBE =
'youtube'

Instance Attribute Summary collapse

Instance Method Summary collapse

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_keyObject (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

#configObject (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
# File 'lib/scrape_creators/client.rb', line 75

def comments(url_or_code, options = {})
  comments_page(url_or_code, options)[:comments]
end

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/scrape_creators/client.rb', line 79

def comments_page(url_or_code, options = {})
  source = (options[:source] || 'instagram').to_s
  url = normalize_url(url_or_code)
  extra = options.reject { |k, _| %w[source cursor continuationToken continuation_token].include?(k.to_s) }
  params = { url: url }.merge(extra)
  cursor = options[:cursor] || options['cursor'] || options[:continuationToken] || options['continuationToken'] || options[:continuation_token]
  if cursor && !cursor.to_s.empty?
    if source == YOUTUBE
      params[:continuationToken] = cursor
    else
      params[:cursor] = cursor
    end
  end

  res = get(comments_url_for(source: source), params)
  comments = res.is_a?(Hash) ? (res['comments'] || []) : []
  next_cursor = next_comments_cursor(res, source)
  {
    comments: comments,
    cursor: next_cursor,
    has_more: comments_has_more?(res, source, next_cursor)
  }
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



103
104
105
# File 'lib/scrape_creators/client.rb', line 103

def get(endpoint, params = {}, options = {})
  request(endpoint, method: :get, params: params, options: 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, options = {})
  source = (options[:source] || 'instagram').to_s
  url = normalize_url(url_or_code)
  post_url = post_url_for(source: source)
  params = { url: url }.merge(options.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, options = {})
  source = (options[:source] || 'instagram').to_s
  params = { handle: handle }.merge(options.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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/scrape_creators/client.rb', line 107

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