pinterest-dl 2.0.0 → 2.0.1

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: 6c0b3841a1ae58f40462a3b5e3545a7f72c8e3e99457e34a046a78a06598ad5c
4
- data.tar.gz: 220569a87bab3bfe8776169a97cf373278402a8ad49190deb761e749010f5798
3
+ metadata.gz: b4d94f0a5bbac63a983308d541b5e50a6a13c1d5bee422c356e56395c686ebcd
4
+ data.tar.gz: 148473c309ec313a3e0d7ddb0864f0388d22a2bf9edee39e819e54ccaab10745
5
5
  SHA512:
6
- metadata.gz: aa67c66ab2f7bbfb5c58de173ec73b1b077ea29bdb316623f1a2d9abf4a6f3a2c26388127a167a4955fa0f5ac63dbb7705963cad9d4bbf018aaa70ec24e53cad
7
- data.tar.gz: 0ea15400b28788771709772a9d5058ca392231a2c27270b310ee634847b075e209531182bfb5eec1873c4d4b058d6b96567a8bf0503a7e533c1b6df4d6dc6972
6
+ metadata.gz: 4e26a226009b81da00d41f46f9ccfcdca2f058aa7ff93210b488e1f2082c27f927103f5a0329dcea921ea34a62c49c3ad70c77d411d4a1b569b4b97fbba8c4cf
7
+ data.tar.gz: 66b3ddf6d10555f9e018198a3de376578b22fc504dfe386d8a49f06bdcc213519e0b273c5f123d34bcb33eb3217da51af7609712a2f1a0a3b67004be6858eb9b
@@ -5,20 +5,15 @@ require 'net/http'
5
5
  require 'uri'
6
6
 
7
7
  module PinterestDL
8
- # Handles Pinterest's internal search resource, including pagination
9
- # via the `bookmarks` cursor Pinterest returns with each page.
10
8
  class Search
11
- RESOURCE_URL = 'https://www.pinterest.com/resource/BaseSearchResource/get/'
9
+ B_URL = 'https://www.pinterest.com/resource/BaseSearchResource/get/'
12
10
 
13
11
  def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
14
12
  @client = client
15
13
  @config = config
16
14
  end
17
15
 
18
- # @param query [String]
19
- # @param limit [Integer] max number of pins to return in total
20
- # @param pages [Integer] max number of pages to fetch (pagination safety valve)
21
- # @return [Array<Hash>] pin metadata hashes
16
+
22
17
  def call(query, limit: 25, pages: 5)
23
18
  results = []
24
19
  bookmarks = nil
@@ -55,17 +50,17 @@ module PinterestDL
55
50
  options[:bookmarks] = bookmarks if bookmarks && !bookmarks.empty?
56
51
  data = { options: options, context: {} }.to_json
57
52
 
58
- res_uri = URI(RESOURCE_URL)
53
+ res_uri = URI(B_URL)
59
54
  res_uri.query = URI.encode_www_form('source_url' => source_path, 'data' => data)
60
55
 
61
56
  body = @client.get(res_uri.to_s, use_cache: false, headers: {
62
- 'Accept' => 'application/json, text/javascript, */*, q=0.01',
63
- 'X-Requested-With' => 'XMLHttpRequest',
64
- 'X-Pinterest-PWS-Handler' => 'www/search/[scope].js',
65
- 'X-CSRFToken' => csrf,
66
- 'Referer' => home_uri.to_s,
67
- 'Cookie' => cookie_header
68
- })
57
+ 'Accept' => 'application/json, text/javascript, */*, q=0.01',
58
+ 'X-Requested-With' => 'XMLHttpRequest',
59
+ 'X-Pinterest-PWS-Handler' => 'www/search/[scope].js',
60
+ 'X-CSRFToken' => csrf,
61
+ 'Referer' => home_uri.to_s,
62
+ 'Cookie' => cookie_header
63
+ })
69
64
 
70
65
  json = safe_parse(body)
71
66
  raise PinterestDL::NotFoundError, "No search results for \"#{query}\"" unless json
@@ -73,7 +68,7 @@ module PinterestDL
73
68
  data_node = json.dig('resource_response', 'data') || {}
74
69
  raw_results = data_node['results'] || []
75
70
  next_bookmarks = json.dig('resource_response', 'bookmark') ||
76
- json.dig('resource', 'options', 'bookmarks')
71
+ json.dig('resource', 'options', 'bookmarks')
77
72
 
78
73
  { pins: raw_results.filter_map { |r| build_pin(r) }, bookmarks: Array(next_bookmarks) }
79
74
  end
@@ -6,7 +6,7 @@ require 'uri'
6
6
 
7
7
  module PinterestDL
8
8
  class Search
9
- RESOURCE_URL = 'https://www.pinterest.com/resource/BaseSearchResource/get/'
9
+ S_URL = 'https://www.pinterest.com/resource/BaseSearchResource/get/'
10
10
 
11
11
  def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
12
12
  @client = client
@@ -50,6 +50,8 @@ module PinterestDL
50
50
  parse_search_response(json)
51
51
  end
52
52
 
53
+ # Pinterest's search resource requires a session cookie + CSRF token
54
+ # obtained by first loading the human-facing search page.
53
55
  def establish_session(query)
54
56
  source_path = "/search/pins/?q=#{URI.encode_www_form_component(query)}"
55
57
  home_uri = URI("https://www.pinterest.com#{source_path}")
@@ -69,7 +71,7 @@ module PinterestDL
69
71
  options[:bookmarks] = bookmarks if bookmarks && !bookmarks.empty?
70
72
  data = { options: options, context: {} }.to_json
71
73
 
72
- res_uri = URI(RESOURCE_URL)
74
+ res_uri = URI(S_URL)
73
75
  res_uri.query = URI.encode_www_form('source_url' => source_path, 'data' => data)
74
76
  res_uri.to_s
75
77
  end
@@ -91,7 +93,7 @@ module PinterestDL
91
93
  data_node = json.dig('resource_response', 'data') || {}
92
94
  raw_results = data_node['results'] || []
93
95
  next_bookmarks = json.dig('resource_response', 'bookmark') ||
94
- json.dig('resource', 'options', 'bookmarks')
96
+ json.dig('resource', 'options', 'bookmarks')
95
97
 
96
98
  { pins: raw_results.filter_map { |r| build_pin(r) }, bookmarks: Array(next_bookmarks) }
97
99
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Monji024
4
4
  module PinterestDL
5
- VERSION = '2.0.0'
5
+ VERSION = '2.0.1'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pinterest-dl
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Monji