pinterest-dl 2.1.5 → 2.3.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 +30 -2
- data/README.md +77 -45
- data/bin/pinterest-dl +79 -28
- data/lib/pinterest-dl.rb +1 -1
- data/lib/pinterest_dl/api.rb +35 -18
- data/lib/pinterest_dl/board.rb +41 -32
- data/lib/pinterest_dl/client.rb +120 -29
- data/lib/pinterest_dl/configuration.rb +64 -13
- data/lib/pinterest_dl/downloader.rb +155 -17
- data/lib/pinterest_dl/errors.rb +1 -1
- data/lib/pinterest_dl/extractors/image.rb +9 -6
- data/lib/pinterest_dl/extractors/pin_data.rb +59 -5
- data/lib/pinterest_dl/extractors/video.rb +20 -10
- data/lib/pinterest_dl/media_type.rb +2 -3
- data/lib/pinterest_dl/progress_bar.rb +3 -4
- data/lib/pinterest_dl/search.rb +52 -50
- data/lib/pinterest_dl/version.rb +1 -2
- metadata +2 -2
data/lib/pinterest_dl/board.rb
CHANGED
|
@@ -6,6 +6,7 @@ require 'uri'
|
|
|
6
6
|
module PinterestDL
|
|
7
7
|
class Board
|
|
8
8
|
PAGE_SIZE = 50
|
|
9
|
+
RESERVED_PATHS = %w[pin search ideas today settings business].freeze
|
|
9
10
|
|
|
10
11
|
def initialize(client: PinterestDL::Client.new, config: PinterestDL.config)
|
|
11
12
|
@client = client
|
|
@@ -17,27 +18,42 @@ module PinterestDL
|
|
|
17
18
|
html = start_session(username, slug)
|
|
18
19
|
board_id = board_id_from_page(html, slug) || find_board_id(username, slug)
|
|
19
20
|
resource, options = feed_request(board_id, section)
|
|
21
|
+
results = collect_pins(resource, options, limit: limit, pages: pages, type: type)
|
|
22
|
+
if results.empty?
|
|
23
|
+
fallback = PinterestDL::Extractors::PinData.extract_pins(html, quality: @config.quality)
|
|
24
|
+
results = PinterestDL::MediaType.filter(fallback, type)
|
|
25
|
+
end
|
|
26
|
+
results.first(limit)
|
|
27
|
+
end
|
|
20
28
|
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def collect_pins(resource, options, limit:, pages:, type:)
|
|
21
32
|
results = []
|
|
33
|
+
seen = {}
|
|
22
34
|
bookmarks = nil
|
|
23
35
|
pages_fetched = 0
|
|
24
36
|
|
|
25
37
|
loop do
|
|
26
38
|
page = fetch_page(resource, options, bookmarks)
|
|
27
|
-
|
|
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
|
|
28
45
|
bookmarks = page[:bookmarks]
|
|
29
46
|
pages_fetched += 1
|
|
30
47
|
|
|
31
|
-
break if results.size >= limit
|
|
32
|
-
break if bookmarks.empty? || bookmarks == ['-end-']
|
|
33
|
-
break if pages_fetched >= pages
|
|
34
|
-
break if page[:pins].empty?
|
|
48
|
+
break if results.size >= limit || stop_pagination?(bookmarks, pages_fetched, pages, page[:pins])
|
|
35
49
|
end
|
|
36
50
|
|
|
37
|
-
results
|
|
51
|
+
results
|
|
38
52
|
end
|
|
39
53
|
|
|
40
|
-
|
|
54
|
+
def stop_pagination?(bookmarks, pages_fetched, max_pages, pins)
|
|
55
|
+
bookmarks.empty? || bookmarks == ['-end-'] || pages_fetched >= max_pages || pins.empty?
|
|
56
|
+
end
|
|
41
57
|
|
|
42
58
|
def parse_url(board_url)
|
|
43
59
|
url = board_url.to_s
|
|
@@ -48,7 +64,8 @@ module PinterestDL
|
|
|
48
64
|
|
|
49
65
|
uri = URI(url)
|
|
50
66
|
parts = uri.path.to_s.split('/').reject(&:empty?)
|
|
51
|
-
valid = uri.host.to_s.include?('pinterest') && [2, 3].include?(parts.size) &&
|
|
67
|
+
valid = uri.host.to_s.include?('pinterest') && [2, 3].include?(parts.size) &&
|
|
68
|
+
!RESERVED_PATHS.include?(parts.first.to_s.downcase)
|
|
52
69
|
raise PinterestDL::InvalidURLError, "Not a Pinterest board URL: #{board_url}" unless valid
|
|
53
70
|
|
|
54
71
|
parts
|
|
@@ -57,10 +74,11 @@ module PinterestDL
|
|
|
57
74
|
end
|
|
58
75
|
|
|
59
76
|
def start_session(username, slug)
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
@
|
|
63
|
-
@
|
|
77
|
+
target = "https://www.pinterest.com/#{username}/#{slug}/"
|
|
78
|
+
response = @client.raw_get(target)
|
|
79
|
+
@page_url = @client.last_url || target
|
|
80
|
+
@host = URI(@page_url).host || 'www.pinterest.com'
|
|
81
|
+
@source_path = URI(@page_url).path || "/#{username}/#{slug}/"
|
|
64
82
|
response.body
|
|
65
83
|
end
|
|
66
84
|
|
|
@@ -80,11 +98,12 @@ module PinterestDL
|
|
|
80
98
|
end
|
|
81
99
|
end
|
|
82
100
|
end
|
|
83
|
-
|
|
101
|
+
html.to_s[/"board_id"\s*:\s*"(\d+)"/, 1]
|
|
84
102
|
end
|
|
85
103
|
|
|
86
104
|
def board_node?(node, slug)
|
|
87
|
-
node['type'] == 'board' && node['id'].to_s.match?(/\A\d+\z/) &&
|
|
105
|
+
node['type'] == 'board' && node['id'].to_s.match?(/\A\d+\z/) &&
|
|
106
|
+
node['url'].to_s.downcase.include?("/#{slug.to_s.downcase}/")
|
|
88
107
|
end
|
|
89
108
|
|
|
90
109
|
def find_board_id(username, slug)
|
|
@@ -93,10 +112,11 @@ module PinterestDL
|
|
|
93
112
|
id = data.is_a?(Hash) && data['id']
|
|
94
113
|
raise PinterestDL::NotFoundError, "Board not found: #{username}/#{slug} (private boards need --cookies)" unless id
|
|
95
114
|
|
|
96
|
-
id
|
|
115
|
+
id.to_s
|
|
97
116
|
end
|
|
98
117
|
|
|
99
118
|
def feed_request(board_id, section)
|
|
119
|
+
@section = section
|
|
100
120
|
unless section
|
|
101
121
|
options = {
|
|
102
122
|
board_id: board_id,
|
|
@@ -116,7 +136,7 @@ module PinterestDL
|
|
|
116
136
|
def find_section_id(board_id, slug)
|
|
117
137
|
json = call_resource('BoardSectionsResource', board_id: board_id)
|
|
118
138
|
data = json.dig('resource_response', 'data')
|
|
119
|
-
found = data.is_a?(Array) && data.find { |s| s['slug']
|
|
139
|
+
found = data.is_a?(Array) && data.find { |s| s['slug'].to_s.casecmp?(slug.to_s) }
|
|
120
140
|
raise PinterestDL::NotFoundError, "Section not found: #{slug}" unless found
|
|
121
141
|
|
|
122
142
|
found['id']
|
|
@@ -143,31 +163,20 @@ module PinterestDL
|
|
|
143
163
|
end
|
|
144
164
|
|
|
145
165
|
def api_headers
|
|
166
|
+
handler = @section ? 'www/[username]/[slug]/[section_slug].js' : 'www/[username]/[slug].js'
|
|
146
167
|
{
|
|
147
168
|
'Accept' => 'application/json, text/javascript, */*, q=0.01',
|
|
148
169
|
'X-Requested-With' => 'XMLHttpRequest',
|
|
149
|
-
'X-Pinterest-
|
|
170
|
+
'X-Pinterest-AppState' => 'active',
|
|
171
|
+
'X-Pinterest-Source-Url' => @source_path,
|
|
172
|
+
'X-Pinterest-PWS-Handler' => handler,
|
|
150
173
|
'X-CSRFToken' => @client.cookie('csrftoken', @page_url) || 'undefined',
|
|
151
174
|
'Referer' => @page_url
|
|
152
175
|
}
|
|
153
176
|
end
|
|
154
177
|
|
|
155
178
|
def build_pin(raw)
|
|
156
|
-
|
|
157
|
-
return nil if raw['type'] && raw['type'] != 'pin'
|
|
158
|
-
|
|
159
|
-
id = raw['id']
|
|
160
|
-
return nil unless id
|
|
161
|
-
|
|
162
|
-
{
|
|
163
|
-
id: id,
|
|
164
|
-
pin_url: "https://www.pinterest.com/pin/#{id}/",
|
|
165
|
-
title: raw['title'] || raw['grid_title'],
|
|
166
|
-
description: raw['description'],
|
|
167
|
-
creator: raw.dig('pinner', 'username') || raw.dig('pinner', 'full_name'),
|
|
168
|
-
image_url: raw.dig('images', 'orig', 'url'),
|
|
169
|
-
video_url: PinterestDL::Extractors::PinData.first_video_url(raw)
|
|
170
|
-
}
|
|
179
|
+
PinterestDL::Extractors::PinData.build_pin_hash(raw, quality: @config.quality)
|
|
171
180
|
end
|
|
172
181
|
end
|
|
173
182
|
end
|
data/lib/pinterest_dl/client.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Monji024
|
|
4
3
|
require 'net/http'
|
|
5
4
|
require 'uri'
|
|
6
5
|
require 'openssl'
|
|
@@ -8,6 +7,7 @@ require 'openssl'
|
|
|
8
7
|
module PinterestDL
|
|
9
8
|
class Client
|
|
10
9
|
MAX_REDIRECTS = 5
|
|
10
|
+
RETRYABLE_HTTP_CODES = %w[408 429 500 502 503 504].freeze
|
|
11
11
|
|
|
12
12
|
attr_reader :last_url
|
|
13
13
|
|
|
@@ -17,17 +17,21 @@ module PinterestDL
|
|
|
17
17
|
@cookies = Hash.new { |hash, key| hash[key] = {} }
|
|
18
18
|
@last_request_at = nil
|
|
19
19
|
@last_url = nil
|
|
20
|
+
@mutex = Mutex.new
|
|
20
21
|
end
|
|
21
22
|
|
|
22
23
|
def get(url, headers: {}, use_cache: true)
|
|
23
24
|
cache_key = [url, headers].hash
|
|
24
|
-
if use_cache
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
if use_cache
|
|
26
|
+
cached = @mutex.synchronize { @cache[cache_key] }
|
|
27
|
+
if cached
|
|
28
|
+
body, @last_url = cached
|
|
29
|
+
return body
|
|
30
|
+
end
|
|
27
31
|
end
|
|
28
32
|
|
|
29
33
|
body = fetch(url, headers: headers).body
|
|
30
|
-
|
|
34
|
+
store_cache(cache_key, [body, @last_url]) if use_cache
|
|
31
35
|
body
|
|
32
36
|
end
|
|
33
37
|
|
|
@@ -35,21 +39,38 @@ module PinterestDL
|
|
|
35
39
|
fetch(url, headers: headers)
|
|
36
40
|
end
|
|
37
41
|
|
|
42
|
+
def stream_get(url, headers: {}, &block)
|
|
43
|
+
raise ArgumentError, 'stream_get requires a block' unless block_given?
|
|
44
|
+
|
|
45
|
+
fetch(url, headers: headers, &block)
|
|
46
|
+
end
|
|
47
|
+
|
|
38
48
|
def cookie(name, url = @last_url)
|
|
39
|
-
|
|
49
|
+
target = url.to_s.empty? ? 'https://www.pinterest.com' : url.to_s
|
|
50
|
+
key = site_key(URI(target))
|
|
51
|
+
@mutex.synchronize { @cookies[key][name] }
|
|
52
|
+
rescue URI::InvalidURIError
|
|
53
|
+
nil
|
|
40
54
|
end
|
|
41
55
|
|
|
42
56
|
def clear_cache
|
|
43
|
-
@cache.clear
|
|
57
|
+
@mutex.synchronize { @cache.clear }
|
|
44
58
|
end
|
|
45
59
|
|
|
46
60
|
private
|
|
47
61
|
|
|
48
|
-
def
|
|
62
|
+
def store_cache(key, value)
|
|
63
|
+
@mutex.synchronize do
|
|
64
|
+
max = @config.respond_to?(:max_cache_entries) ? @config.max_cache_entries.to_i : 256
|
|
65
|
+
@cache.shift if max.positive? && @cache.size >= max
|
|
66
|
+
@cache[key] = value if max.positive?
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def fetch(url, headers: {}, &stream_block)
|
|
49
71
|
(MAX_REDIRECTS + 1).times do
|
|
50
|
-
response = request_with_retries(url, headers: headers)
|
|
72
|
+
response = request_with_retries(url, headers: headers, &stream_block)
|
|
51
73
|
save_cookies(response, url)
|
|
52
|
-
|
|
53
74
|
unless response.is_a?(Net::HTTPRedirection)
|
|
54
75
|
check_response(response, url)
|
|
55
76
|
@last_url = url
|
|
@@ -61,19 +82,32 @@ module PinterestDL
|
|
|
61
82
|
|
|
62
83
|
url = URI.join(url, location).to_s
|
|
63
84
|
end
|
|
85
|
+
|
|
64
86
|
raise PinterestDL::NetworkError, "Too many redirects for #{url}"
|
|
65
87
|
end
|
|
66
88
|
|
|
67
|
-
def request_with_retries(url, headers: {})
|
|
89
|
+
def request_with_retries(url, headers: {}, &stream_block)
|
|
68
90
|
attempts = 0
|
|
69
91
|
begin
|
|
70
92
|
throttle!
|
|
71
93
|
uri = URI(url)
|
|
72
94
|
http = build_http(uri)
|
|
73
95
|
req = Net::HTTP::Get.new(uri, default_headers(uri).merge(headers))
|
|
74
|
-
http
|
|
75
|
-
|
|
76
|
-
|
|
96
|
+
response = perform_request(http, req, &stream_block)
|
|
97
|
+
|
|
98
|
+
if retryable_response?(response) && attempts < @config.max_retries
|
|
99
|
+
attempts += 1
|
|
100
|
+
wait = retry_delay_for(response, attempts)
|
|
101
|
+
@config.logger.warn("HTTP #{response.code} from #{url}, retrying in #{wait}s (#{attempts}/#{@config.max_retries})")
|
|
102
|
+
sleep(wait) if wait.positive?
|
|
103
|
+
raise RetryableResponseSignal
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
response
|
|
107
|
+
rescue RetryableResponseSignal
|
|
108
|
+
retry
|
|
109
|
+
rescue Timeout::Error, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ETIMEDOUT,
|
|
110
|
+
Errno::EHOSTUNREACH, SocketError, OpenSSL::SSL::SSLError, EOFError => e
|
|
77
111
|
attempts += 1
|
|
78
112
|
if attempts <= @config.max_retries
|
|
79
113
|
@config.logger.warn("request to #{url} failed (#{e.class}), retrying (#{attempts}/#{@config.max_retries})")
|
|
@@ -84,6 +118,33 @@ module PinterestDL
|
|
|
84
118
|
end
|
|
85
119
|
end
|
|
86
120
|
|
|
121
|
+
class RetryableResponseSignal < StandardError; end
|
|
122
|
+
private_constant :RetryableResponseSignal
|
|
123
|
+
|
|
124
|
+
def perform_request(http, req, &stream_block)
|
|
125
|
+
return http.request(req) unless stream_block
|
|
126
|
+
|
|
127
|
+
http.request(req) do |response|
|
|
128
|
+
if response.is_a?(Net::HTTPSuccess)
|
|
129
|
+
response.read_body(&stream_block)
|
|
130
|
+
end
|
|
131
|
+
return response
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def retryable_response?(response)
|
|
136
|
+
RETRYABLE_HTTP_CODES.include?(response.code.to_s)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def retry_delay_for(response, attempts)
|
|
140
|
+
header = response['Retry-After'].to_s.strip
|
|
141
|
+
if header.match?(/\A\d+(\.\d+)?\z/)
|
|
142
|
+
header.to_f
|
|
143
|
+
else
|
|
144
|
+
@config.retry_wait * attempts
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
87
148
|
def check_response(response, url)
|
|
88
149
|
case response
|
|
89
150
|
when Net::HTTPSuccess
|
|
@@ -93,6 +154,8 @@ module PinterestDL
|
|
|
93
154
|
"Rate limited while requesting #{url}",
|
|
94
155
|
retry_after: response['Retry-After']
|
|
95
156
|
)
|
|
157
|
+
when Net::HTTPUnauthorized
|
|
158
|
+
raise PinterestDL::AuthenticationError, "Authentication required for #{url}"
|
|
96
159
|
when Net::HTTPNotFound
|
|
97
160
|
raise PinterestDL::NotFoundError, "Nothing found at #{url}"
|
|
98
161
|
when Net::HTTPForbidden
|
|
@@ -103,13 +166,24 @@ module PinterestDL
|
|
|
103
166
|
end
|
|
104
167
|
|
|
105
168
|
def build_http(uri)
|
|
106
|
-
http =
|
|
169
|
+
http = build_proxy_http(uri)
|
|
107
170
|
http.use_ssl = (uri.scheme == 'https')
|
|
108
171
|
http.open_timeout = @config.open_timeout
|
|
109
172
|
http.read_timeout = @config.read_timeout
|
|
173
|
+
http.write_timeout = @config.write_timeout if http.respond_to?(:write_timeout=) && @config.respond_to?(:write_timeout)
|
|
110
174
|
http
|
|
111
175
|
end
|
|
112
176
|
|
|
177
|
+
def build_proxy_http(uri)
|
|
178
|
+
proxy_url = @config.respond_to?(:proxy) ? @config.proxy : nil
|
|
179
|
+
return Net::HTTP.new(uri.host, uri.port) if proxy_url.nil? || proxy_url.to_s.strip.empty?
|
|
180
|
+
|
|
181
|
+
p_uri = URI(proxy_url.to_s)
|
|
182
|
+
Net::HTTP.new(uri.host, uri.port, p_uri.host, p_uri.port, p_uri.user, p_uri.password)
|
|
183
|
+
rescue URI::InvalidURIError
|
|
184
|
+
Net::HTTP.new(uri.host, uri.port)
|
|
185
|
+
end
|
|
186
|
+
|
|
113
187
|
def default_headers(uri)
|
|
114
188
|
headers = {
|
|
115
189
|
'User-Agent' => @config.user_agent,
|
|
@@ -123,33 +197,50 @@ module PinterestDL
|
|
|
123
197
|
def cookie_header(uri)
|
|
124
198
|
parts = []
|
|
125
199
|
parts << @config.cookies.to_s if @config.authenticated?
|
|
126
|
-
|
|
200
|
+
key = site_key(uri)
|
|
201
|
+
jar_pairs = @mutex.synchronize { @cookies[key].map { |name, value| "#{name}=#{value}" } }
|
|
202
|
+
parts.concat(jar_pairs)
|
|
127
203
|
parts.join('; ')
|
|
128
204
|
end
|
|
129
205
|
|
|
130
206
|
def save_cookies(response, url)
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
207
|
+
fields = response.get_fields('set-cookie')
|
|
208
|
+
return if fields.nil? || fields.empty?
|
|
209
|
+
|
|
210
|
+
key = site_key(URI(url))
|
|
211
|
+
@mutex.synchronize do
|
|
212
|
+
jar = @cookies[key]
|
|
213
|
+
fields.each do |line|
|
|
214
|
+
name, value = line.split(';').first.to_s.split('=', 2)
|
|
215
|
+
next if name.nil? || value.nil?
|
|
135
216
|
|
|
136
|
-
|
|
217
|
+
value.strip.empty? ? jar.delete(name.strip) : jar[name.strip] = value.strip
|
|
218
|
+
end
|
|
137
219
|
end
|
|
138
220
|
end
|
|
139
221
|
|
|
140
222
|
def site_key(uri)
|
|
141
|
-
uri.host.to_s.
|
|
223
|
+
host = uri.host.to_s.downcase
|
|
224
|
+
return 'pinterest.com' if host.include?('pinterest.')
|
|
225
|
+
|
|
226
|
+
host.split('.').last(2).join('.')
|
|
142
227
|
end
|
|
143
228
|
|
|
144
229
|
def throttle!
|
|
145
230
|
return if @config.rate_limit_interval <= 0
|
|
146
|
-
return unless @last_request_at
|
|
147
231
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
232
|
+
@mutex.synchronize do
|
|
233
|
+
if @last_request_at
|
|
234
|
+
elapsed = monotonic_now - @last_request_at
|
|
235
|
+
wait = @config.rate_limit_interval - elapsed
|
|
236
|
+
sleep(wait) if wait.positive?
|
|
237
|
+
end
|
|
238
|
+
@last_request_at = monotonic_now
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def monotonic_now
|
|
243
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
153
244
|
end
|
|
154
245
|
end
|
|
155
|
-
end
|
|
246
|
+
end
|
|
@@ -6,29 +6,35 @@ module PinterestDL
|
|
|
6
6
|
class Configuration
|
|
7
7
|
DEFAULT_USER_AGENT =
|
|
8
8
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' \
|
|
9
|
-
'(KHTML, like Gecko) Chrome/
|
|
9
|
+
'(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
10
10
|
|
|
11
|
-
VALID_QUALITIES = %i[originals 736x 474x 236x].freeze
|
|
11
|
+
VALID_QUALITIES = %i[originals 1200x 736x 564x 474x 236x].freeze
|
|
12
12
|
|
|
13
|
-
attr_accessor :
|
|
14
|
-
:max_retries, :retry_wait, :logger,
|
|
15
|
-
:
|
|
16
|
-
|
|
17
|
-
attr_reader :quality
|
|
13
|
+
attr_accessor :user_agent, :open_timeout, :read_timeout, :write_timeout,
|
|
14
|
+
:max_retries, :retry_wait, :logger, :rate_limit_interval,
|
|
15
|
+
:proxy, :max_cache_entries, :concurrency, :overwrite
|
|
16
|
+
attr_reader :quality, :cookies
|
|
18
17
|
|
|
19
18
|
def initialize
|
|
19
|
+
reset!
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def reset!
|
|
20
23
|
@user_agent = DEFAULT_USER_AGENT
|
|
21
24
|
@cookies = nil
|
|
22
25
|
@open_timeout = 10
|
|
23
26
|
@read_timeout = 20
|
|
27
|
+
@write_timeout = 20
|
|
24
28
|
@max_retries = 3
|
|
25
29
|
@retry_wait = 1.0
|
|
26
30
|
@quality = :originals
|
|
27
31
|
@rate_limit_interval = 0.0
|
|
28
|
-
@
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
@proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV['http_proxy']
|
|
33
|
+
@max_cache_entries = 256
|
|
34
|
+
@concurrency = 4
|
|
35
|
+
@overwrite = true
|
|
36
|
+
@logger = default_logger
|
|
37
|
+
self
|
|
32
38
|
end
|
|
33
39
|
|
|
34
40
|
def quality=(value)
|
|
@@ -39,8 +45,53 @@ module PinterestDL
|
|
|
39
45
|
@quality = value
|
|
40
46
|
end
|
|
41
47
|
|
|
48
|
+
def cookies=(value)
|
|
49
|
+
if value.nil? || value.to_s.strip.empty?
|
|
50
|
+
@cookies = nil
|
|
51
|
+
return
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
raw = value.to_s.strip
|
|
55
|
+
@cookies = File.file?(raw) ? parse_cookie_file(raw) : raw
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def concurrency=(value)
|
|
59
|
+
num = value.to_i
|
|
60
|
+
raise ArgumentError, 'concurrency must be at least 1' if num < 1
|
|
61
|
+
|
|
62
|
+
@concurrency = num
|
|
63
|
+
end
|
|
64
|
+
|
|
42
65
|
def authenticated?
|
|
43
|
-
|
|
66
|
+
!@cookies.nil? && !@cookies.empty?
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def parse_cookie_file(path)
|
|
72
|
+
pairs = []
|
|
73
|
+
File.foreach(path) do |line|
|
|
74
|
+
stripped = line.strip
|
|
75
|
+
next if stripped.empty? || stripped.start_with?('#')
|
|
76
|
+
|
|
77
|
+
fields = stripped.split("\t")
|
|
78
|
+
if fields.size >= 7
|
|
79
|
+
domain, _flag, _path, _secure, _expiry, name, val = fields
|
|
80
|
+
next unless domain.include?('pinterest.')
|
|
81
|
+
|
|
82
|
+
pairs << "#{name}=#{val}"
|
|
83
|
+
elsif stripped.include?('=') && !stripped.include?("\t")
|
|
84
|
+
pairs << stripped
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
pairs.join('; ')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def default_logger
|
|
91
|
+
Logger.new($stderr).tap do |log|
|
|
92
|
+
log.level = Logger::WARN
|
|
93
|
+
log.formatter = proc { |severity, _time, _progname, msg| "[pinterest-dl] #{severity}: #{msg}\n" }
|
|
94
|
+
end
|
|
44
95
|
end
|
|
45
96
|
end
|
|
46
|
-
end
|
|
97
|
+
end
|