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
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'logger'
|
|
4
|
+
require_relative 'errors'
|
|
5
|
+
require_relative 'security'
|
|
6
|
+
require_relative 'cookie_jar'
|
|
4
7
|
|
|
5
8
|
module PinterestDL
|
|
6
9
|
class Configuration
|
|
@@ -8,31 +11,65 @@ module PinterestDL
|
|
|
8
11
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' \
|
|
9
12
|
'(KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
|
|
10
13
|
|
|
11
|
-
VALID_QUALITIES = %i[originals 736x 474x 236x].freeze
|
|
14
|
+
VALID_QUALITIES = %i[originals 1200x 736x 564x 474x 236x].freeze
|
|
15
|
+
DEFAULT_MAX_FILE_SIZE = 500 * 1024 * 1024
|
|
12
16
|
|
|
13
|
-
attr_accessor :
|
|
14
|
-
:max_retries, :retry_wait, :
|
|
15
|
-
:rate_limit_interval
|
|
17
|
+
attr_accessor :user_agent, :open_timeout, :read_timeout, :write_timeout,
|
|
18
|
+
:max_retries, :retry_wait, :max_retry_wait, :retry_jitter,
|
|
19
|
+
:rate_limit_interval, :proxy, :max_cache_entries,
|
|
20
|
+
:overwrite, :max_file_size, :max_speed, :verify_content_type,
|
|
21
|
+
:progress, :resume, :deduplicate, :collision_strategy,
|
|
22
|
+
:allow_local_network, :https_only, :min_free_disk_space
|
|
23
|
+
attr_reader :quality, :cookies, :concurrency, :cookie_jar, :logger
|
|
16
24
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def initialize
|
|
25
|
+
def initialize(**options)
|
|
20
26
|
reset!
|
|
27
|
+
apply_options(options)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize_copy(other)
|
|
31
|
+
super
|
|
32
|
+
@cookie_jar = other.cookie_jar.dup
|
|
33
|
+
@cookies = other.cookies&.dup
|
|
34
|
+
@user_agent = other.user_agent&.dup
|
|
35
|
+
@proxy = other.proxy&.dup
|
|
21
36
|
end
|
|
22
37
|
|
|
23
38
|
def reset!
|
|
24
39
|
@user_agent = DEFAULT_USER_AGENT
|
|
25
40
|
@cookies = nil
|
|
41
|
+
@cookie_jar = CookieJar.new
|
|
26
42
|
@open_timeout = 10
|
|
27
43
|
@read_timeout = 20
|
|
44
|
+
@write_timeout = 20
|
|
28
45
|
@max_retries = 3
|
|
29
46
|
@retry_wait = 1.0
|
|
47
|
+
@max_retry_wait = 30.0
|
|
48
|
+
@retry_jitter = 0.25
|
|
30
49
|
@quality = :originals
|
|
31
50
|
@rate_limit_interval = 0.0
|
|
32
|
-
@
|
|
51
|
+
@proxy = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ENV['HTTP_PROXY'] || ENV.fetch('http_proxy', nil)
|
|
52
|
+
@max_cache_entries = 256
|
|
53
|
+
@concurrency = 4
|
|
54
|
+
@overwrite = true
|
|
55
|
+
@max_file_size = DEFAULT_MAX_FILE_SIZE
|
|
56
|
+
@max_speed = nil
|
|
57
|
+
@verify_content_type = true
|
|
58
|
+
@progress = true
|
|
59
|
+
@resume = true
|
|
60
|
+
@deduplicate = true
|
|
61
|
+
@collision_strategy = :rename
|
|
62
|
+
@allow_local_network = false
|
|
63
|
+
@https_only = false
|
|
64
|
+
@min_free_disk_space = 1024 * 1024
|
|
65
|
+
self.logger = default_logger
|
|
33
66
|
self
|
|
34
67
|
end
|
|
35
68
|
|
|
69
|
+
def merge(**options)
|
|
70
|
+
dup.tap { |copy| copy.send(:apply_options, options) }
|
|
71
|
+
end
|
|
72
|
+
|
|
36
73
|
def quality=(value)
|
|
37
74
|
value = value.to_sym
|
|
38
75
|
valid = VALID_QUALITIES.join(', ')
|
|
@@ -41,17 +78,73 @@ module PinterestDL
|
|
|
41
78
|
@quality = value
|
|
42
79
|
end
|
|
43
80
|
|
|
81
|
+
def cookies=(value)
|
|
82
|
+
@cookie_jar = CookieJar.new
|
|
83
|
+
if value.nil? || value.to_s.strip.empty?
|
|
84
|
+
@cookies = nil
|
|
85
|
+
return
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
raw = value.to_s.strip
|
|
89
|
+
@cookie_jar.load_raw(raw)
|
|
90
|
+
@cookies = @cookie_jar.to_cookie_string
|
|
91
|
+
@cookies = nil if @cookies.empty?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def concurrency=(value)
|
|
95
|
+
num = value.to_i
|
|
96
|
+
raise ArgumentError, 'concurrency must be at least 1' if num < 1
|
|
97
|
+
|
|
98
|
+
@concurrency = num
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def logger=(custom_logger)
|
|
102
|
+
@logger = wrap_redacting_logger(custom_logger || default_logger)
|
|
103
|
+
end
|
|
104
|
+
|
|
44
105
|
def authenticated?
|
|
45
|
-
|
|
106
|
+
!@cookies.nil? && !@cookies.empty?
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def validate!
|
|
110
|
+
raise ConfigurationError, 'open_timeout must be positive' if @open_timeout.to_f <= 0
|
|
111
|
+
raise ConfigurationError, 'read_timeout must be positive' if @read_timeout.to_f <= 0
|
|
112
|
+
raise ConfigurationError, 'max_retries must be >= 0' if @max_retries.to_i.negative?
|
|
113
|
+
raise ConfigurationError, 'max_file_size must be positive' if @max_file_size && @max_file_size.to_i <= 0
|
|
114
|
+
|
|
115
|
+
true
|
|
46
116
|
end
|
|
47
117
|
|
|
48
118
|
private
|
|
49
119
|
|
|
120
|
+
def apply_options(options)
|
|
121
|
+
options.each do |key, value|
|
|
122
|
+
setter = :"#{key}="
|
|
123
|
+
raise ConfigurationError, "Unknown configuration option: #{key}" unless respond_to?(setter)
|
|
124
|
+
|
|
125
|
+
public_send(setter, value)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
50
129
|
def default_logger
|
|
51
|
-
Logger.new($
|
|
130
|
+
Logger.new($stderr).tap do |log|
|
|
52
131
|
log.level = Logger::WARN
|
|
53
|
-
log.formatter = proc { |severity, _time, _progname, msg| "[pinterest-dl] #{severity}: #{msg}\n" }
|
|
54
132
|
end
|
|
55
133
|
end
|
|
134
|
+
|
|
135
|
+
def wrap_redacting_logger(base_logger)
|
|
136
|
+
return base_logger unless base_logger.respond_to?(:formatter=)
|
|
137
|
+
|
|
138
|
+
original_formatter = base_logger.formatter
|
|
139
|
+
base_logger.formatter = proc do |severity, time, progname, msg|
|
|
140
|
+
safe_msg = Security.redact(msg.to_s)
|
|
141
|
+
if original_formatter
|
|
142
|
+
Security.redact(original_formatter.call(severity, time, progname, safe_msg).to_s)
|
|
143
|
+
else
|
|
144
|
+
"[pinterest-dl] #{severity}: #{safe_msg}\n"
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
base_logger
|
|
148
|
+
end
|
|
56
149
|
end
|
|
57
150
|
end
|
|
@@ -0,0 +1,345 @@
|
|
|
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) { |line| load_netscape_line_unlocked(line) }
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def load_netscape_line_unlocked(line)
|
|
123
|
+
stripped = line.strip
|
|
124
|
+
return if stripped.empty?
|
|
125
|
+
|
|
126
|
+
http_only = stripped.start_with?('#HttpOnly_')
|
|
127
|
+
return if stripped.start_with?('#') && !http_only
|
|
128
|
+
|
|
129
|
+
stripped = stripped.sub(/\A#HttpOnly_/, '') if http_only
|
|
130
|
+
fields = stripped.split("\t")
|
|
131
|
+
|
|
132
|
+
if fields.size >= 7
|
|
133
|
+
cookie = build_netscape_cookie(fields, http_only)
|
|
134
|
+
upsert_unlocked(cookie) if cookie
|
|
135
|
+
elsif stripped.include?('=') && !stripped.include?("\t")
|
|
136
|
+
load_cookie_pairs_unlocked(stripped)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def build_netscape_cookie(fields, http_only)
|
|
141
|
+
domain, include_subdomains, path, secure_flag, expiry_raw, name, value = fields
|
|
142
|
+
normalized_domain = domain.sub(/\A\./, '')
|
|
143
|
+
|
|
144
|
+
return unless Security.pinterest_host?(normalized_domain)
|
|
145
|
+
|
|
146
|
+
expiry = expiry_raw.to_i
|
|
147
|
+
Cookie.new(
|
|
148
|
+
name: name.strip,
|
|
149
|
+
value: value.to_s.strip,
|
|
150
|
+
domain: normalized_domain.downcase,
|
|
151
|
+
host_only: include_subdomains.to_s.upcase != 'TRUE' && !domain.start_with?('.'),
|
|
152
|
+
path: path.to_s.empty? ? '/' : path.strip,
|
|
153
|
+
secure: secure_flag.to_s.upcase == 'TRUE',
|
|
154
|
+
http_only: http_only,
|
|
155
|
+
expires: expiry.positive? ? Time.at(expiry) : nil,
|
|
156
|
+
same_site: nil
|
|
157
|
+
)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def load_cookie_pairs_unlocked(raw_cookies)
|
|
161
|
+
raw_cookies.split(';').each do |pair|
|
|
162
|
+
name, value = pair.strip.split('=', 2)
|
|
163
|
+
next if name.nil? || value.nil? || name.empty?
|
|
164
|
+
|
|
165
|
+
upsert_unlocked(
|
|
166
|
+
Cookie.new(
|
|
167
|
+
name: name,
|
|
168
|
+
value: value,
|
|
169
|
+
domain: 'pinterest.com',
|
|
170
|
+
host_only: false,
|
|
171
|
+
path: '/',
|
|
172
|
+
secure: false,
|
|
173
|
+
http_only: false,
|
|
174
|
+
expires: nil,
|
|
175
|
+
same_site: nil
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def header_for(url_or_uri, allow_local: false)
|
|
182
|
+
uri = url_or_uri.is_a?(URI) ? url_or_uri : URI.parse(url_or_uri.to_s)
|
|
183
|
+
return '' unless Security.cookie_eligible_host?(uri.host, allow_local: allow_local)
|
|
184
|
+
|
|
185
|
+
now = Time.now
|
|
186
|
+
matched = @mutex.synchronize do
|
|
187
|
+
@entries.reject! { |c| c.expired?(now) }
|
|
188
|
+
@entries
|
|
189
|
+
.select { |c| c.matches?(uri, now: now) }
|
|
190
|
+
.sort_by { |c| -c.path.to_s.length }
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
seen = {}
|
|
194
|
+
pairs = matched.each_with_object([]) do |cookie, acc|
|
|
195
|
+
next if seen[cookie.name]
|
|
196
|
+
|
|
197
|
+
seen[cookie.name] = true
|
|
198
|
+
acc << "#{cookie.name}=#{cookie.value}"
|
|
199
|
+
end
|
|
200
|
+
pairs.join('; ')
|
|
201
|
+
rescue URI::InvalidURIError
|
|
202
|
+
''
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def value_for(name, url_or_uri = 'https://www.pinterest.com', allow_local: true)
|
|
206
|
+
target = url_or_uri.to_s.strip.empty? ? 'https://www.pinterest.com' : url_or_uri.to_s
|
|
207
|
+
uri = target.is_a?(URI) ? target : URI.parse(target)
|
|
208
|
+
return nil unless Security.cookie_eligible_host?(uri.host, allow_local: allow_local)
|
|
209
|
+
|
|
210
|
+
now = Time.now
|
|
211
|
+
@mutex.synchronize do
|
|
212
|
+
@entries.reject! { |c| c.expired?(now) }
|
|
213
|
+
found = @entries
|
|
214
|
+
.select { |c| c.name == name.to_s && c.matches?(uri, now: now) }
|
|
215
|
+
.max_by { |c| c.path.to_s.length }
|
|
216
|
+
found&.value
|
|
217
|
+
end
|
|
218
|
+
rescue URI::InvalidURIError
|
|
219
|
+
nil
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def to_cookie_string(domain: 'https://www.pinterest.com')
|
|
223
|
+
header_for(domain, allow_local: true)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
private
|
|
227
|
+
|
|
228
|
+
def parse_set_cookie(raw_line, uri)
|
|
229
|
+
segments = raw_line.to_s.split(';').map(&:strip).reject(&:empty?)
|
|
230
|
+
return nil if segments.empty?
|
|
231
|
+
|
|
232
|
+
name, value = segments.shift.split('=', 2)
|
|
233
|
+
return nil if name.nil? || value.nil? || name.strip.empty?
|
|
234
|
+
|
|
235
|
+
origin_host = Security.clean_host(uri.host)
|
|
236
|
+
attrs = default_cookie_attributes(name, value, uri, origin_host)
|
|
237
|
+
state = { max_age_seen: false }
|
|
238
|
+
|
|
239
|
+
segments.each do |segment|
|
|
240
|
+
key, raw_value = segment.split('=', 2)
|
|
241
|
+
apply_cookie_attribute(attrs, key.to_s.strip.downcase, raw_value&.strip, origin_host, state)
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
normalize_pinterest_cookie_domain(attrs)
|
|
245
|
+
Cookie.new(**attrs)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def default_cookie_attributes(name, value, uri, origin_host)
|
|
249
|
+
{
|
|
250
|
+
name: name.strip,
|
|
251
|
+
value: value.strip,
|
|
252
|
+
domain: origin_host,
|
|
253
|
+
host_only: true,
|
|
254
|
+
path: default_cookie_path(uri.path),
|
|
255
|
+
secure: false,
|
|
256
|
+
http_only: false,
|
|
257
|
+
expires: nil,
|
|
258
|
+
same_site: nil
|
|
259
|
+
}
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def apply_cookie_attribute(attrs, key, value, origin_host, state)
|
|
263
|
+
case key
|
|
264
|
+
when 'domain'
|
|
265
|
+
apply_cookie_domain(attrs, value, origin_host)
|
|
266
|
+
when 'path'
|
|
267
|
+
attrs[:path] = value && value.start_with?('/') ? value : '/'
|
|
268
|
+
when 'secure'
|
|
269
|
+
attrs[:secure] = true
|
|
270
|
+
when 'httponly'
|
|
271
|
+
attrs[:http_only] = true
|
|
272
|
+
when 'max-age'
|
|
273
|
+
apply_cookie_max_age(attrs, value, state)
|
|
274
|
+
when 'expires'
|
|
275
|
+
attrs[:expires] = parse_expiry(value) unless state[:max_age_seen]
|
|
276
|
+
when 'samesite'
|
|
277
|
+
attrs[:same_site] = value&.downcase
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def apply_cookie_domain(attrs, value, origin_host)
|
|
282
|
+
return if value.nil? || value.empty?
|
|
283
|
+
|
|
284
|
+
normalized = value.downcase.sub(/\A\./, '').chomp('.')
|
|
285
|
+
return unless domain_allowed_for_origin?(normalized, origin_host)
|
|
286
|
+
|
|
287
|
+
attrs[:domain] = normalized
|
|
288
|
+
attrs[:host_only] = false
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def apply_cookie_max_age(attrs, value, state)
|
|
292
|
+
seconds = Integer(value, 10)
|
|
293
|
+
attrs[:expires] = seconds <= 0 ? Time.at(0) : Time.now + seconds
|
|
294
|
+
state[:max_age_seen] = true
|
|
295
|
+
rescue ArgumentError, TypeError
|
|
296
|
+
nil
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def normalize_pinterest_cookie_domain(attrs)
|
|
300
|
+
return unless Security.pinterest_host?(attrs[:domain]) && attrs[:host_only]
|
|
301
|
+
|
|
302
|
+
attrs[:domain] = 'pinterest.com'
|
|
303
|
+
attrs[:host_only] = false
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def domain_allowed_for_origin?(cookie_domain, origin_host)
|
|
307
|
+
return false if cookie_domain.empty? || (!cookie_domain.include?('.') && origin_host != cookie_domain)
|
|
308
|
+
return true if origin_host == cookie_domain
|
|
309
|
+
return true if cookie_domain == 'pinterest.com' && Security.pinterest_host?(origin_host)
|
|
310
|
+
|
|
311
|
+
origin_host.end_with?(".#{cookie_domain}")
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
def default_cookie_path(uri_path)
|
|
315
|
+
path = uri_path.to_s
|
|
316
|
+
return '/' if path.empty? || !path.start_with?('/') || path == '/'
|
|
317
|
+
|
|
318
|
+
last_slash = path.rindex('/')
|
|
319
|
+
last_slash.nil? || last_slash.zero? ? '/' : path[0...last_slash]
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def parse_expiry(raw)
|
|
323
|
+
return nil if raw.nil? || raw.empty?
|
|
324
|
+
|
|
325
|
+
Time.httpdate(raw)
|
|
326
|
+
rescue ArgumentError
|
|
327
|
+
begin
|
|
328
|
+
Time.parse(raw)
|
|
329
|
+
rescue ArgumentError
|
|
330
|
+
nil
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def upsert_unlocked(cookie)
|
|
335
|
+
@entries.reject! do |existing|
|
|
336
|
+
existing.name == cookie.name &&
|
|
337
|
+
existing.domain == cookie.domain &&
|
|
338
|
+
existing.path == cookie.path
|
|
339
|
+
end
|
|
340
|
+
return if cookie.value.empty? || cookie.expired?
|
|
341
|
+
|
|
342
|
+
@entries << cookie
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
|
|
5
|
+
module PinterestDL
|
|
6
|
+
class PartFile
|
|
7
|
+
attr_reader :path
|
|
8
|
+
|
|
9
|
+
def initialize(destination, resumable: false)
|
|
10
|
+
@path = if resumable
|
|
11
|
+
"#{destination}.part"
|
|
12
|
+
else
|
|
13
|
+
"#{destination}.part.#{Process.pid}.#{Thread.current.object_id}"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def finalize!(destination)
|
|
18
|
+
File.rename(path, destination)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.cleanup(path)
|
|
22
|
+
FileUtils.rm_f(path) if path && File.exist?(path)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|