pinterest-dl 2.2.0 → 2.4.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 +4 -4
- data/CHANGELOG.md +52 -8
- data/README.md +108 -113
- data/bin/pinterest-dl +1 -125
- data/lib/pinterest-dl.rb +8 -1
- data/lib/pinterest_dl/api.rb +59 -24
- data/lib/pinterest_dl/board.rb +31 -42
- data/lib/pinterest_dl/cli.rb +428 -0
- data/lib/pinterest_dl/client.rb +61 -125
- data/lib/pinterest_dl/configuration.rb +104 -11
- data/lib/pinterest_dl/cookie_jar.rb +345 -0
- data/lib/pinterest_dl/downloader/part_file.rb +25 -0
- data/lib/pinterest_dl/downloader/response_plan.rb +194 -0
- data/lib/pinterest_dl/downloader.rb +714 -19
- data/lib/pinterest_dl/errors.rb +129 -15
- data/lib/pinterest_dl/extractors/image.rb +63 -9
- data/lib/pinterest_dl/extractors/pin_data.rb +156 -31
- data/lib/pinterest_dl/extractors/video.rb +42 -10
- data/lib/pinterest_dl/media_validator.rb +110 -0
- data/lib/pinterest_dl/models.rb +169 -0
- data/lib/pinterest_dl/paginator.rb +80 -0
- data/lib/pinterest_dl/progress_bar.rb +193 -4
- data/lib/pinterest_dl/search.rb +26 -42
- data/lib/pinterest_dl/security.rb +208 -0
- data/lib/pinterest_dl/session.rb +65 -0
- data/lib/pinterest_dl/transport.rb +322 -0
- data/lib/pinterest_dl/version.rb +1 -1
- metadata +54 -4
data/lib/pinterest_dl/api.rb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module PinterestDL
|
|
4
|
-
PIN_URL_PATTERN = %r{(pinterest\.[a-z
|
|
4
|
+
PIN_URL_PATTERN = %r{\Ahttps?://(?:[a-z0-9-]+\.)*(?:pinterest\.(?:com|[a-z]{2,3}(?:\.[a-z]{2})?)/pin/|pin\.it/)}i
|
|
5
5
|
|
|
6
6
|
class << self
|
|
7
7
|
def config
|
|
@@ -9,10 +9,20 @@ module PinterestDL
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
def configure
|
|
12
|
-
yield config
|
|
12
|
+
yield config if block_given?
|
|
13
13
|
config
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
def reset_config!
|
|
17
|
+
config.reset!
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def session(**options, &block)
|
|
21
|
+
sess = Session.new(**options)
|
|
22
|
+
sess.configure(&block) if block
|
|
23
|
+
sess
|
|
24
|
+
end
|
|
25
|
+
|
|
16
26
|
def logger
|
|
17
27
|
config.logger
|
|
18
28
|
end
|
|
@@ -21,7 +31,7 @@ module PinterestDL
|
|
|
21
31
|
html, pin_id = load_pin(pin_url, client)
|
|
22
32
|
url = extract_pin_image(html, pin_id, quality: quality)
|
|
23
33
|
url ||= api_image_url(pin_id, client, quality)
|
|
24
|
-
raise NotFoundError
|
|
34
|
+
raise NotFoundError.new("No image found at #{pin_url}", url: pin_url) unless url
|
|
25
35
|
|
|
26
36
|
url
|
|
27
37
|
end
|
|
@@ -38,7 +48,7 @@ module PinterestDL
|
|
|
38
48
|
end
|
|
39
49
|
|
|
40
50
|
hint = pin && pin[:image_url] ? ' (it looks like an image pin, try the image command)' : ''
|
|
41
|
-
raise NotFoundError
|
|
51
|
+
raise NotFoundError.new("No video found at #{pin_url}#{hint}", url: pin_url)
|
|
42
52
|
end
|
|
43
53
|
|
|
44
54
|
def get_media(pin_url, quality: config.quality, client: Client.new(config: config))
|
|
@@ -53,36 +63,58 @@ module PinterestDL
|
|
|
53
63
|
{ image_url: image, video_url: video }
|
|
54
64
|
end
|
|
55
65
|
|
|
56
|
-
def download(url, path:)
|
|
57
|
-
|
|
66
|
+
def download(url, path:, overwrite: nil, client: nil, **options, &progress)
|
|
67
|
+
active_client = client || Client.new(config: config)
|
|
68
|
+
Downloader.new(client: active_client, config: config).download(
|
|
69
|
+
url,
|
|
70
|
+
path: path,
|
|
71
|
+
overwrite: overwrite,
|
|
72
|
+
**options,
|
|
73
|
+
&progress
|
|
74
|
+
)
|
|
58
75
|
end
|
|
59
76
|
|
|
60
|
-
def download_batch(urls, dir:, filename: nil, &progress)
|
|
61
|
-
|
|
77
|
+
def download_batch(urls, dir:, filename: nil, concurrency: nil, overwrite: nil, client: nil, **options, &progress)
|
|
78
|
+
active_client = client || Client.new(config: config)
|
|
79
|
+
Downloader.new(client: active_client, config: config).download_batch(
|
|
80
|
+
urls,
|
|
81
|
+
dir: dir,
|
|
82
|
+
filename: filename,
|
|
83
|
+
concurrency: concurrency,
|
|
84
|
+
overwrite: overwrite,
|
|
85
|
+
**options,
|
|
86
|
+
&progress
|
|
87
|
+
)
|
|
62
88
|
end
|
|
63
89
|
|
|
64
|
-
def search(query, limit: 25, pages:
|
|
90
|
+
def search(query, limit: 25, pages: nil, type: :all, client: nil, config: self.config)
|
|
65
91
|
type = MediaType.parse(type)
|
|
66
|
-
|
|
67
|
-
|
|
92
|
+
active_client = client || Client.new(config: config)
|
|
93
|
+
needed_pages = (limit.to_f / 25).ceil + 2
|
|
94
|
+
effective_pages = pages || [needed_pages, 5].max
|
|
95
|
+
effective_pages = [effective_pages, 15].max unless type == :all
|
|
96
|
+
Search.new(client: active_client, config: config).call(query, limit: limit, pages: effective_pages, type: type)
|
|
68
97
|
end
|
|
69
98
|
|
|
70
|
-
def search_json(query, limit: 25)
|
|
71
|
-
JSON.pretty_generate(search(query, limit: limit))
|
|
99
|
+
def search_json(query, limit: 25, **options)
|
|
100
|
+
JSON.pretty_generate(search(query, limit: limit, **options))
|
|
72
101
|
end
|
|
73
102
|
|
|
74
|
-
def search_images(query, limit: 25)
|
|
75
|
-
search(query, limit: limit, type: :image).filter_map { |p| p[:image_url] }
|
|
103
|
+
def search_images(query, limit: 25, **options)
|
|
104
|
+
search(query, limit: limit, type: :image, **options).filter_map { |p| p[:image_url] }
|
|
76
105
|
end
|
|
77
106
|
|
|
78
|
-
def search_videos(query, limit: 25)
|
|
79
|
-
search(query, limit: limit, type: :video).filter_map { |p| p[:video_url] }
|
|
107
|
+
def search_videos(query, limit: 25, **options)
|
|
108
|
+
search(query, limit: limit, type: :video, **options).filter_map { |p| p[:video_url] }
|
|
80
109
|
end
|
|
81
110
|
|
|
82
|
-
def board(board_url, limit: 50, pages:
|
|
111
|
+
def board(board_url, limit: 50, pages: nil, type: :all, client: nil, config: self.config)
|
|
83
112
|
type = MediaType.parse(type)
|
|
84
|
-
|
|
85
|
-
|
|
113
|
+
active_client = client || Client.new(config: config)
|
|
114
|
+
needed_pages = (limit.to_f / Board::PAGE_SIZE).ceil + 2
|
|
115
|
+
effective_pages = pages || [needed_pages, 10].max
|
|
116
|
+
effective_pages = [effective_pages, 20].max unless type == :all
|
|
117
|
+
Board.new(client: active_client, config: config).call(board_url, limit: limit, pages: effective_pages, type: type)
|
|
86
118
|
end
|
|
87
119
|
|
|
88
120
|
private
|
|
@@ -109,7 +141,6 @@ module PinterestDL
|
|
|
109
141
|
'X-CSRFToken' => client.cookie('csrftoken') || 'undefined',
|
|
110
142
|
'Referer' => "https://www.pinterest.com/pin/#{pin_id}/"
|
|
111
143
|
}
|
|
112
|
-
|
|
113
144
|
pin = JSON.parse(client.get(uri.to_s, headers: headers, use_cache: false)).dig('resource_response', 'data')
|
|
114
145
|
return nil unless pin.is_a?(Hash)
|
|
115
146
|
|
|
@@ -118,7 +149,7 @@ module PinterestDL
|
|
|
118
149
|
video_url: Extractors::PinData.first_video_url(pin)
|
|
119
150
|
}
|
|
120
151
|
rescue PinterestDL::Error, JSON::ParserError => e
|
|
121
|
-
logger.debug("PinResource failed for pin #{pin_id}: #{e.message}")
|
|
152
|
+
logger.debug(Security.redact("PinResource failed for pin #{pin_id}: #{e.message}"))
|
|
122
153
|
nil
|
|
123
154
|
end
|
|
124
155
|
|
|
@@ -152,9 +183,13 @@ module PinterestDL
|
|
|
152
183
|
end
|
|
153
184
|
|
|
154
185
|
def validate_pin_url!(pin_url)
|
|
155
|
-
|
|
186
|
+
str = pin_url.to_s.strip
|
|
187
|
+
raise InvalidURLError.new("Not a Pinterest pin URL: #{pin_url.inspect}", url: pin_url) unless str.match?(PIN_URL_PATTERN)
|
|
156
188
|
|
|
157
|
-
|
|
189
|
+
uri = URI.parse(str)
|
|
190
|
+
raise InvalidURLError.new("Credentials not allowed in URL: #{pin_url.inspect}", url: pin_url) if uri.user || uri.password
|
|
191
|
+
rescue URI::InvalidURIError => e
|
|
192
|
+
raise InvalidURLError.new("Not a Pinterest pin URL: #{pin_url.inspect}", url: pin_url, cause_error: e)
|
|
158
193
|
end
|
|
159
194
|
end
|
|
160
195
|
end
|
data/lib/pinterest_dl/board.rb
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
4
|
require 'uri'
|
|
5
|
+
require_relative 'paginator'
|
|
6
|
+
require_relative 'media_type'
|
|
7
|
+
require_relative 'extractors/pin_data'
|
|
5
8
|
|
|
6
9
|
module PinterestDL
|
|
7
10
|
class Board
|
|
8
11
|
PAGE_SIZE = 50
|
|
9
|
-
RESERVED_PATHS = %w[pin search ideas today settings business].freeze
|
|
12
|
+
RESERVED_PATHS = %w[pin search ideas today settings business resource _/_/].freeze
|
|
10
13
|
|
|
11
14
|
def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
|
|
12
15
|
@client = client
|
|
@@ -14,49 +17,30 @@ module PinterestDL
|
|
|
14
17
|
end
|
|
15
18
|
|
|
16
19
|
def call(board_url, limit: 50, pages: 10, type: :all)
|
|
20
|
+
parsed_type = MediaType.parse(type)
|
|
17
21
|
username, slug, section = parse_url(board_url)
|
|
18
22
|
html = start_session(username, slug)
|
|
19
23
|
board_id = board_id_from_page(html, slug) || find_board_id(username, slug)
|
|
20
24
|
resource, options = feed_request(board_id, section)
|
|
21
|
-
|
|
25
|
+
|
|
26
|
+
paginator = Paginator.new(max_pages: pages, limit: limit, type: parsed_type) do |bookmarks, _page_idx|
|
|
27
|
+
fetch_page(resource, options, bookmarks)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
results = paginator.to_a
|
|
22
31
|
if results.empty?
|
|
23
32
|
fallback = PinterestDL::Extractors::PinData.extract_pins(html, quality: @config.quality)
|
|
24
|
-
results = PinterestDL::MediaType.filter(fallback,
|
|
33
|
+
results = PinterestDL::MediaType.filter(fallback, parsed_type)
|
|
25
34
|
end
|
|
26
35
|
results.first(limit)
|
|
27
36
|
end
|
|
28
37
|
|
|
29
38
|
private
|
|
30
39
|
|
|
31
|
-
def collect_pins(resource, options, limit:, pages:, type:)
|
|
32
|
-
results = []
|
|
33
|
-
seen = {}
|
|
34
|
-
bookmarks = nil
|
|
35
|
-
pages_fetched = 0
|
|
36
|
-
|
|
37
|
-
loop do
|
|
38
|
-
page = fetch_page(resource, options, bookmarks)
|
|
39
|
-
PinterestDL::MediaType.filter(page[:pins], type).each do |pin|
|
|
40
|
-
next if seen[pin[:id]]
|
|
41
|
-
|
|
42
|
-
seen[pin[:id]] = true
|
|
43
|
-
results << pin
|
|
44
|
-
end
|
|
45
|
-
bookmarks = page[:bookmarks]
|
|
46
|
-
pages_fetched += 1
|
|
47
|
-
|
|
48
|
-
break if results.size >= limit || stop_pagination?(bookmarks, pages_fetched, pages, page[:pins])
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
results
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def stop_pagination?(bookmarks, pages_fetched, max_pages, pins)
|
|
55
|
-
bookmarks.empty? || bookmarks == ['-end-'] || pages_fetched >= max_pages || pins.empty?
|
|
56
|
-
end
|
|
57
|
-
|
|
58
40
|
def parse_url(board_url)
|
|
59
|
-
url = board_url.to_s
|
|
41
|
+
url = board_url.to_s.strip
|
|
42
|
+
raise PinterestDL::InvalidURLError.new("Not a Pinterest board URL: #{board_url}", url: board_url) if url.empty?
|
|
43
|
+
|
|
60
44
|
if url.include?('pin.it/')
|
|
61
45
|
@client.raw_get(url)
|
|
62
46
|
url = @client.last_url
|
|
@@ -64,13 +48,14 @@ module PinterestDL
|
|
|
64
48
|
|
|
65
49
|
uri = URI(url)
|
|
66
50
|
parts = uri.path.to_s.split('/').reject(&:empty?)
|
|
67
|
-
valid = uri.host.to_s.include?('pinterest') &&
|
|
51
|
+
valid = uri.host.to_s.downcase.include?('pinterest.') &&
|
|
52
|
+
[2, 3].include?(parts.size) &&
|
|
68
53
|
!RESERVED_PATHS.include?(parts.first.to_s.downcase)
|
|
69
|
-
raise PinterestDL::InvalidURLError
|
|
54
|
+
raise PinterestDL::InvalidURLError.new("Not a Pinterest board URL: #{board_url}", url: board_url) unless valid
|
|
70
55
|
|
|
71
56
|
parts
|
|
72
|
-
rescue URI::InvalidURIError
|
|
73
|
-
raise PinterestDL::InvalidURLError
|
|
57
|
+
rescue URI::InvalidURIError => e
|
|
58
|
+
raise PinterestDL::InvalidURLError.new("Not a Pinterest board URL: #{board_url}", url: board_url, cause_error: e)
|
|
74
59
|
end
|
|
75
60
|
|
|
76
61
|
def start_session(username, slug)
|
|
@@ -83,8 +68,7 @@ module PinterestDL
|
|
|
83
68
|
end
|
|
84
69
|
|
|
85
70
|
def board_id_from_page(html, slug)
|
|
86
|
-
|
|
87
|
-
data = PinterestDL::Extractors::PinData.safe_parse(raw)
|
|
71
|
+
PinterestDL::Extractors::PinData.each_script_payload(html) do |data|
|
|
88
72
|
stack = [data]
|
|
89
73
|
until stack.empty?
|
|
90
74
|
node = stack.pop
|
|
@@ -136,19 +120,21 @@ module PinterestDL
|
|
|
136
120
|
def find_section_id(board_id, slug)
|
|
137
121
|
json = call_resource('BoardSectionsResource', board_id: board_id)
|
|
138
122
|
data = json.dig('resource_response', 'data')
|
|
139
|
-
found = data.is_a?(Array) && data.find { |s| s['slug'].to_s.casecmp?(slug.to_s) }
|
|
123
|
+
found = data.is_a?(Array) && data.find { |s| s.is_a?(Hash) && s['slug'].to_s.casecmp?(slug.to_s) }
|
|
140
124
|
raise PinterestDL::NotFoundError, "Section not found: #{slug}" unless found
|
|
141
125
|
|
|
142
126
|
found['id']
|
|
143
127
|
end
|
|
144
128
|
|
|
145
129
|
def fetch_page(resource, options, bookmarks)
|
|
146
|
-
|
|
147
|
-
json = call_resource(resource,
|
|
130
|
+
req_options = bookmarks && !bookmarks.empty? ? options.merge(bookmarks: bookmarks) : options
|
|
131
|
+
json = call_resource(resource, req_options)
|
|
148
132
|
|
|
149
133
|
data = json.dig('resource_response', 'data')
|
|
150
134
|
pins = data.is_a?(Array) ? data.filter_map { |r| build_pin(r) } : []
|
|
151
|
-
|
|
135
|
+
next_bookmarks = json.dig('resource_response', 'bookmark') ||
|
|
136
|
+
json.dig('resource', 'options', 'bookmarks')
|
|
137
|
+
{ pins: pins, bookmarks: Array(next_bookmarks) }
|
|
152
138
|
end
|
|
153
139
|
|
|
154
140
|
def call_resource(name, options)
|
|
@@ -157,7 +143,10 @@ module PinterestDL
|
|
|
157
143
|
uri.query = URI.encode_www_form('source_url' => @source_path, 'data' => data)
|
|
158
144
|
|
|
159
145
|
body = @client.get(uri.to_s, use_cache: false, headers: api_headers)
|
|
160
|
-
JSON.parse(body)
|
|
146
|
+
parsed = JSON.parse(body.to_s)
|
|
147
|
+
raise PinterestDL::NotFoundError, "Unexpected response from Pinterest (#{name})" unless parsed.is_a?(Hash)
|
|
148
|
+
|
|
149
|
+
parsed
|
|
161
150
|
rescue JSON::ParserError, TypeError
|
|
162
151
|
raise PinterestDL::NotFoundError, "Unexpected response from Pinterest (#{name})"
|
|
163
152
|
end
|