pinterest-dl 2.0.5 → 2.2.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 +20 -2
- data/README.md +1 -1
- data/bin/pinterest-dl +23 -11
- data/lib/pinterest-dl.rb +1 -1
- data/lib/pinterest_dl/api.rb +60 -18
- data/lib/pinterest_dl/board.rb +65 -31
- data/lib/pinterest_dl/client.rb +12 -4
- data/lib/pinterest_dl/configuration.rb +17 -6
- data/lib/pinterest_dl/downloader.rb +4 -3
- data/lib/pinterest_dl/errors.rb +1 -1
- data/lib/pinterest_dl/extractors/image.rb +3 -3
- data/lib/pinterest_dl/extractors/pin_data.rb +81 -10
- 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 +51 -52
- data/lib/pinterest_dl/version.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 471becb9c2f3cb595ad9ebabded3b3cf766b2fd09ca4f7f59b50b171da47e307
|
|
4
|
+
data.tar.gz: 26034c4391ec3ba04f8f712c2ebf7e2b90cf994c599a55eb52c499c49908bfc0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ecb5c3cf457faa56ba8d3f275ff2952b2efed749646c7db2f7486a38c42651c2a300ad0b9eaadb0f165b4fac1adef59f9f0baa29b3155f87eca5e8ae5be35050
|
|
7
|
+
data.tar.gz: 75034d098152e53f0484cb59c17a25bdf6cbe7f10702e6311d1bfe69c9eff62e991522a0e07c2482b2009de7d589027a39006b6691457ddf83f66d2dcb5ef317
|
data/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,17 @@
|
|
|
3
3
|
All notable changes to this project are documented here.
|
|
4
4
|
This project follows [Semantic Versioning](https://semver.org/).
|
|
5
5
|
|
|
6
|
-
## [2.0
|
|
6
|
+
## [2.2.0]
|
|
7
|
+
|
|
8
|
+
### Fixed
|
|
9
|
+
- `Board` and `Search` requests now send `X-Pinterest-Source-Url` and `X-Pinterest-AppState`, match slugs case-insensitively, deduplicate pins, apply `config.quality`, and fall back to SSR HTML pins.
|
|
10
|
+
- `Search` creates a session once per search call instead of once per page and handles array `data` payloads.
|
|
11
|
+
- `PinData` reads `<script data-relay-response="true">` blocks and non-`orig` image buckets.
|
|
12
|
+
- `Client` normalizes country domains in its cookie jar, guards `cookie` when URL is nil, and raises `AuthenticationError` on HTTP 401.
|
|
13
|
+
- `get_media` falls back to `PinResource` when page HTML is incomplete.
|
|
14
|
+
- CLI supports `-v/--verbose`, `--version`, `media`, and applies `-q/--quality` globally.
|
|
15
|
+
|
|
16
|
+
## [2.1.5]
|
|
7
17
|
|
|
8
18
|
### Fixed
|
|
9
19
|
- Pin URLs on country domains (e.g. `de.pinterest.com/pin/...`) didn't download
|
|
@@ -17,6 +27,14 @@ This project follows [Semantic Versioning](https://semver.org/).
|
|
|
17
27
|
- Video pins returned "No video found". The best `.mp4` is picked now (not just
|
|
18
28
|
the first format, which could be an `.m3u8`), story pins are supported, and
|
|
19
29
|
`og:video` meta tags are read in any attribute order.
|
|
30
|
+
- `video` worked only some of the time: Pinterest sometimes serves a lighter
|
|
31
|
+
page without the video data. Now every `__PWS_*__` data block of the page is
|
|
32
|
+
read, then the pin is asked from the `PinResource` API (with the
|
|
33
|
+
`X-Pinterest-PWS-Handler` header Pinterest needs, otherwise it answers 403),
|
|
34
|
+
and the page is fetched fresh up to 3 times before giving up. The "this is an
|
|
35
|
+
image" hint only shows when Pinterest really returned an image pin.
|
|
36
|
+
- `board` reads the board id from the page first and only asks the API when it
|
|
37
|
+
isn't there. Story modules ("Related interests") are no longer treated as pins.
|
|
20
38
|
- `board` never worked: Pinterest needs the real board id, we sent an empty
|
|
21
39
|
one. The id is now looked up first, the session is created once instead of
|
|
22
40
|
once per page, links with `?invite_code=...` are parsed correctly, and
|
|
@@ -89,4 +107,4 @@ This project follows [Semantic Versioning](https://semver.org/).
|
|
|
89
107
|
## [1.0.3]
|
|
90
108
|
|
|
91
109
|
- Initial public release: `get_image_url`, `get_video_url`, `search`,
|
|
92
|
-
`search_json`, `search_images`, `search_videos`.
|
|
110
|
+
`search_json`, `search_images`, `search_videos`.
|
data/README.md
CHANGED
|
@@ -166,7 +166,7 @@ pinterest-dl search "cats" -t pin -n 30
|
|
|
166
166
|
pinterest-dl --help
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
Flags: `-o/--output`, `-n/--limit`, `-t/--type` (`all`, `image`, `video`, `pin`), `-q/--quality`, `-c/--cookies`, `-v/--verbose`.
|
|
169
|
+
Flags: `-o/--output`, `-n/--limit`, `-t/--type` (`all`, `image`, `video`, `pin`), `-q/--quality`, `-c/--cookies`, `-v/--verbose`, `--version`.
|
|
170
170
|
|
|
171
171
|
|
|
172
172
|
## Development
|
data/bin/pinterest-dl
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
-
#
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
3
4
|
require_relative '../lib/pinterest-dl'
|
|
4
5
|
require 'optparse'
|
|
5
6
|
|
|
@@ -10,8 +11,9 @@ def usage
|
|
|
10
11
|
Usage:
|
|
11
12
|
pinterest-dl image URL [-o FILE]
|
|
12
13
|
pinterest-dl video URL [-o FILE]
|
|
13
|
-
pinterest-dl
|
|
14
|
-
pinterest-dl
|
|
14
|
+
pinterest-dl media URL
|
|
15
|
+
pinterest-dl search QUERY [-n LIMIT] [-t TYPE] [-q QUALITY] [-o DIR]
|
|
16
|
+
pinterest-dl board URL [-n LIMIT] [-t TYPE] [-q QUALITY] [-o DIR]
|
|
15
17
|
|
|
16
18
|
Options:
|
|
17
19
|
-o, --output PATH File (image/video) or directory (search/board) to save to
|
|
@@ -19,12 +21,13 @@ def usage
|
|
|
19
21
|
-t, --type TYPE What to collect in search/board: all, image, video, pin (default all)
|
|
20
22
|
-q, --quality Q Image quality: originals, 736x, 474x, 236x (default originals)
|
|
21
23
|
-c, --cookies STR Cookie string for authenticated requests
|
|
22
|
-
-v, --verbose
|
|
24
|
+
-v, --verbose Enable debug logging
|
|
25
|
+
--version Print version and exit
|
|
23
26
|
-h, --help Show this help
|
|
24
27
|
USAGE
|
|
25
28
|
end
|
|
26
29
|
|
|
27
|
-
options = { limit: 25, quality: :originals, type: :all }
|
|
30
|
+
options = { limit: 25, quality: :originals, type: :all, verbose: false }
|
|
28
31
|
parser = OptionParser.new do |opts|
|
|
29
32
|
opts.on('-o PATH', '--output PATH') { |v| options[:output] = v }
|
|
30
33
|
opts.on('-n N', '--limit N', Integer) { |v| options[:limit] = v }
|
|
@@ -32,6 +35,10 @@ parser = OptionParser.new do |opts|
|
|
|
32
35
|
opts.on('-q Q', '--quality Q') { |v| options[:quality] = v.to_sym }
|
|
33
36
|
opts.on('-c STR', '--cookies STR') { |v| options[:cookies] = v }
|
|
34
37
|
opts.on('-v', '--verbose') { options[:verbose] = true }
|
|
38
|
+
opts.on('--version') do
|
|
39
|
+
puts PinterestDL::VERSION
|
|
40
|
+
exit 0
|
|
41
|
+
end
|
|
35
42
|
opts.on('-h', '--help') do
|
|
36
43
|
puts usage
|
|
37
44
|
exit 0
|
|
@@ -53,11 +60,6 @@ if command.nil? || target.nil?
|
|
|
53
60
|
exit(command.nil? ? 0 : 1)
|
|
54
61
|
end
|
|
55
62
|
|
|
56
|
-
PinterestDL.configure do |c|
|
|
57
|
-
c.cookies = options[:cookies] if options[:cookies]
|
|
58
|
-
c.logger.level = Logger::DEBUG if options[:verbose]
|
|
59
|
-
end
|
|
60
|
-
|
|
61
63
|
def die(message)
|
|
62
64
|
warn "pinterest-dl: #{message}"
|
|
63
65
|
exit 1
|
|
@@ -79,7 +81,9 @@ def handle_pins(pins, options)
|
|
|
79
81
|
puts links
|
|
80
82
|
end
|
|
81
83
|
elsif options[:output]
|
|
82
|
-
urls = pins.filter_map
|
|
84
|
+
urls = pins.filter_map do |p|
|
|
85
|
+
options[:type] == :image ? p[:image_url] : (p[:video_url] || p[:image_url])
|
|
86
|
+
end
|
|
83
87
|
bar = PinterestDL::ProgressBar.new(total: urls.size)
|
|
84
88
|
result = PinterestDL.download_batch(urls, dir: options[:output]) { |done, _total, _| bar.update(done) }
|
|
85
89
|
puts "Downloaded #{result[:succeeded].size}/#{urls.size} files to #{options[:output]}"
|
|
@@ -89,6 +93,12 @@ def handle_pins(pins, options)
|
|
|
89
93
|
end
|
|
90
94
|
|
|
91
95
|
begin
|
|
96
|
+
PinterestDL.configure do |c|
|
|
97
|
+
c.cookies = options[:cookies] if options[:cookies]
|
|
98
|
+
c.quality = options[:quality] if options[:quality]
|
|
99
|
+
c.logger.level = Logger::DEBUG if options[:verbose]
|
|
100
|
+
end
|
|
101
|
+
|
|
92
102
|
case command
|
|
93
103
|
when 'image'
|
|
94
104
|
url = PinterestDL.get_image_url(target, quality: options[:quality])
|
|
@@ -106,6 +116,8 @@ begin
|
|
|
106
116
|
else
|
|
107
117
|
puts url
|
|
108
118
|
end
|
|
119
|
+
when 'media'
|
|
120
|
+
puts JSON.pretty_generate(PinterestDL.get_media(target, quality: options[:quality]))
|
|
109
121
|
when 'search'
|
|
110
122
|
handle_pins(PinterestDL.search(target, limit: options[:limit], type: options[:type]), options)
|
|
111
123
|
when 'board'
|
data/lib/pinterest-dl.rb
CHANGED
data/lib/pinterest_dl/api.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Monji024
|
|
4
3
|
module PinterestDL
|
|
5
4
|
PIN_URL_PATTERN = %r{(pinterest\.[a-z.]+/pin/|pin\.it/)}i.freeze
|
|
6
5
|
|
|
@@ -21,28 +20,37 @@ module PinterestDL
|
|
|
21
20
|
def get_image_url(pin_url, quality: config.quality, client: Client.new(config: config))
|
|
22
21
|
html, pin_id = load_pin(pin_url, client)
|
|
23
22
|
url = extract_pin_image(html, pin_id, quality: quality)
|
|
23
|
+
url ||= api_image_url(pin_id, client, quality)
|
|
24
24
|
raise NotFoundError, "No image found at #{pin_url}" unless url
|
|
25
25
|
|
|
26
26
|
url
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def get_video_url(pin_url, client: Client.new(config: config))
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
pin = nil
|
|
31
|
+
3.times do |attempt|
|
|
32
|
+
html, pin_id = load_pin(pin_url, client, fresh: attempt.positive?)
|
|
33
|
+
url = extract_pin_video(html, pin_id)
|
|
34
|
+
return url if url
|
|
35
|
+
|
|
36
|
+
pin = pin_from_api(pin_id, client)
|
|
37
|
+
return pin[:video_url] if pin && pin[:video_url]
|
|
35
38
|
end
|
|
36
39
|
|
|
37
|
-
|
|
40
|
+
hint = pin && pin[:image_url] ? ' (it looks like an image pin, try the image command)' : ''
|
|
41
|
+
raise NotFoundError, "No video found at #{pin_url}#{hint}"
|
|
38
42
|
end
|
|
39
43
|
|
|
40
44
|
def get_media(pin_url, quality: config.quality, client: Client.new(config: config))
|
|
41
45
|
html, pin_id = load_pin(pin_url, client)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
image = extract_pin_image(html, pin_id, quality: quality)
|
|
47
|
+
video = extract_pin_video(html, pin_id)
|
|
48
|
+
if (!image || !video) && pin_id
|
|
49
|
+
api_pin = pin_from_api(pin_id, client)
|
|
50
|
+
image ||= api_pin && api_pin[:image_url] && Extractors::Image.apply_quality(api_pin[:image_url], quality)
|
|
51
|
+
video ||= api_pin && api_pin[:video_url]
|
|
52
|
+
end
|
|
53
|
+
{ image_url: image, video_url: video }
|
|
46
54
|
end
|
|
47
55
|
|
|
48
56
|
def download(url, path:)
|
|
@@ -53,10 +61,10 @@ module PinterestDL
|
|
|
53
61
|
Downloader.new(config: config).download_batch(urls, dir: dir, filename: filename, &progress)
|
|
54
62
|
end
|
|
55
63
|
|
|
56
|
-
def search(query, limit: 25, pages: 5, type: :all)
|
|
64
|
+
def search(query, limit: 25, pages: 5, type: :all, client: Client.new(config: config))
|
|
57
65
|
type = MediaType.parse(type)
|
|
58
66
|
pages = [pages, 15].max unless type == :all
|
|
59
|
-
Search.new(config: config).call(query, limit: limit, pages: pages, type: type)
|
|
67
|
+
Search.new(client: client, config: config).call(query, limit: limit, pages: pages, type: type)
|
|
60
68
|
end
|
|
61
69
|
|
|
62
70
|
def search_json(query, limit: 25)
|
|
@@ -71,18 +79,52 @@ module PinterestDL
|
|
|
71
79
|
search(query, limit: limit, type: :video).filter_map { |p| p[:video_url] }
|
|
72
80
|
end
|
|
73
81
|
|
|
74
|
-
def board(board_url, limit: 50, pages: 10, type: :all)
|
|
82
|
+
def board(board_url, limit: 50, pages: 10, type: :all, client: Client.new(config: config))
|
|
75
83
|
type = MediaType.parse(type)
|
|
76
84
|
pages = [pages, 20].max unless type == :all
|
|
77
|
-
Board.new(config: config).call(board_url, limit: limit, pages: pages, type: type)
|
|
85
|
+
Board.new(client: client, config: config).call(board_url, limit: limit, pages: pages, type: type)
|
|
78
86
|
end
|
|
79
87
|
|
|
80
88
|
private
|
|
81
89
|
|
|
82
|
-
def load_pin(pin_url, client)
|
|
90
|
+
def load_pin(pin_url, client, fresh: false)
|
|
83
91
|
validate_pin_url!(pin_url)
|
|
84
|
-
html = client.get(normalize_pin_url(pin_url))
|
|
85
|
-
|
|
92
|
+
html = client.get(normalize_pin_url(pin_url), use_cache: !fresh)
|
|
93
|
+
id = pin_id_from(client.last_url) || pin_id_from(pin_url) || html.to_s[%r{pinterest\.[a-z.]+/pin/(\d+)}, 1]
|
|
94
|
+
[html, id]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def pin_from_api(pin_id, client)
|
|
98
|
+
return nil unless pin_id
|
|
99
|
+
|
|
100
|
+
data = { options: { id: pin_id, field_set_key: 'detailed' }, context: {} }.to_json
|
|
101
|
+
uri = URI('https://www.pinterest.com/resource/PinResource/get/')
|
|
102
|
+
uri.query = URI.encode_www_form('source_url' => "/pin/#{pin_id}/", 'data' => data)
|
|
103
|
+
headers = {
|
|
104
|
+
'Accept' => 'application/json, text/javascript, */*, q=0.01',
|
|
105
|
+
'X-Requested-With' => 'XMLHttpRequest',
|
|
106
|
+
'X-Pinterest-AppState' => 'active',
|
|
107
|
+
'X-Pinterest-PWS-Handler' => 'www/pin/[id].js',
|
|
108
|
+
'X-Pinterest-Source-Url' => "/pin/#{pin_id}/",
|
|
109
|
+
'X-CSRFToken' => client.cookie('csrftoken') || 'undefined',
|
|
110
|
+
'Referer' => "https://www.pinterest.com/pin/#{pin_id}/"
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
pin = JSON.parse(client.get(uri.to_s, headers: headers, use_cache: false)).dig('resource_response', 'data')
|
|
114
|
+
return nil unless pin.is_a?(Hash)
|
|
115
|
+
|
|
116
|
+
{
|
|
117
|
+
image_url: Extractors::PinData.best_image_url(pin),
|
|
118
|
+
video_url: Extractors::PinData.first_video_url(pin)
|
|
119
|
+
}
|
|
120
|
+
rescue PinterestDL::Error, JSON::ParserError => e
|
|
121
|
+
logger.debug("PinResource failed for pin #{pin_id}: #{e.message}")
|
|
122
|
+
nil
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def api_image_url(pin_id, client, quality)
|
|
126
|
+
url = pin_from_api(pin_id, client)&.dig(:image_url)
|
|
127
|
+
url && Extractors::Image.apply_quality(url, quality)
|
|
86
128
|
end
|
|
87
129
|
|
|
88
130
|
def normalize_pin_url(pin_url)
|
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
|
|
@@ -14,30 +15,45 @@ module PinterestDL
|
|
|
14
15
|
|
|
15
16
|
def call(board_url, limit: 50, pages: 10, type: :all)
|
|
16
17
|
username, slug, section = parse_url(board_url)
|
|
17
|
-
start_session(username, slug)
|
|
18
|
-
board_id = find_board_id(username, slug)
|
|
18
|
+
html = start_session(username, slug)
|
|
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
|
|
28
|
+
|
|
29
|
+
private
|
|
20
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,36 @@ 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}/"
|
|
82
|
+
response.body
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def board_id_from_page(html, slug)
|
|
86
|
+
html.to_s.scan(PinterestDL::Extractors::PinData::SCRIPT_PATTERN).each do |(raw)|
|
|
87
|
+
data = PinterestDL::Extractors::PinData.safe_parse(raw)
|
|
88
|
+
stack = [data]
|
|
89
|
+
until stack.empty?
|
|
90
|
+
node = stack.pop
|
|
91
|
+
case node
|
|
92
|
+
when Hash
|
|
93
|
+
return node['id'].to_s if board_node?(node, slug)
|
|
94
|
+
|
|
95
|
+
stack.concat(node.values)
|
|
96
|
+
when Array
|
|
97
|
+
stack.concat(node)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
html.to_s[/"board_id"\s*:\s*"(\d+)"/, 1]
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def board_node?(node, slug)
|
|
105
|
+
node['type'] == 'board' && node['id'].to_s.match?(/\A\d+\z/) &&
|
|
106
|
+
node['url'].to_s.downcase.include?("/#{slug.to_s.downcase}/")
|
|
64
107
|
end
|
|
65
108
|
|
|
66
109
|
def find_board_id(username, slug)
|
|
@@ -69,10 +112,11 @@ module PinterestDL
|
|
|
69
112
|
id = data.is_a?(Hash) && data['id']
|
|
70
113
|
raise PinterestDL::NotFoundError, "Board not found: #{username}/#{slug} (private boards need --cookies)" unless id
|
|
71
114
|
|
|
72
|
-
id
|
|
115
|
+
id.to_s
|
|
73
116
|
end
|
|
74
117
|
|
|
75
118
|
def feed_request(board_id, section)
|
|
119
|
+
@section = section
|
|
76
120
|
unless section
|
|
77
121
|
options = {
|
|
78
122
|
board_id: board_id,
|
|
@@ -92,7 +136,7 @@ module PinterestDL
|
|
|
92
136
|
def find_section_id(board_id, slug)
|
|
93
137
|
json = call_resource('BoardSectionsResource', board_id: board_id)
|
|
94
138
|
data = json.dig('resource_response', 'data')
|
|
95
|
-
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) }
|
|
96
140
|
raise PinterestDL::NotFoundError, "Section not found: #{slug}" unless found
|
|
97
141
|
|
|
98
142
|
found['id']
|
|
@@ -119,30 +163,20 @@ module PinterestDL
|
|
|
119
163
|
end
|
|
120
164
|
|
|
121
165
|
def api_headers
|
|
166
|
+
handler = @section ? 'www/[username]/[slug]/[section_slug].js' : 'www/[username]/[slug].js'
|
|
122
167
|
{
|
|
123
168
|
'Accept' => 'application/json, text/javascript, */*, q=0.01',
|
|
124
169
|
'X-Requested-With' => 'XMLHttpRequest',
|
|
125
|
-
'X-Pinterest-
|
|
170
|
+
'X-Pinterest-AppState' => 'active',
|
|
171
|
+
'X-Pinterest-Source-Url' => @source_path,
|
|
172
|
+
'X-Pinterest-PWS-Handler' => handler,
|
|
126
173
|
'X-CSRFToken' => @client.cookie('csrftoken', @page_url) || 'undefined',
|
|
127
174
|
'Referer' => @page_url
|
|
128
175
|
}
|
|
129
176
|
end
|
|
130
177
|
|
|
131
178
|
def build_pin(raw)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
id = raw['id']
|
|
135
|
-
return nil unless id
|
|
136
|
-
|
|
137
|
-
{
|
|
138
|
-
id: id,
|
|
139
|
-
pin_url: "https://www.pinterest.com/pin/#{id}/",
|
|
140
|
-
title: raw['title'] || raw['grid_title'],
|
|
141
|
-
description: raw['description'],
|
|
142
|
-
creator: raw.dig('pinner', 'username') || raw.dig('pinner', 'full_name'),
|
|
143
|
-
image_url: raw.dig('images', 'orig', 'url'),
|
|
144
|
-
video_url: PinterestDL::Extractors::PinData.first_video_url(raw)
|
|
145
|
-
}
|
|
179
|
+
PinterestDL::Extractors::PinData.build_pin_hash(raw, quality: @config.quality)
|
|
146
180
|
end
|
|
147
181
|
end
|
|
148
182
|
end
|
data/lib/pinterest_dl/client.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
require 'net/http'
|
|
5
5
|
require 'uri'
|
|
6
6
|
require 'openssl'
|
|
@@ -36,7 +36,10 @@ module PinterestDL
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def cookie(name, url = @last_url)
|
|
39
|
-
|
|
39
|
+
target = url.to_s.empty? ? 'https://www.pinterest.com' : url.to_s
|
|
40
|
+
@cookies[site_key(URI(target))][name]
|
|
41
|
+
rescue URI::InvalidURIError
|
|
42
|
+
nil
|
|
40
43
|
end
|
|
41
44
|
|
|
42
45
|
def clear_cache
|
|
@@ -93,6 +96,8 @@ module PinterestDL
|
|
|
93
96
|
"Rate limited while requesting #{url}",
|
|
94
97
|
retry_after: response['Retry-After']
|
|
95
98
|
)
|
|
99
|
+
when Net::HTTPUnauthorized
|
|
100
|
+
raise PinterestDL::AuthenticationError, "Authentication required for #{url}"
|
|
96
101
|
when Net::HTTPNotFound
|
|
97
102
|
raise PinterestDL::NotFoundError, "Nothing found at #{url}"
|
|
98
103
|
when Net::HTTPForbidden
|
|
@@ -133,12 +138,15 @@ module PinterestDL
|
|
|
133
138
|
name, value = line.split(';').first.to_s.split('=', 2)
|
|
134
139
|
next if name.nil? || value.nil?
|
|
135
140
|
|
|
136
|
-
value.empty? ? jar.delete(name.strip) : jar[name.strip] = value
|
|
141
|
+
value.strip.empty? ? jar.delete(name.strip) : jar[name.strip] = value.strip
|
|
137
142
|
end
|
|
138
143
|
end
|
|
139
144
|
|
|
140
145
|
def site_key(uri)
|
|
141
|
-
uri.host.to_s.
|
|
146
|
+
host = uri.host.to_s.downcase
|
|
147
|
+
return 'pinterest.com' if host.include?('pinterest.')
|
|
148
|
+
|
|
149
|
+
host.split('.').last(2).join('.')
|
|
142
150
|
end
|
|
143
151
|
|
|
144
152
|
def throttle!
|
|
@@ -6,7 +6,7 @@ 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
11
|
VALID_QUALITIES = %i[originals 736x 474x 236x].freeze
|
|
12
12
|
|
|
@@ -17,6 +17,10 @@ module PinterestDL
|
|
|
17
17
|
attr_reader :quality
|
|
18
18
|
|
|
19
19
|
def initialize
|
|
20
|
+
reset!
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def reset!
|
|
20
24
|
@user_agent = DEFAULT_USER_AGENT
|
|
21
25
|
@cookies = nil
|
|
22
26
|
@open_timeout = 10
|
|
@@ -25,10 +29,8 @@ module PinterestDL
|
|
|
25
29
|
@retry_wait = 1.0
|
|
26
30
|
@quality = :originals
|
|
27
31
|
@rate_limit_interval = 0.0
|
|
28
|
-
@logger =
|
|
29
|
-
|
|
30
|
-
log.formatter = proc { |severity, _time, _progname, msg| "[pinterest-dl] #{severity}: #{msg}\n" }
|
|
31
|
-
end
|
|
32
|
+
@logger = default_logger
|
|
33
|
+
self
|
|
32
34
|
end
|
|
33
35
|
|
|
34
36
|
def quality=(value)
|
|
@@ -40,7 +42,16 @@ module PinterestDL
|
|
|
40
42
|
end
|
|
41
43
|
|
|
42
44
|
def authenticated?
|
|
43
|
-
!cookies.nil? && !cookies.to_s.empty?
|
|
45
|
+
!cookies.nil? && !cookies.to_s.strip.empty?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def default_logger
|
|
51
|
+
Logger.new($stdout).tap do |log|
|
|
52
|
+
log.level = Logger::WARN
|
|
53
|
+
log.formatter = proc { |severity, _time, _progname, msg| "[pinterest-dl] #{severity}: #{msg}\n" }
|
|
54
|
+
end
|
|
44
55
|
end
|
|
45
56
|
end
|
|
46
57
|
end
|
|
@@ -10,7 +10,8 @@ module PinterestDL
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def download(url, path:)
|
|
13
|
-
raise ArgumentError, 'url must not be nil/empty' if url.to_s.empty?
|
|
13
|
+
raise ArgumentError, 'url must not be nil/empty' if url.to_s.strip.empty?
|
|
14
|
+
raise ArgumentError, 'path must not be nil/empty' if path.to_s.strip.empty?
|
|
14
15
|
|
|
15
16
|
FileUtils.mkdir_p(File.dirname(path))
|
|
16
17
|
response = @client.raw_get(url)
|
|
@@ -47,8 +48,8 @@ module PinterestDL
|
|
|
47
48
|
private
|
|
48
49
|
|
|
49
50
|
def default_filename(url, index)
|
|
50
|
-
ext = File.extname(URI(url).path)
|
|
51
|
-
ext
|
|
51
|
+
ext = File.extname(URI(url).path.to_s)[/\A\.[A-Za-z0-9]{2,5}\z/]
|
|
52
|
+
ext ||= '.jpg'
|
|
52
53
|
format('%<index>04d%<ext>s', index: index + 1, ext: ext)
|
|
53
54
|
rescue URI::InvalidURIError
|
|
54
55
|
format('%<index>04d.jpg', index: index + 1)
|
data/lib/pinterest_dl/errors.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
module PinterestDL
|
|
5
5
|
module Extractors
|
|
6
6
|
module Image
|
|
@@ -29,14 +29,14 @@ module PinterestDL
|
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def from_raw_pattern(html)
|
|
32
|
-
match = html.match(
|
|
32
|
+
match = html.to_s.match(
|
|
33
33
|
%r{https://i\.pinimg\.com/(?:originals|736x|474x|236x)/[A-Za-z0-9_\-/]+\.(?:jpg|jpeg|png|gif|webp)}i
|
|
34
34
|
)
|
|
35
35
|
match && match[0]
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def apply_quality(url, quality)
|
|
39
|
-
return url unless url.include?('i.pinimg.com')
|
|
39
|
+
return url unless url.to_s.include?('i.pinimg.com')
|
|
40
40
|
|
|
41
41
|
target = QUALITY_PATH[quality.to_sym]
|
|
42
42
|
return url unless target
|
|
@@ -5,25 +5,87 @@ require 'json'
|
|
|
5
5
|
module PinterestDL
|
|
6
6
|
module Extractors
|
|
7
7
|
module PinData
|
|
8
|
-
SCRIPT_PATTERN =
|
|
8
|
+
SCRIPT_PATTERN =
|
|
9
|
+
%r{<script[^>]*(?:id=["']__PWS_[A-Z_]+__["']|data-relay-response=["']true["'])[^>]*>(.*?)</script>}m.freeze
|
|
9
10
|
|
|
10
11
|
module_function
|
|
11
12
|
|
|
12
13
|
def extract(html, pin_id = nil)
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
found = nil
|
|
15
|
+
html.to_s.scan(SCRIPT_PATTERN).each do |(raw)|
|
|
16
|
+
data = safe_parse(raw)
|
|
17
|
+
pin = data && find_pin_node(data, pin_id)
|
|
18
|
+
next unless pin
|
|
19
|
+
|
|
20
|
+
result = build_result(pin)
|
|
21
|
+
return result if result[:video_url]
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
found ||= result
|
|
24
|
+
end
|
|
25
|
+
found
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def extract_pins(html, quality: :originals)
|
|
29
|
+
pins = []
|
|
30
|
+
html.to_s.scan(SCRIPT_PATTERN).each do |(raw)|
|
|
31
|
+
stack = [safe_parse(raw)]
|
|
32
|
+
until stack.empty?
|
|
33
|
+
node = stack.pop
|
|
34
|
+
case node
|
|
35
|
+
when Hash
|
|
36
|
+
pin = build_pin_hash(node, quality: quality)
|
|
37
|
+
pins << pin if pin
|
|
38
|
+
stack.concat(node.values)
|
|
39
|
+
when Array
|
|
40
|
+
stack.concat(node)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
pins
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def build_pin_hash(raw, quality: :originals)
|
|
48
|
+
return nil unless raw.is_a?(Hash)
|
|
49
|
+
return nil if raw['type'] && raw['type'] != 'pin'
|
|
50
|
+
|
|
51
|
+
id = raw['id']
|
|
52
|
+
img = best_image_url(raw)
|
|
53
|
+
video = first_video_url(raw)
|
|
54
|
+
return nil unless id && (img || video)
|
|
18
55
|
|
|
19
56
|
{
|
|
20
|
-
|
|
57
|
+
id: id.to_s,
|
|
58
|
+
pin_url: "https://www.pinterest.com/pin/#{id}/",
|
|
59
|
+
title: raw['title'] || raw['grid_title'],
|
|
60
|
+
description: raw['description'],
|
|
61
|
+
creator: raw.dig('pinner', 'username') || raw.dig('pinner', 'full_name'),
|
|
62
|
+
image_url: img && PinterestDL::Extractors::Image.apply_quality(img, quality),
|
|
63
|
+
video_url: video
|
|
64
|
+
}
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def build_result(pin)
|
|
68
|
+
{
|
|
69
|
+
image_url: best_image_url(pin),
|
|
21
70
|
video_url: first_video_url(pin),
|
|
22
71
|
title: pin['title'] || pin['grid_title'],
|
|
23
|
-
description: pin['description']
|
|
72
|
+
description: pin['description'],
|
|
73
|
+
creator: pin.dig('pinner', 'username') || pin.dig('pinner', 'full_name')
|
|
24
74
|
}
|
|
25
75
|
end
|
|
26
76
|
|
|
77
|
+
def best_image_url(pin)
|
|
78
|
+
return nil unless pin.is_a?(Hash)
|
|
79
|
+
|
|
80
|
+
orig = pin.dig('images', 'orig', 'url')
|
|
81
|
+
return orig if orig
|
|
82
|
+
|
|
83
|
+
images = pin['images']
|
|
84
|
+
return nil unless images.is_a?(Hash)
|
|
85
|
+
|
|
86
|
+
images.values.filter_map { |v| v['url'] if v.is_a?(Hash) }.last
|
|
87
|
+
end
|
|
88
|
+
|
|
27
89
|
def parse_embedded_json(html)
|
|
28
90
|
html.to_s.scan(SCRIPT_PATTERN).each do |(raw)|
|
|
29
91
|
parsed = safe_parse(raw)
|
|
@@ -53,8 +115,7 @@ module PinterestDL
|
|
|
53
115
|
end
|
|
54
116
|
|
|
55
117
|
def pin_like?(node)
|
|
56
|
-
|
|
57
|
-
images.is_a?(Hash) && images.dig('orig', 'url')
|
|
118
|
+
!best_image_url(node).nil?
|
|
58
119
|
end
|
|
59
120
|
|
|
60
121
|
def has_media?(node)
|
|
@@ -62,6 +123,8 @@ module PinterestDL
|
|
|
62
123
|
end
|
|
63
124
|
|
|
64
125
|
def first_video_url(pin)
|
|
126
|
+
return nil unless pin.is_a?(Hash)
|
|
127
|
+
|
|
65
128
|
video_list = pin.dig('videos', 'video_list')
|
|
66
129
|
video_list = deep_find(pin['story_pin_data'], 'video_list') unless video_list.is_a?(Hash)
|
|
67
130
|
best_video(video_list)
|
|
@@ -76,7 +139,15 @@ module PinterestDL
|
|
|
76
139
|
return best['url'] if best
|
|
77
140
|
|
|
78
141
|
hls = formats.find { |v| v['url'].include?('.m3u8') }
|
|
79
|
-
hls && hls['url']
|
|
142
|
+
hls && hls_to_mp4(hls['url'])
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def hls_to_mp4(url)
|
|
146
|
+
if url.include?('/iht/hls/')
|
|
147
|
+
url.sub('/hls/', '/expMp4/').sub(/\.m3u8.*\z/, '_720w.mp4')
|
|
148
|
+
else
|
|
149
|
+
url.sub('/hls/', '/720p/').sub(/\.m3u8.*\z/, '.mp4')
|
|
150
|
+
end
|
|
80
151
|
end
|
|
81
152
|
|
|
82
153
|
def deep_find(node, key)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Monji024
|
|
4
3
|
module PinterestDL
|
|
5
4
|
module Extractors
|
|
6
5
|
module Video
|
|
@@ -22,18 +21,18 @@ module PinterestDL
|
|
|
22
21
|
end
|
|
23
22
|
|
|
24
23
|
def from_content_url(html)
|
|
25
|
-
match = html.match(/"contentUrl"\s*:\s*"([^"]+\.mp4[^"]*)"/)
|
|
24
|
+
match = html.to_s.match(/"contentUrl"\s*:\s*"([^"]+\.mp4[^"]*)"/)
|
|
26
25
|
match && match[1]
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
def from_raw_pattern(html)
|
|
30
|
-
clean = html.gsub('\\u002F', '/').gsub('\\/', '/')
|
|
29
|
+
clean = html.to_s.gsub('\\u002F', '/').gsub('\\/', '/')
|
|
31
30
|
match = clean.match(%r{https://v\d*\.pinimg\.com/videos/[^"'\s\\<>]+?\.mp4})
|
|
32
31
|
match && match[0]
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
def unescape(url)
|
|
36
|
-
url.gsub('\\u002F', '/').gsub('\\', '')
|
|
35
|
+
url.to_s.gsub('\\u002F', '/').gsub('\\', '')
|
|
37
36
|
end
|
|
38
37
|
end
|
|
39
38
|
|
|
@@ -56,13 +55,24 @@ module PinterestDL
|
|
|
56
55
|
|
|
57
56
|
def content_url(html, type)
|
|
58
57
|
html.to_s.scan(%r{<script[^>]*type=["']application/ld\+json["'][^>]*>(.*?)</script>}m).each do |(raw)|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
found = find_type_url(safe_parse(raw), type)
|
|
59
|
+
return found if found
|
|
60
|
+
end
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
64
63
|
|
|
65
|
-
|
|
64
|
+
def find_type_url(node, type)
|
|
65
|
+
stack = [node]
|
|
66
|
+
until stack.empty?
|
|
67
|
+
current = stack.pop
|
|
68
|
+
case current
|
|
69
|
+
when Hash
|
|
70
|
+
return current['contentUrl'] if current['@type'] == type && current['contentUrl']
|
|
71
|
+
|
|
72
|
+
stack.concat(current.values)
|
|
73
|
+
when Array
|
|
74
|
+
stack.concat(current)
|
|
75
|
+
end
|
|
66
76
|
end
|
|
67
77
|
nil
|
|
68
78
|
end
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Monji024
|
|
4
3
|
module PinterestDL
|
|
5
4
|
module MediaType
|
|
6
5
|
TYPES = %i[all image video pin].freeze
|
|
@@ -8,8 +7,8 @@ module PinterestDL
|
|
|
8
7
|
module_function
|
|
9
8
|
|
|
10
9
|
def parse(value)
|
|
11
|
-
|
|
12
|
-
return
|
|
10
|
+
normalized = value.nil? || value.to_s.strip.empty? ? :all : value.to_s.strip.downcase.to_sym
|
|
11
|
+
return normalized if TYPES.include?(normalized)
|
|
13
12
|
|
|
14
13
|
raise ArgumentError, "type must be one of: #{TYPES.join(', ')}"
|
|
15
14
|
end
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# Monji024
|
|
4
3
|
module PinterestDL
|
|
5
4
|
class ProgressBar
|
|
6
5
|
def initialize(total:, width: 30, out: $stdout)
|
|
7
|
-
@total = total.
|
|
6
|
+
@total = total.to_i <= 0 ? 1 : total.to_i
|
|
8
7
|
@width = width
|
|
9
8
|
@out = out
|
|
10
9
|
end
|
|
11
10
|
|
|
12
11
|
def update(done)
|
|
13
|
-
ratio = done.to_f / @total
|
|
12
|
+
ratio = [[done.to_f / @total, 0.0].max, 1.0].min
|
|
14
13
|
filled = (@width * ratio).round
|
|
15
14
|
bar = ('#' * filled) + ('-' * (@width - filled))
|
|
16
|
-
@out.print "\r[#{bar}] #{done}/#{@total} (#{(
|
|
15
|
+
@out.print "\r[#{bar}] #{done}/#{@total} (#{((done.to_f / @total) * 100).round}%)"
|
|
17
16
|
@out.print "\n" if done >= @total
|
|
18
17
|
@out.flush
|
|
19
18
|
end
|
data/lib/pinterest_dl/search.rb
CHANGED
|
@@ -14,30 +14,47 @@ module PinterestDL
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
def call(query, limit: 25, pages: 5, type: :all)
|
|
17
|
+
raise ArgumentError, 'query must not be empty' if query.to_s.strip.empty?
|
|
18
|
+
|
|
19
|
+
session = establish_session(query)
|
|
20
|
+
results = collect_pages(session, query, limit: limit, pages: pages, type: type)
|
|
21
|
+
if results.empty?
|
|
22
|
+
fallback = PinterestDL::Extractors::PinData.extract_pins(session[:html], quality: @config.quality)
|
|
23
|
+
results = PinterestDL::MediaType.filter(fallback, type)
|
|
24
|
+
end
|
|
25
|
+
results.first(limit)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def collect_pages(session, query, limit:, pages:, type:)
|
|
17
31
|
results = []
|
|
32
|
+
seen = {}
|
|
18
33
|
bookmarks = nil
|
|
19
34
|
pages_fetched = 0
|
|
20
35
|
|
|
21
36
|
loop do
|
|
22
|
-
page = fetch_page(query, bookmarks: bookmarks)
|
|
23
|
-
|
|
24
|
-
|
|
37
|
+
page = fetch_page(session, query, bookmarks: bookmarks)
|
|
38
|
+
PinterestDL::MediaType.filter(page[:pins], type).each do |pin|
|
|
39
|
+
next if seen[pin[:id]]
|
|
40
|
+
|
|
41
|
+
seen[pin[:id]] = true
|
|
42
|
+
results << pin
|
|
43
|
+
end
|
|
25
44
|
bookmarks = page[:bookmarks]
|
|
26
45
|
pages_fetched += 1
|
|
27
46
|
|
|
28
|
-
break if results.size >= limit
|
|
29
|
-
break if bookmarks.nil? || bookmarks.empty? || bookmarks == ['-end-']
|
|
30
|
-
break if pages_fetched >= pages
|
|
31
|
-
break if pins.empty?
|
|
47
|
+
break if results.size >= limit || stop_pagination?(bookmarks, pages_fetched, pages, page[:pins])
|
|
32
48
|
end
|
|
33
49
|
|
|
34
|
-
results
|
|
50
|
+
results
|
|
35
51
|
end
|
|
36
52
|
|
|
37
|
-
|
|
53
|
+
def stop_pagination?(bookmarks, pages_fetched, max_pages, pins)
|
|
54
|
+
bookmarks.nil? || bookmarks.empty? || bookmarks == ['-end-'] || pages_fetched >= max_pages || pins.empty?
|
|
55
|
+
end
|
|
38
56
|
|
|
39
|
-
def fetch_page(query, bookmarks: nil)
|
|
40
|
-
session = establish_session(query)
|
|
57
|
+
def fetch_page(session, query, bookmarks: nil)
|
|
41
58
|
body = @client.get(
|
|
42
59
|
build_search_url(session[:source_path], query, bookmarks),
|
|
43
60
|
use_cache: false,
|
|
@@ -50,15 +67,20 @@ module PinterestDL
|
|
|
50
67
|
parse_search_response(json)
|
|
51
68
|
end
|
|
52
69
|
|
|
53
|
-
# Pinterest's search resource requires a session cookie + CSRF token
|
|
54
|
-
# obtained by first loading the human-facing search page.
|
|
55
70
|
def establish_session(query)
|
|
56
71
|
source_path = "/search/pins/?q=#{URI.encode_www_form_component(query)}"
|
|
57
72
|
home_uri = URI("https://www.pinterest.com#{source_path}")
|
|
58
73
|
home_response = @client.raw_get(home_uri.to_s)
|
|
59
74
|
cookies = (home_response.get_fields('set-cookie') || []).map { |c| c.split(';').first }
|
|
75
|
+
csrf = @client.cookie('csrftoken', home_uri.to_s) || csrf_token(cookies)
|
|
60
76
|
|
|
61
|
-
{
|
|
77
|
+
{
|
|
78
|
+
source_path: source_path,
|
|
79
|
+
home_uri: home_uri,
|
|
80
|
+
cookies: cookies,
|
|
81
|
+
csrf: csrf,
|
|
82
|
+
html: home_response.body
|
|
83
|
+
}
|
|
62
84
|
end
|
|
63
85
|
|
|
64
86
|
def csrf_token(cookies)
|
|
@@ -78,58 +100,35 @@ module PinterestDL
|
|
|
78
100
|
|
|
79
101
|
def search_headers(session)
|
|
80
102
|
cookie_header = [@config.cookies, session[:cookies].join('; ')].compact.reject(&:empty?).join('; ')
|
|
81
|
-
|
|
82
|
-
{
|
|
103
|
+
headers = {
|
|
83
104
|
'Accept' => 'application/json, text/javascript, */*, q=0.01',
|
|
84
105
|
'X-Requested-With' => 'XMLHttpRequest',
|
|
106
|
+
'X-Pinterest-AppState' => 'active',
|
|
107
|
+
'X-Pinterest-Source-Url' => session[:source_path],
|
|
85
108
|
'X-Pinterest-PWS-Handler' => 'www/search/[scope].js',
|
|
86
109
|
'X-CSRFToken' => session[:csrf],
|
|
87
|
-
'Referer' => session[:home_uri].to_s
|
|
88
|
-
'Cookie' => cookie_header
|
|
110
|
+
'Referer' => session[:home_uri].to_s
|
|
89
111
|
}
|
|
112
|
+
headers['Cookie'] = cookie_header unless cookie_header.empty?
|
|
113
|
+
headers
|
|
90
114
|
end
|
|
91
115
|
|
|
92
116
|
def parse_search_response(json)
|
|
93
|
-
data_node = json.dig('resource_response', 'data')
|
|
94
|
-
raw_results =
|
|
117
|
+
data_node = json.dig('resource_response', 'data')
|
|
118
|
+
raw_results =
|
|
119
|
+
case data_node
|
|
120
|
+
when Array then data_node
|
|
121
|
+
when Hash then data_node['results'] || []
|
|
122
|
+
else []
|
|
123
|
+
end
|
|
95
124
|
next_bookmarks = json.dig('resource_response', 'bookmark') ||
|
|
96
|
-
|
|
125
|
+
json.dig('resource', 'options', 'bookmarks')
|
|
97
126
|
|
|
98
127
|
{ pins: raw_results.filter_map { |r| build_pin(r) }, bookmarks: Array(next_bookmarks) }
|
|
99
128
|
end
|
|
100
129
|
|
|
101
130
|
def build_pin(raw)
|
|
102
|
-
|
|
103
|
-
return nil unless id
|
|
104
|
-
|
|
105
|
-
{
|
|
106
|
-
id: id,
|
|
107
|
-
pin_url: "https://www.pinterest.com/pin/#{id}/",
|
|
108
|
-
title: raw['title'] || raw['grid_title'],
|
|
109
|
-
description: raw['description'],
|
|
110
|
-
creator: pin_creator(raw),
|
|
111
|
-
image_url: pin_image_url(raw),
|
|
112
|
-
video_url: first_video_url(raw)
|
|
113
|
-
}
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def pin_creator(raw)
|
|
117
|
-
raw.dig('pinner', 'username') || raw.dig('pinner', 'full_name')
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def pin_image_url(raw)
|
|
121
|
-
raw.dig('images', 'orig', 'url') || fallback_image_url(raw)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def fallback_image_url(raw)
|
|
125
|
-
images = raw['images']
|
|
126
|
-
return nil unless images
|
|
127
|
-
|
|
128
|
-
images.values.filter_map { |v| v['url'] if v.is_a?(Hash) }.last
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
def first_video_url(raw)
|
|
132
|
-
PinterestDL::Extractors::PinData.first_video_url(raw)
|
|
131
|
+
PinterestDL::Extractors::PinData.build_pin_hash(raw, quality: @config.quality)
|
|
133
132
|
end
|
|
134
133
|
|
|
135
134
|
def safe_parse(body)
|
data/lib/pinterest_dl/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pinterest-dl
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0
|
|
4
|
+
version: 2.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Monji
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A Ruby toolkit for getting direct image and video URLs from Pinterest
|
|
14
14
|
pins, searching pins, fetching board contents, and downloading media to disk — as
|