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