pinterest-dl 2.3.0 → 2.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 +4 -4
- data/.yardopts +7 -0
- data/CHANGELOG.md +33 -0
- data/README.md +174 -145
- data/bin/pinterest-dl +1 -162
- data/lib/pinterest-dl.rb +9 -1
- data/lib/pinterest_dl/api.rb +47 -22
- data/lib/pinterest_dl/board.rb +38 -45
- data/lib/pinterest_dl/cli.rb +455 -0
- data/lib/pinterest_dl/client.rb +53 -200
- data/lib/pinterest_dl/configuration.rb +77 -24
- data/lib/pinterest_dl/cookie_jar.rb +318 -0
- data/lib/pinterest_dl/downloader.rb +735 -46
- data/lib/pinterest_dl/errors.rb +129 -15
- data/lib/pinterest_dl/extractors/image.rb +60 -9
- data/lib/pinterest_dl/extractors/pin_data.rb +158 -32
- 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 +81 -0
- data/lib/pinterest_dl/progress_bar.rb +195 -4
- data/lib/pinterest_dl/search.rb +30 -44
- data/lib/pinterest_dl/security.rb +208 -0
- data/lib/pinterest_dl/session.rb +65 -0
- data/lib/pinterest_dl/transport.rb +326 -0
- data/lib/pinterest_dl/tui/app.rb +498 -0
- data/lib/pinterest_dl/tui/download_queue.rb +423 -0
- data/lib/pinterest_dl/tui/icons.rb +113 -0
- data/lib/pinterest_dl/tui/preview.rb +674 -0
- data/lib/pinterest_dl/tui/renderer.rb +738 -0
- data/lib/pinterest_dl/tui/state.rb +190 -0
- data/lib/pinterest_dl/tui/terminal.rb +281 -0
- data/lib/pinterest_dl/tui/theme.rb +318 -0
- data/lib/pinterest_dl/tui.rb +53 -0
- data/lib/pinterest_dl/version.rb +1 -1
- metadata +21 -3
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
require 'uri'
|
|
5
|
+
require_relative 'security'
|
|
6
|
+
|
|
7
|
+
module PinterestDL
|
|
8
|
+
class CookieJar
|
|
9
|
+
Cookie = Struct.new(:name, :value, :domain, :host_only, :path, :secure, :http_only, :expires, :same_site, keyword_init: true) do
|
|
10
|
+
def expired?(now = Time.now)
|
|
11
|
+
!expires.nil? && expires <= now
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def matches?(uri, now: Time.now)
|
|
15
|
+
return false if expired?(now)
|
|
16
|
+
return false if secure && uri.scheme.to_s.downcase != 'https'
|
|
17
|
+
|
|
18
|
+
host = Security.clean_host(uri.host)
|
|
19
|
+
return false unless domain_matches?(host)
|
|
20
|
+
|
|
21
|
+
req_path = uri.path.to_s.empty? ? '/' : uri.path.to_s
|
|
22
|
+
path_matches?(req_path)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def domain_matches?(host)
|
|
26
|
+
target_domain = domain.to_s.downcase.sub(/\A\./, '')
|
|
27
|
+
return false if target_domain.empty?
|
|
28
|
+
return true if host == target_domain
|
|
29
|
+
return true if target_domain == 'pinterest.com' && Security.pinterest_host?(host)
|
|
30
|
+
return false if host_only
|
|
31
|
+
|
|
32
|
+
host.end_with?(".#{target_domain}")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def path_matches?(req_path)
|
|
36
|
+
cookie_path = path.to_s.empty? ? '/' : path.to_s
|
|
37
|
+
return true if req_path == cookie_path
|
|
38
|
+
return true if req_path.start_with?(cookie_path) && cookie_path.end_with?('/')
|
|
39
|
+
return true if req_path.start_with?("#{cookie_path}/")
|
|
40
|
+
|
|
41
|
+
false
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def initialize
|
|
46
|
+
@entries = []
|
|
47
|
+
@mutex = Mutex.new
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def initialize_copy(other)
|
|
51
|
+
super
|
|
52
|
+
@mutex = Mutex.new
|
|
53
|
+
@entries = other.entries_snapshot.map(&:dup)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def entries_snapshot
|
|
57
|
+
@mutex.synchronize { @entries.map(&:dup) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def clear
|
|
61
|
+
@mutex.synchronize { @entries.clear }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def empty?
|
|
65
|
+
@mutex.synchronize { @entries.empty? }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def set_from_header(set_cookie_fields, origin_url)
|
|
69
|
+
fields = Array(set_cookie_fields).flatten.compact
|
|
70
|
+
return if fields.empty?
|
|
71
|
+
|
|
72
|
+
uri = URI.parse(origin_url.to_s)
|
|
73
|
+
@mutex.synchronize do
|
|
74
|
+
fields.each do |raw_line|
|
|
75
|
+
cookie = parse_set_cookie(raw_line, uri)
|
|
76
|
+
upsert_unlocked(cookie) if cookie
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
rescue URI::InvalidURIError
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def load_raw(raw_cookies, default_domain: '.pinterest.com')
|
|
84
|
+
return if raw_cookies.nil? || raw_cookies.to_s.strip.empty?
|
|
85
|
+
|
|
86
|
+
text = raw_cookies.to_s.strip
|
|
87
|
+
if File.file?(text)
|
|
88
|
+
load_netscape_file(text)
|
|
89
|
+
else
|
|
90
|
+
load_cookie_string(text, default_domain: default_domain)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def load_cookie_string(cookie_str, default_domain: '.pinterest.com')
|
|
95
|
+
@mutex.synchronize do
|
|
96
|
+
cookie_str.to_s.split(';').each do |pair|
|
|
97
|
+
name, value = pair.strip.split('=', 2)
|
|
98
|
+
next if name.nil? || value.nil? || name.strip.empty?
|
|
99
|
+
|
|
100
|
+
cookie = Cookie.new(
|
|
101
|
+
name: name.strip,
|
|
102
|
+
value: value.strip,
|
|
103
|
+
domain: default_domain.sub(/\A\./, ''),
|
|
104
|
+
host_only: false,
|
|
105
|
+
path: '/',
|
|
106
|
+
secure: false,
|
|
107
|
+
http_only: false,
|
|
108
|
+
expires: nil,
|
|
109
|
+
same_site: nil
|
|
110
|
+
)
|
|
111
|
+
upsert_unlocked(cookie)
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def load_netscape_file(file_path)
|
|
117
|
+
@mutex.synchronize do
|
|
118
|
+
File.foreach(file_path) do |line|
|
|
119
|
+
stripped = line.strip
|
|
120
|
+
next if stripped.empty?
|
|
121
|
+
|
|
122
|
+
if stripped.start_with?('#HttpOnly_')
|
|
123
|
+
stripped = stripped.sub(/\A#HttpOnly_/, '')
|
|
124
|
+
http_only = true
|
|
125
|
+
elsif stripped.start_with?('#')
|
|
126
|
+
next
|
|
127
|
+
else
|
|
128
|
+
http_only = false
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
fields = stripped.split("\t")
|
|
132
|
+
if fields.size >= 7
|
|
133
|
+
domain, include_subdomains, path, secure_flag, expiry_raw, name, value = fields
|
|
134
|
+
next unless Security.pinterest_host?(domain.sub(/\A\./, ''))
|
|
135
|
+
|
|
136
|
+
exp_int = expiry_raw.to_i
|
|
137
|
+
expires = exp_int.positive? ? Time.at(exp_int) : nil
|
|
138
|
+
cookie = Cookie.new(
|
|
139
|
+
name: name.strip,
|
|
140
|
+
value: value.to_s.strip,
|
|
141
|
+
domain: domain.sub(/\A\./, '').downcase,
|
|
142
|
+
host_only: include_subdomains.to_s.upcase != 'TRUE' && !domain.start_with?('.'),
|
|
143
|
+
path: path.to_s.empty? ? '/' : path.strip,
|
|
144
|
+
secure: secure_flag.to_s.upcase == 'TRUE',
|
|
145
|
+
http_only: http_only,
|
|
146
|
+
expires: expires,
|
|
147
|
+
same_site: nil
|
|
148
|
+
)
|
|
149
|
+
upsert_unlocked(cookie)
|
|
150
|
+
elsif stripped.include?('=') && !stripped.include?("\t")
|
|
151
|
+
stripped.split(';').each do |pair|
|
|
152
|
+
k, v = pair.strip.split('=', 2)
|
|
153
|
+
next if k.nil? || v.nil? || k.empty?
|
|
154
|
+
|
|
155
|
+
upsert_unlocked(
|
|
156
|
+
Cookie.new(
|
|
157
|
+
name: k,
|
|
158
|
+
value: v,
|
|
159
|
+
domain: 'pinterest.com',
|
|
160
|
+
host_only: false,
|
|
161
|
+
path: '/',
|
|
162
|
+
secure: false,
|
|
163
|
+
http_only: false,
|
|
164
|
+
expires: nil,
|
|
165
|
+
same_site: nil
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def header_for(url_or_uri, allow_local: false)
|
|
175
|
+
uri = url_or_uri.is_a?(URI) ? url_or_uri : URI.parse(url_or_uri.to_s)
|
|
176
|
+
return '' unless Security.cookie_eligible_host?(uri.host, allow_local: allow_local)
|
|
177
|
+
|
|
178
|
+
now = Time.now
|
|
179
|
+
matched = @mutex.synchronize do
|
|
180
|
+
@entries.reject! { |c| c.expired?(now) }
|
|
181
|
+
@entries
|
|
182
|
+
.select { |c| c.matches?(uri, now: now) }
|
|
183
|
+
.sort_by { |c| -c.path.to_s.length }
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
seen = {}
|
|
187
|
+
pairs = matched.each_with_object([]) do |cookie, acc|
|
|
188
|
+
next if seen[cookie.name]
|
|
189
|
+
|
|
190
|
+
seen[cookie.name] = true
|
|
191
|
+
acc << "#{cookie.name}=#{cookie.value}"
|
|
192
|
+
end
|
|
193
|
+
pairs.join('; ')
|
|
194
|
+
rescue URI::InvalidURIError
|
|
195
|
+
''
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def value_for(name, url_or_uri = 'https://www.pinterest.com', allow_local: true)
|
|
199
|
+
target = url_or_uri.to_s.strip.empty? ? 'https://www.pinterest.com' : url_or_uri.to_s
|
|
200
|
+
uri = target.is_a?(URI) ? target : URI.parse(target)
|
|
201
|
+
return nil unless Security.cookie_eligible_host?(uri.host, allow_local: allow_local)
|
|
202
|
+
|
|
203
|
+
now = Time.now
|
|
204
|
+
@mutex.synchronize do
|
|
205
|
+
@entries.reject! { |c| c.expired?(now) }
|
|
206
|
+
found = @entries
|
|
207
|
+
.select { |c| c.name == name.to_s && c.matches?(uri, now: now) }
|
|
208
|
+
.max_by { |c| c.path.to_s.length }
|
|
209
|
+
found&.value
|
|
210
|
+
end
|
|
211
|
+
rescue URI::InvalidURIError
|
|
212
|
+
nil
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def to_cookie_string(domain: 'https://www.pinterest.com')
|
|
216
|
+
header_for(domain, allow_local: true)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
private
|
|
220
|
+
|
|
221
|
+
def parse_set_cookie(raw_line, uri)
|
|
222
|
+
segments = raw_line.to_s.split(';').map(&:strip).reject(&:empty?)
|
|
223
|
+
return nil if segments.empty?
|
|
224
|
+
|
|
225
|
+
first = segments.shift
|
|
226
|
+
name, value = first.split('=', 2)
|
|
227
|
+
return nil if name.nil? || value.nil? || name.strip.empty?
|
|
228
|
+
|
|
229
|
+
origin_host = Security.clean_host(uri.host)
|
|
230
|
+
attrs = {
|
|
231
|
+
name: name.strip,
|
|
232
|
+
value: value.strip,
|
|
233
|
+
domain: origin_host,
|
|
234
|
+
host_only: true,
|
|
235
|
+
path: default_cookie_path(uri.path),
|
|
236
|
+
secure: false,
|
|
237
|
+
http_only: false,
|
|
238
|
+
expires: nil,
|
|
239
|
+
same_site: nil
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
segments.each do |seg|
|
|
243
|
+
attr_name, attr_val = seg.split('=', 2)
|
|
244
|
+
key = attr_name.to_s.strip.downcase
|
|
245
|
+
val = attr_val&.strip
|
|
246
|
+
case key
|
|
247
|
+
when 'domain'
|
|
248
|
+
next if val.nil? || val.empty?
|
|
249
|
+
|
|
250
|
+
normalized = val.downcase.sub(/\A\./, '').chomp('.')
|
|
251
|
+
next unless domain_allowed_for_origin?(normalized, origin_host)
|
|
252
|
+
|
|
253
|
+
attrs[:domain] = normalized
|
|
254
|
+
attrs[:host_only] = false
|
|
255
|
+
when 'path'
|
|
256
|
+
attrs[:path] = val && val.start_with?('/') ? val : '/'
|
|
257
|
+
when 'secure'
|
|
258
|
+
attrs[:secure] = true
|
|
259
|
+
when 'httponly'
|
|
260
|
+
attrs[:http_only] = true
|
|
261
|
+
when 'max-age'
|
|
262
|
+
seconds = val.to_i
|
|
263
|
+
attrs[:expires] = seconds <= 0 ? Time.at(0) : Time.now + seconds
|
|
264
|
+
when 'expires'
|
|
265
|
+
attrs[:expires] = parse_expiry(val)
|
|
266
|
+
when 'samesite'
|
|
267
|
+
attrs[:same_site] = val&.downcase
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
if Security.pinterest_host?(attrs[:domain]) && attrs[:host_only]
|
|
272
|
+
attrs[:domain] = 'pinterest.com'
|
|
273
|
+
attrs[:host_only] = false
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
Cookie.new(**attrs)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def domain_allowed_for_origin?(cookie_domain, origin_host)
|
|
280
|
+
return false if cookie_domain.empty? || !cookie_domain.include?('.') && origin_host != cookie_domain
|
|
281
|
+
return true if origin_host == cookie_domain
|
|
282
|
+
return true if cookie_domain == 'pinterest.com' && Security.pinterest_host?(origin_host)
|
|
283
|
+
|
|
284
|
+
origin_host.end_with?(".#{cookie_domain}")
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
def default_cookie_path(uri_path)
|
|
288
|
+
path = uri_path.to_s
|
|
289
|
+
return '/' if path.empty? || !path.start_with?('/') || path == '/'
|
|
290
|
+
|
|
291
|
+
last_slash = path.rindex('/')
|
|
292
|
+
last_slash.nil? || last_slash.zero? ? '/' : path[0...last_slash]
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def parse_expiry(raw)
|
|
296
|
+
return nil if raw.nil? || raw.empty?
|
|
297
|
+
|
|
298
|
+
Time.httpdate(raw)
|
|
299
|
+
rescue ArgumentError
|
|
300
|
+
begin
|
|
301
|
+
Time.parse(raw)
|
|
302
|
+
rescue ArgumentError
|
|
303
|
+
nil
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
def upsert_unlocked(cookie)
|
|
308
|
+
@entries.reject! do |existing|
|
|
309
|
+
existing.name == cookie.name &&
|
|
310
|
+
existing.domain == cookie.domain &&
|
|
311
|
+
existing.path == cookie.path
|
|
312
|
+
end
|
|
313
|
+
return if cookie.value.empty? || cookie.expired?
|
|
314
|
+
|
|
315
|
+
@entries << cookie
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|