scrape_creators 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1b0ec47242c7f1e3f0d551233d27f9e11be3874cc3b78a78181eaa40a21b84f
4
- data.tar.gz: 5a8827a29aa9b0ae06a890bc979d9e52e8a9f94ba2b717d38ebb305b7459fcad
3
+ metadata.gz: c214fa7c8b7b56a2483d3432420003745f73747e8391100312168466f9f03c7b
4
+ data.tar.gz: 0ec20deecab206e3ba0f54707abbfee7e624485d5f7954311bdb34552b207f40
5
5
  SHA512:
6
- metadata.gz: '08f33db279a55d7b432d7cc29c0b7b1ebec5b8d5938bab8c00e13484871f4fcdfe7bba2989146f0253e4c3cfe1dc86dc8110ba732cfd05ac6a0e2b08184c9108'
7
- data.tar.gz: 92519f692c9d01b014c933f3a333a5494ebc86f0cc3ad0c98247b6799cb2d457220304cd11f71c7f8df3c5f277ee1ea66aaf75f05a025a27e080640ada04378e
6
+ metadata.gz: f15f5722980fca1ccb48a32e1d62927324ae944c81132cc3210f2a35d12209033ef244804c988a9f4b3924c28f78c9ddca63bf66e29dd0e24e9477195ad95aed
7
+ data.tar.gz: 20aadfbd40d3362a8d6fbbe52223fb1bb4baafdaec99406e9a9bbe477e8ab1fd40b29bf7279fdfc813a88c3126237a5bd598bf7ccf2db5c0adb133e7c79d4c66
data/README.md CHANGED
@@ -50,6 +50,16 @@ info = scraper.post("https://www.instagram.com/p/DKSMEpKRd6h/", download_media:
50
50
 
51
51
  ```ruby
52
52
  comments = scraper.comments("https://www.instagram.com/p/DKSMEpKRd6h/")
53
+
54
+ page = scraper.comments_page("https://www.instagram.com/p/DKSMEpKRd6h/")
55
+ page[:comments]
56
+ page[:cursor]
57
+ page[:has_more]
58
+
59
+ next_page = scraper.comments_page(
60
+ "https://www.instagram.com/p/DKSMEpKRd6h/",
61
+ cursor: page[:cursor]
62
+ )
53
63
  ```
54
64
 
55
65
  ## License
@@ -10,6 +10,7 @@ module ScrapeCreators
10
10
  class Client
11
11
  TIKTOK = 'tiktok'
12
12
  INSTAGRAM = 'instagram'
13
+ YOUTUBE = 'youtube'
13
14
 
14
15
  attr_reader :config, :api_key
15
16
 
@@ -19,61 +20,84 @@ module ScrapeCreators
19
20
  end
20
21
 
21
22
  def posts_url_for(source:)
22
- case source
23
+ case source.to_s
23
24
  when TIKTOK then '/v3/tiktok/profile/videos'
24
25
  when INSTAGRAM then '/v2/instagram/user/posts'
26
+ when YOUTUBE then '/v1/youtube/channel/shorts'
25
27
  else '/404'
26
28
  end
27
29
  end
28
30
 
29
31
  def post_url_for(source:)
30
- case source
32
+ case source.to_s
31
33
  when TIKTOK then '/v2/tiktok/video'
32
34
  when INSTAGRAM then '/v1/instagram/post'
35
+ when YOUTUBE then '/v1/youtube/video'
33
36
  else '/404'
34
37
  end
35
38
  end
36
39
 
37
40
  def comments_url_for(source:)
38
- case source
41
+ case source.to_s
39
42
  when TIKTOK then '/v1/tiktok/video/comments'
40
43
  when INSTAGRAM then '/v2/instagram/post/comments'
44
+ when YOUTUBE then '/v1/youtube/video/comments'
41
45
  else '/404'
42
46
  end
43
47
  end
44
48
 
45
49
  def posts_key_for(source:)
46
- case source
50
+ case source.to_s
47
51
  when TIKTOK then 'aweme_list'
48
52
  when INSTAGRAM then 'items'
53
+ when YOUTUBE then 'shorts'
49
54
  else 'items'
50
55
  end
51
56
  end
52
57
 
53
58
  def posts(handle, options = {})
54
- params = { handle: }.merge(options)
55
- source = options.dig(:source) || 'instagram'
56
- posts_url = posts_url_for(source:)
59
+ source = (options[:source] || 'instagram').to_s
60
+ params = { handle: handle }.merge(options.reject { |k, _| k.to_s == 'source' })
61
+ posts_url = posts_url_for(source: source)
57
62
  res = get(posts_url, params)
58
- key = posts_key_for(source:)
63
+ key = posts_key_for(source: source)
59
64
  res.is_a?(Hash) ? (res[key] || []) : []
60
65
  end
61
66
 
62
67
  def post(url_or_code, options = {})
68
+ source = (options[:source] || 'instagram').to_s
63
69
  url = normalize_url(url_or_code)
64
- source = options.dig(:source) || 'instagram'
65
- post_url = post_url_for(source:)
66
- params = { url: url }.merge(options)
70
+ post_url = post_url_for(source: source)
71
+ params = { url: url }.merge(options.reject { |k, _| k.to_s == 'source' })
67
72
  get(post_url, params)
68
73
  end
69
74
 
70
75
  def comments(url_or_code, options = {})
76
+ comments_page(url_or_code, options)[:comments]
77
+ end
78
+
79
+ def comments_page(url_or_code, options = {})
80
+ source = (options[:source] || 'instagram').to_s
71
81
  url = normalize_url(url_or_code)
72
- params = { url: url }.merge(options)
73
- source = options.dig(:source) || 'instagram'
74
- comments_url = comments_url_for(source:)
75
- res = get(comments_url, params)
76
- res.is_a?(Hash) ? (res["comments"] || []) : []
82
+ extra = options.reject { |k, _| %w[source cursor continuationToken continuation_token].include?(k.to_s) }
83
+ params = { url: url }.merge(extra)
84
+ cursor = options[:cursor] || options['cursor'] || options[:continuationToken] || options['continuationToken'] || options[:continuation_token]
85
+ if cursor && !cursor.to_s.empty?
86
+ if source == YOUTUBE
87
+ params[:continuationToken] = cursor
88
+ else
89
+ params[:cursor] = cursor
90
+ end
91
+ end
92
+
93
+ res = get(comments_url_for(source: source), params)
94
+ comments = res.is_a?(Hash) ? (res['comments'] || []) : []
95
+ next_cursor = next_comments_cursor(res, source)
96
+ {
97
+ comments: comments,
98
+ cursor: next_cursor,
99
+ has_more: comments_has_more?(res, source, next_cursor)
100
+ }
77
101
  end
78
102
 
79
103
  def get(endpoint, params = {}, options = {})
@@ -116,6 +140,29 @@ module ScrapeCreators
116
140
 
117
141
  private
118
142
 
143
+ def next_comments_cursor(res, source)
144
+ return nil unless res.is_a?(Hash)
145
+
146
+ cursor = if source == YOUTUBE
147
+ res['continuationToken']
148
+ else
149
+ res['cursor']
150
+ end
151
+ return nil if cursor.nil? || cursor.to_s.empty?
152
+
153
+ cursor
154
+ end
155
+
156
+ def comments_has_more?(res, source, next_cursor)
157
+ return false unless res.is_a?(Hash)
158
+
159
+ if source == TIKTOK
160
+ res['has_more'].to_i == 1
161
+ else
162
+ !next_cursor.nil?
163
+ end
164
+ end
165
+
119
166
  def normalize_url(url_or_code)
120
167
  str = url_or_code.to_s.strip
121
168
  if str.start_with?('http://', 'https://')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ScrapeCreators
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrape_creators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - swlkr