scrape_creators 0.4.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: 41069945ebf877cbd439c8bfbb318ba0114d4e9df6275aabacef16867417a7b4
4
- data.tar.gz: 24d8743a67a698cfc97f974015a8ff30ab33254b8dd155e96f1d4b0283e9d9c0
3
+ metadata.gz: c214fa7c8b7b56a2483d3432420003745f73747e8391100312168466f9f03c7b
4
+ data.tar.gz: 0ec20deecab206e3ba0f54707abbfee7e624485d5f7954311bdb34552b207f40
5
5
  SHA512:
6
- metadata.gz: fdab6ab8877d97093743a3e52d3daa17bf33ac1c4b3a461b6ea85957e83bff213b9e27adbaa878b36e9af15858f014334aceda898beae6cb9d498d15e56e2d1f
7
- data.tar.gz: ddeaf8dc9f781648485b348a3ccaeb660a2dfe47a611e37a6c0cd9283fae66179a91521c11be731faf698b8cf22683cf5a8b8b3862b198d1c148f20641011bda
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
@@ -73,12 +73,31 @@ module ScrapeCreators
73
73
  end
74
74
 
75
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 = {})
76
80
  source = (options[:source] || 'instagram').to_s
77
81
  url = normalize_url(url_or_code)
78
- params = { url: url }.merge(options.reject { |k, _| k.to_s == 'source' })
79
- comments_url = comments_url_for(source: source)
80
- res = get(comments_url, params)
81
- 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
+ }
82
101
  end
83
102
 
84
103
  def get(endpoint, params = {}, options = {})
@@ -121,6 +140,29 @@ module ScrapeCreators
121
140
 
122
141
  private
123
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
+
124
166
  def normalize_url(url_or_code)
125
167
  str = url_or_code.to_s.strip
126
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.4.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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - swlkr