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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7398ed88032d99ccf9ec27c2bf853984bf91033d985888eb2a1597eb27019558
|
|
4
|
+
data.tar.gz: 9e7ea146fc43ab2cebb4b909f3ba2b9f1852acfe4bb311b9c801e35a0c387581
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: abdc20702fba9c5349231be78541fbb07607a66f40ef0a1a6bbec1dd7ed63ddc2217fe94a4d4b8c9b13a9290d7a9eb66d428cab407ddb375bbaa8a6f3aaa38df
|
|
7
|
+
data.tar.gz: 5b7adcb2e28adc9a7e35e6399bb730411e030bf636de331f87844754cb2668a720e99f8041f4d0661f70f0acad77b22b54b98cb6a9a41e420c9ddc06e9a4a069
|
data/CHANGELOG.md
CHANGED
|
@@ -3,15 +3,60 @@
|
|
|
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.4.0]
|
|
7
|
+
|
|
8
|
+
### Security
|
|
9
|
+
- Added `PinterestDL::Security` with strict URL/host validation, SSRF protection against private, loopback, link-local, and metadata IP ranges (`SSRFError`), and rejection of embedded URL credentials.
|
|
10
|
+
- Added `PinterestDL::CookieJar` preserving `Domain`, `Path`, `Secure`, `HttpOnly`, and `Expires`/`Max-Age` attributes and strictly preventing cookies from being sent to CDN (`*.pinimg.com`), shortener (`pin.it`), or external hosts.
|
|
11
|
+
- Cross-domain redirects are now validated against trusted hosts (`RedirectError`) and strip sensitive headers (`Cookie`, `Authorization`, `X-CSRFToken`) when crossing security domains.
|
|
12
|
+
- Added `PinterestDL::MediaValidator` to verify HTTP `Content-Type` headers and binary file signatures (JPEG, PNG, GIF, WebP, MP4, WebM) and reject HTML/JSON error pages disguised as media (`InvalidMediaError`).
|
|
13
|
+
- Enforced configurable maximum download file size (`config.max_file_size`, default 500 MB, or `--max-size` on the CLI) with `FileTooLargeError`.
|
|
14
|
+
- Logger output automatically redacts cookies, CSRF tokens, session IDs, and proxy passwords.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
- `PinterestDL::Session` (`PinterestDL.session`) for fully isolated per-client configuration and cookie jars without global state interference.
|
|
18
|
+
- `PinterestDL::Transport` layer with exponential backoff and random jitter (`retry_jitter`, `max_retry_wait`), plus full support for both integer and HTTP-date `Retry-After` headers.
|
|
19
|
+
- `PinterestDL::Paginator` for unified cursor/bookmark pagination across `Search` and `Board`, with duplicate bookmark cycle detection and `Enumerable` support.
|
|
20
|
+
- `PinterestDL::Pin` and `PinterestDL::DownloadReport` models supporting both object-oriented methods and Hash `[]`/`dig` access.
|
|
21
|
+
- Resumable downloads (`Range: bytes=N-` with HTTP `206 Partial Content`) via `resume: true` and `--resume`/`--no-resume`.
|
|
22
|
+
- Download speed throttling (`max_speed:` / `--max-speed`, e.g. `500K`, `2M`), batch URL deduplication (`deduplicate: true`), smart filename collision handling (`collision_strategy: :rename`), and disk space / write-permission error handling (`DiskSpaceError`).
|
|
23
|
+
- Live byte-level `ProgressBar` showing percentage, received/total bytes, download speed, ETA, and media type badge (`[IMAGE]` / `[VIDEO]`), with `--no-progress` and `--non-interactive` flags.
|
|
24
|
+
- Extracted CLI logic into `PinterestDL::CLI` (`lib/pinterest_dl/cli.rb`) with `--format text|json|csv|quiet`, `--json`, `--images-only`, `--videos-only`, and actionable error hints.
|
|
25
|
+
- Expanded error hierarchy: `ForbiddenError` (distinct from `RateLimitError`), `TimeoutError`, `ConnectionError`, `SSLError`, `RedirectError`, `SSRFError`, `InvalidResponseError`, `FileTooLargeError`, `IncompleteDownloadError`, `InvalidMediaError`, and `DiskSpaceError`.
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
- `Extractors::PinData.best_image_url` now ranks image variants by resolution area and bucket priority instead of relying on hash insertion order.
|
|
29
|
+
- Added support for `__NEXT_DATA__`, `window.__INITIAL_STATE__`, `srcset`, and `<video>`/`<source>` fallbacks when extracting media from HTML.
|
|
30
|
+
- Fixed race conditions and thread exception propagation in `Downloader#download_batch`, and verified `Content-Length` to detect truncated downloads (`IncompleteDownloadError`).
|
|
31
|
+
|
|
32
|
+
## [2.3.0]
|
|
33
|
+
|
|
34
|
+
### Added
|
|
35
|
+
- `download_batch` now runs downloads in parallel using a thread pool (default 4 workers, configurable via `config.concurrency` or `-j/--jobs` on the CLI).
|
|
36
|
+
- `cookies` in `PinterestDL.configure` (and `-c/--cookies` on the CLI) now accepts a path to a Netscape `cookies.txt` file in addition to a raw `key=val; ...` cookie string.
|
|
37
|
+
- Added `1200x` and `564x` image quality buckets alongside `originals`, `736x`, `474x`, and `236x`.
|
|
38
|
+
- `overwrite: false` on `download` / `download_batch` and `-s/--skip-existing` on the CLI to skip files that are already on disk when resuming a big board or search dump.
|
|
39
|
+
- HTTP/HTTPS proxy support via `config.proxy` (defaults to `HTTPS_PROXY` / `HTTP_PROXY` env vars) and `--proxy` on the CLI, plus `--timeout` and `--retries` flags.
|
|
40
|
+
- `Client#stream_get` for streaming large video responses straight to disk instead of buffering the whole file in memory.
|
|
41
|
+
|
|
42
|
+
### Fixed
|
|
43
|
+
- `search` and `board` had a hardcoded default of 5 and 10 pages, so passing `-n 200` on the CLI silently stopped around ~125 pins. Page count now scales automatically with `limit` unless `pages:` is passed explicitly.
|
|
44
|
+
- Downloads now write to a temporary `.part` file first and atomically rename on completion, so a dropped connection or `Ctrl-C` mid-download doesn't leave half-written corrupt files behind.
|
|
45
|
+
- `Client` only retried socket-level exceptions (`SocketError`, `Timeout::Error`, etc.) and immediately blew up on HTTP 429 or 502/503/504. Transient HTTP status codes are now retried too, respecting `Retry-After` when Pinterest sends one.
|
|
46
|
+
- `Extractors::Image` missed numbered CDN hosts like `i0.pinimg.com` and `i1.pinimg.com` and didn't rewrite non-standard dimension buckets (e.g. `600x315`) when switching quality.
|
|
47
|
+
- `Client` cache and cookie jar access are now guarded by a mutex so concurrent workers don't race each other, rate-limit throttling uses `CLOCK_MONOTONIC` instead of wall-clock `Time.now`, and the in-memory cache is capped (`max_cache_entries`, default 256) so long-running scripts don't leak memory.
|
|
48
|
+
- Logger and `ProgressBar` output on the CLI now go to `stderr` instead of `stdout`, so piping `pinterest-dl search ... | jq .` doesn't get polluted by warnings or progress bars.
|
|
49
|
+
- `Ctrl-C` (`SIGINT`) in the CLI now exits cleanly with code 130 instead of dumping a Ruby backtrace.
|
|
50
|
+
|
|
6
51
|
## [2.2.0]
|
|
7
52
|
|
|
8
53
|
### Fixed
|
|
9
|
-
- `Board` and `Search`
|
|
10
|
-
- `Search`
|
|
11
|
-
- `PinData` reads `<script data-relay-response="true">` blocks and
|
|
12
|
-
- `Client` normalizes country
|
|
13
|
-
- `get_media` falls back to `PinResource` when
|
|
14
|
-
- CLI
|
|
54
|
+
- `Board` and `Search` API calls were getting rejected on some endpoints because `X-Pinterest-Source-Url` and `X-Pinterest-AppState` headers were missing. Both are sent now, board slugs are matched case-insensitively, duplicate pins across pages are filtered out, `config.quality` is applied to returned URLs, and both fall back to SSR pins embedded in the HTML if the API feed comes back empty.
|
|
55
|
+
- `Search` was hitting the search HTML page on every single pagination step to grab a session; it now establishes the session once per search call and also handles responses where `data` is a flat array instead of `{ results: [...] }`.
|
|
56
|
+
- `PinData` now reads `<script data-relay-response="true">` blocks in addition to `__PWS_*__` tags, and picks the best available image when `orig` isn't present.
|
|
57
|
+
- `Client` normalizes country subdomains (`de.pinterest.com`, `fr.pinterest.com`, etc.) in its cookie jar, guards `Client#cookie` when `@last_url` is nil, and raises `AuthenticationError` on HTTP 401.
|
|
58
|
+
- `get_media` now falls back to `PinResource` when the initial pin HTML doesn't have both media URLs.
|
|
59
|
+
- CLI: wired up `-v/--verbose`, `--version`, the `media` subcommand, and made `-q/--quality` apply globally.
|
|
15
60
|
|
|
16
61
|
## [2.1.5]
|
|
17
62
|
|
|
@@ -102,9 +147,8 @@ This project follows [Semantic Versioning](https://semver.org/).
|
|
|
102
147
|
is now the single source of truth for the gem version.
|
|
103
148
|
|
|
104
149
|
### Fixed
|
|
105
|
-
- The gem no longer prints
|
|
150
|
+
- The gem no longer prints anything on `require`.
|
|
106
151
|
|
|
107
152
|
## [1.0.3]
|
|
108
|
-
|
|
109
153
|
- Initial public release: `get_image_url`, `get_video_url`, `search`,
|
|
110
154
|
`search_json`, `search_images`, `search_videos`.
|
data/README.md
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
|
|
2
1
|
# pinterest-dl
|
|
2
|
+
|
|
3
3
|
**Get direct image/video URLs from Pinterest, search pins, fetch board contents, and download media — as a Ruby library or a CLI.**
|
|
4
4
|
|
|
5
|
-
[](https://rubygems.org/gems/pinterest-dl)
|
|
6
|
-
[](https://rubygems.org/gems/pinterest-dl)
|
|
7
|
-
[](https://github.com/monji024/pinterest-dl/actions/workflows/ci.yml)
|
|
8
|
-
[](LICENSE)
|
|
9
|
-
[](https://rubygems.org/gems/pinterest-dl)
|
|
6
|
+
[](https://rubygems.org/gems/pinterest-dl)
|
|
7
|
+
[](https://github.com/monji024/pinterest-dl/actions/workflows/ci.yml)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://www.ruby-lang.org/)
|
|
11
10
|
|
|
12
|
-
|
|
11
|
+
<img src="https://img.icons8.com/color/96/000000/pinterest--v1.png" width="80">
|
|
13
12
|
|
|
14
|
-
> **Note:** Users in some regions (e.g. Iran) may need a
|
|
15
|
-
> (Yeah, because even in 2026 Pinterest still gives you the middle finger on clean APIs.)
|
|
13
|
+
> **Note:** Users in some regions (e.g. Iran) may need a VPN or proxy (`--proxy`) to reach Pinterest.
|
|
16
14
|
|
|
17
|
-
Pinterest doesn't give you a clean, official API for getting a direct media URL out of a pin, walking a board, or downloading a batch of images. `pinterest-dl` does that: point it at a pin, a search query, or a board, and get back real `i.pinimg.com` / `v.pinimg.com` URLs — or the
|
|
15
|
+
Pinterest doesn't give you a clean, official API for getting a direct media URL out of a pin, walking a board, or downloading a batch of images. `pinterest-dl` does that: point it at a pin, a search query, or a board, and get back real `i.pinimg.com` / `v.pinimg.com` URLs — or save the files straight to disk.
|
|
18
16
|
|
|
19
17
|
## What it does
|
|
20
18
|
|
|
21
|
-
- Pulls the **direct image or video URL** out of a single pin page.
|
|
22
|
-
- **Searches** Pinterest and returns pin metadata (id
|
|
23
|
-
- Fetches all pins from a **board** (or a specific board section).
|
|
24
|
-
- **Downloads**
|
|
25
|
-
-
|
|
26
|
-
- Raises **specific, catchable exceptions** (`NotFoundError`, `RateLimitError`, `
|
|
27
|
-
- Ships a **CLI** (`pinterest-dl`)
|
|
19
|
+
- Pulls the **direct image or video URL** out of a single pin page (including `pin.it` short links and country domains).
|
|
20
|
+
- **Searches** Pinterest and returns pin metadata (`id`, `pin_url`, `title`, `description`, `creator`, `image_url`, `video_url`), with automatic cursor pagination and duplicate detection.
|
|
21
|
+
- Fetches all pins from a **board** (or a specific board section).
|
|
22
|
+
- **Downloads** single files or parallel batches with live progress bars (percentage, bytes, speed, ETA), HTTP `Range` resume, bandwidth throttling, content-type/magic-byte verification, max file size limits, and automatic deduplication.
|
|
23
|
+
- Provides **isolated sessions** (`PinterestDL.session`) alongside global configuration, with domain-scoped `CookieJar` protection, cross-domain redirect guards, SSRF prevention, and automatic log redaction.
|
|
24
|
+
- Raises **specific, catchable exceptions** (`NotFoundError`, `ForbiddenError`, `RateLimitError`, `AuthenticationError`, `TimeoutError`, `SSRFError`, `FileTooLargeError`, `IncompleteDownloadError`, `InvalidMediaError`, ...) instead of returning `nil`.
|
|
25
|
+
- Ships a full-featured **CLI** (`pinterest-dl`) with `text`, `json`, `csv`, and `quiet` output modes.
|
|
28
26
|
|
|
29
27
|
## Installation
|
|
30
28
|
|
|
@@ -42,10 +40,15 @@ gem 'pinterest-dl'
|
|
|
42
40
|
|
|
43
41
|
```ruby
|
|
44
42
|
require 'pinterest-dl'
|
|
43
|
+
|
|
45
44
|
# Direct image URL
|
|
46
|
-
PinterestDL.get_image_url('https://www.pinterest.com/pin/1234567890123456789/')
|
|
45
|
+
PinterestDL.get_image_url('https://www.pinterest.com/pin/1234567890123456789/')
|
|
46
|
+
# => "https://i.pinimg.com/originals/aa/bb/cc/aabbcc...jpg"
|
|
47
|
+
|
|
47
48
|
# Direct video URL (raises PinterestDL::NotFoundError if the pin has no video)
|
|
48
|
-
PinterestDL.get_video_url('https://www.pinterest.com/pin/1234567890123456789/')
|
|
49
|
+
PinterestDL.get_video_url('https://www.pinterest.com/pin/1234567890123456789/')
|
|
50
|
+
# => "https://v.pinimg.com/videos/mc/720p/aa/bb/cc/aabbcc....mp4"
|
|
51
|
+
|
|
49
52
|
# Download it
|
|
50
53
|
PinterestDL.download(
|
|
51
54
|
PinterestDL.get_image_url('https://www.pinterest.com/pin/1234567890123456789/'),
|
|
@@ -55,136 +58,128 @@ PinterestDL.download(
|
|
|
55
58
|
|
|
56
59
|
## Usage
|
|
57
60
|
|
|
61
|
+
### Isolated Sessions
|
|
62
|
+
|
|
63
|
+
Create independent sessions with their own cookies, timeouts, and settings without mutating global state:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
session = PinterestDL.session(
|
|
67
|
+
cookies: File.expand_path('~/cookies.txt'),
|
|
68
|
+
quality: :'1200x',
|
|
69
|
+
concurrency: 6,
|
|
70
|
+
max_speed: '2M'
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
pins = session.board('https://www.pinterest.com/username/board-name/', limit: 50)
|
|
74
|
+
report = session.download_batch(pins, dir: './downloads')
|
|
75
|
+
```
|
|
76
|
+
|
|
58
77
|
### Get image / video URLs
|
|
59
78
|
|
|
60
79
|
```ruby
|
|
61
|
-
url = PinterestDL.get_image_url('https://www.pinterest.com/pin/example/')
|
|
62
|
-
|
|
80
|
+
url = PinterestDL.get_image_url('https://www.pinterest.com/pin/example/')
|
|
81
|
+
|
|
82
|
+
# Pick a specific size bucket (:originals, :"1200x", :"736x", :"564x", :"474x", :"236x")
|
|
83
|
+
url = PinterestDL.get_image_url('https://www.pinterest.com/pin/example/', quality: :"736x")
|
|
84
|
+
|
|
63
85
|
url = PinterestDL.get_video_url('https://www.pinterest.com/pin/example/')
|
|
86
|
+
|
|
64
87
|
# Both at once
|
|
65
|
-
media = PinterestDL.get_media('https://www.pinterest.com/pin/example/')
|
|
88
|
+
media = PinterestDL.get_media('https://www.pinterest.com/pin/example/')
|
|
89
|
+
# => { image_url: "https://i.pinimg.com/...", video_url: nil }
|
|
66
90
|
```
|
|
67
91
|
|
|
68
|
-
### Search
|
|
92
|
+
### Search & Boards
|
|
69
93
|
|
|
70
94
|
```ruby
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
# Fetch more than one page of results (25 pins/page from Pinterest)
|
|
74
|
-
results = PinterestDL.search('nature landscape', limit: 100, pages: 10)
|
|
75
|
-
# Get only image URLs from search results
|
|
76
|
-
images = PinterestDL.search_images('sunset', limit: 5) # => ["https://i.pinimg.com/originals/...", ...]
|
|
77
|
-
# Get only video URLs from search results
|
|
78
|
-
videos = PinterestDL.search_videos('tutorial', limit: 3) # => ["https://v.pinimg.com/videos/...", ...]
|
|
79
|
-
# Get search results as a JSON string
|
|
80
|
-
json_results = PinterestDL.search_json('anime art', limit: 20)
|
|
81
|
-
```
|
|
95
|
+
results = PinterestDL.search('nature landscape', limit: 25)
|
|
96
|
+
video_pins = PinterestDL.search('motion graphics', limit: 20, type: :video)
|
|
82
97
|
|
|
83
|
-
|
|
98
|
+
images = PinterestDL.search_images('sunset', limit: 5)
|
|
99
|
+
videos = PinterestDL.search_videos('tutorial', limit: 3)
|
|
84
100
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
# A specific section within a board
|
|
89
|
-
pins = PinterestDL.board('https://www.pinterest.com/username/board-name/section-slug/', limit: 50)
|
|
101
|
+
# Boards and board sections
|
|
102
|
+
pins = PinterestDL.board('https://www.pinterest.com/username/board-name/', limit: 100)
|
|
103
|
+
section_pins = PinterestDL.board('https://www.pinterest.com/username/board-name/section-slug/', limit: 50)
|
|
90
104
|
```
|
|
91
105
|
|
|
92
106
|
### Downloading
|
|
93
107
|
|
|
94
108
|
```ruby
|
|
95
|
-
# Single file
|
|
96
|
-
PinterestDL.download(
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
bar.update(done)
|
|
104
|
-
end
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### Cookies / authenticated sessions
|
|
109
|
+
# Single file with resume, max size, and speed limit
|
|
110
|
+
PinterestDL.download(
|
|
111
|
+
'https://i.pinimg.com/originals/aa/bb/cc/img.jpg',
|
|
112
|
+
path: 'out/img.jpg',
|
|
113
|
+
resume: true,
|
|
114
|
+
max_size: 100 * 1024 * 1024,
|
|
115
|
+
max_speed: '1.5M'
|
|
116
|
+
)
|
|
108
117
|
|
|
109
|
-
|
|
118
|
+
# Batch download (returns a PinterestDL::DownloadReport, compatible with Hash access)
|
|
119
|
+
urls = PinterestDL.search_images('mountains', limit: 40)
|
|
120
|
+
report = PinterestDL.download_batch(
|
|
121
|
+
urls,
|
|
122
|
+
dir: './downloads',
|
|
123
|
+
concurrency: 6,
|
|
124
|
+
overwrite: false,
|
|
125
|
+
resume: true,
|
|
126
|
+
deduplicate: true,
|
|
127
|
+
collision_strategy: :rename
|
|
128
|
+
)
|
|
110
129
|
|
|
111
|
-
|
|
112
|
-
PinterestDL.configure do |c|
|
|
113
|
-
c.cookies = 'sessionid=...; csrftoken=...' # yeah, steal your own fucking session if you have to
|
|
114
|
-
c.user_agent = 'Mozilla/5.0 ...' # optional override
|
|
115
|
-
end
|
|
130
|
+
puts "Downloaded: #{report.succeeded.size}, Failed: #{report.failed.size}, Bytes: #{report.bytes_downloaded}"
|
|
116
131
|
```
|
|
117
132
|
|
|
118
|
-
###
|
|
133
|
+
### Configuration
|
|
119
134
|
|
|
120
135
|
```ruby
|
|
121
136
|
PinterestDL.configure do |c|
|
|
122
|
-
c.
|
|
123
|
-
c.
|
|
124
|
-
c.
|
|
125
|
-
c.
|
|
126
|
-
c.
|
|
127
|
-
c.
|
|
128
|
-
c.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
# Pinterest throttled us; e.retry_after may hold a hint
|
|
143
|
-
rescue PinterestDL::AuthenticationError
|
|
144
|
-
# the resource needs c.cookies to be set
|
|
145
|
-
rescue PinterestDL::NetworkError => e
|
|
146
|
-
# DNS/timeout/connection-level failure; e.cause_error has the original exception
|
|
147
|
-
rescue PinterestDL::InvalidURLError
|
|
148
|
-
# the given string isn't a Pinterest pin/board URL
|
|
149
|
-
rescue PinterestDL::Error
|
|
150
|
-
# catch-all for anything above
|
|
137
|
+
c.cookies = File.expand_path('~/cookies.txt')
|
|
138
|
+
c.open_timeout = 10
|
|
139
|
+
c.read_timeout = 20
|
|
140
|
+
c.write_timeout = 20
|
|
141
|
+
c.max_retries = 3
|
|
142
|
+
c.retry_wait = 1.0
|
|
143
|
+
c.max_retry_wait = 30.0
|
|
144
|
+
c.retry_jitter = 0.25
|
|
145
|
+
c.rate_limit_interval = 0.5
|
|
146
|
+
c.concurrency = 4
|
|
147
|
+
c.overwrite = true
|
|
148
|
+
c.resume = true
|
|
149
|
+
c.deduplicate = true
|
|
150
|
+
c.collision_strategy = :rename
|
|
151
|
+
c.max_file_size = 500 * 1024 * 1024
|
|
152
|
+
c.max_speed = nil
|
|
153
|
+
c.verify_content_type = true
|
|
154
|
+
c.proxy = nil
|
|
155
|
+
c.quality = :originals
|
|
156
|
+
c.max_cache_entries = 256
|
|
151
157
|
end
|
|
152
158
|
```
|
|
153
159
|
|
|
154
160
|
### CLI
|
|
155
161
|
|
|
156
162
|
```bash
|
|
157
|
-
pinterest-dl image https://www.pinterest.com/pin/example/
|
|
158
163
|
pinterest-dl image https://www.pinterest.com/pin/example/ -o pin.jpg
|
|
159
|
-
pinterest-dl video https://www.pinterest.com/pin/example/ -o pin.mp4
|
|
160
|
-
pinterest-dl
|
|
161
|
-
pinterest-dl search "cyberpunk city" -n
|
|
162
|
-
pinterest-dl board https://www.pinterest.com/username/board-name/ -n
|
|
163
|
-
pinterest-dl board https://www.pinterest.com/username/board-name/ -
|
|
164
|
-
pinterest-dl search "cats" -
|
|
165
|
-
pinterest-dl
|
|
166
|
-
pinterest-dl --help
|
|
164
|
+
pinterest-dl video https://www.pinterest.com/pin/example/ -o pin.mp4 --max-speed 2M
|
|
165
|
+
pinterest-dl media https://www.pinterest.com/pin/example/ --json
|
|
166
|
+
pinterest-dl search "cyberpunk city" -n 50 -j 8 -o ./downloads
|
|
167
|
+
pinterest-dl board https://www.pinterest.com/username/board-name/ -n 100 -j 6 -s -o ./downloads
|
|
168
|
+
pinterest-dl board https://www.pinterest.com/username/board-name/ --videos-only -o ./videos
|
|
169
|
+
pinterest-dl search "cats" --images-only -n 30 --format json
|
|
170
|
+
pinterest-dl board https://www.pinterest.com/username/secret/ -c ~/cookies.txt --no-progress -o ./secret
|
|
167
171
|
```
|
|
168
172
|
|
|
169
|
-
Flags: `-o/--output`, `-n/--limit`, `-t/--type` (`all`, `image`, `video`, `pin`), `-q/--quality`, `-c/--cookies`, `-v/--verbose`, `--version`.
|
|
170
|
-
|
|
171
|
-
|
|
172
173
|
## Development
|
|
173
174
|
|
|
174
175
|
```bash
|
|
175
176
|
bundle install
|
|
176
|
-
bundle exec rake test
|
|
177
|
-
bundle exec rake
|
|
178
|
-
bundle exec rake
|
|
177
|
+
bundle exec rake test # run the test suite
|
|
178
|
+
bundle exec rake coverage # run tests with SimpleCov coverage
|
|
179
|
+
bundle exec rake rubocop # lint
|
|
180
|
+
bundle exec rake # test + rubocop
|
|
179
181
|
```
|
|
180
182
|
|
|
181
|
-
## Links
|
|
182
|
-
|
|
183
|
-
- [RubyGems](https://rubygems.org/gems/pinterest-dl)
|
|
184
|
-
- [GitHub](https://github.com/monji024/pinterest-dl)
|
|
185
|
-
- [Changelog](CHANGELOG.md)
|
|
186
|
-
- [Issue tracker](https://github.com/monji024/pinterest-dl/issues)
|
|
187
|
-
|
|
188
183
|
## License
|
|
189
184
|
|
|
190
|
-
WTFPL
|
|
185
|
+
WTFPL — [monji024](https://github.com/monji024)
|
data/bin/pinterest-dl
CHANGED
|
@@ -2,129 +2,5 @@
|
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
4
|
require_relative '../lib/pinterest-dl'
|
|
5
|
-
require 'optparse'
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
<<~USAGE
|
|
9
|
-
pinterest-dl v#{PinterestDL::VERSION}
|
|
10
|
-
|
|
11
|
-
Usage:
|
|
12
|
-
pinterest-dl image URL [-o FILE]
|
|
13
|
-
pinterest-dl video URL [-o FILE]
|
|
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]
|
|
17
|
-
|
|
18
|
-
Options:
|
|
19
|
-
-o, --output PATH File (image/video) or directory (search/board) to save to
|
|
20
|
-
-n, --limit N Max number of results (search/board), default 25
|
|
21
|
-
-t, --type TYPE What to collect in search/board: all, image, video, pin (default all)
|
|
22
|
-
-q, --quality Q Image quality: originals, 736x, 474x, 236x (default originals)
|
|
23
|
-
-c, --cookies STR Cookie string for authenticated requests
|
|
24
|
-
-v, --verbose Enable debug logging
|
|
25
|
-
--version Print version and exit
|
|
26
|
-
-h, --help Show this help
|
|
27
|
-
USAGE
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
options = { limit: 25, quality: :originals, type: :all, verbose: false }
|
|
31
|
-
parser = OptionParser.new do |opts|
|
|
32
|
-
opts.on('-o PATH', '--output PATH') { |v| options[:output] = v }
|
|
33
|
-
opts.on('-n N', '--limit N', Integer) { |v| options[:limit] = v }
|
|
34
|
-
opts.on('-t T', '--type T') { |v| options[:type] = v.to_sym }
|
|
35
|
-
opts.on('-q Q', '--quality Q') { |v| options[:quality] = v.to_sym }
|
|
36
|
-
opts.on('-c STR', '--cookies STR') { |v| options[:cookies] = v }
|
|
37
|
-
opts.on('-v', '--verbose') { options[:verbose] = true }
|
|
38
|
-
opts.on('--version') do
|
|
39
|
-
puts PinterestDL::VERSION
|
|
40
|
-
exit 0
|
|
41
|
-
end
|
|
42
|
-
opts.on('-h', '--help') do
|
|
43
|
-
puts usage
|
|
44
|
-
exit 0
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
begin
|
|
49
|
-
parser.parse!(ARGV)
|
|
50
|
-
rescue OptionParser::ParseError => e
|
|
51
|
-
warn "pinterest-dl: #{e.message}"
|
|
52
|
-
exit 1
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
command = ARGV.shift
|
|
56
|
-
target = ARGV.shift
|
|
57
|
-
|
|
58
|
-
if command.nil? || target.nil?
|
|
59
|
-
puts usage
|
|
60
|
-
exit(command.nil? ? 0 : 1)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def die(message)
|
|
64
|
-
warn "pinterest-dl: #{message}"
|
|
65
|
-
exit 1
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def handle_pins(pins, options)
|
|
69
|
-
if pins.empty?
|
|
70
|
-
puts 'No matching pins found'
|
|
71
|
-
return
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
if options[:type] == :pin
|
|
75
|
-
links = pins.map { |p| p[:pin_url] }
|
|
76
|
-
if options[:output]
|
|
77
|
-
FileUtils.mkdir_p(options[:output])
|
|
78
|
-
File.write(File.join(options[:output], 'pins.txt'), "#{links.join("\n")}\n")
|
|
79
|
-
puts "Saved #{links.size} pin links to #{options[:output]}/pins.txt"
|
|
80
|
-
else
|
|
81
|
-
puts links
|
|
82
|
-
end
|
|
83
|
-
elsif options[:output]
|
|
84
|
-
urls = pins.filter_map do |p|
|
|
85
|
-
options[:type] == :image ? p[:image_url] : (p[:video_url] || p[:image_url])
|
|
86
|
-
end
|
|
87
|
-
bar = PinterestDL::ProgressBar.new(total: urls.size)
|
|
88
|
-
result = PinterestDL.download_batch(urls, dir: options[:output]) { |done, _total, _| bar.update(done) }
|
|
89
|
-
puts "Downloaded #{result[:succeeded].size}/#{urls.size} files to #{options[:output]}"
|
|
90
|
-
else
|
|
91
|
-
puts JSON.pretty_generate(pins)
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
|
|
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
|
-
|
|
102
|
-
case command
|
|
103
|
-
when 'image'
|
|
104
|
-
url = PinterestDL.get_image_url(target, quality: options[:quality])
|
|
105
|
-
if options[:output]
|
|
106
|
-
PinterestDL.download(url, path: options[:output])
|
|
107
|
-
puts "Saved to #{options[:output]}"
|
|
108
|
-
else
|
|
109
|
-
puts url
|
|
110
|
-
end
|
|
111
|
-
when 'video'
|
|
112
|
-
url = PinterestDL.get_video_url(target)
|
|
113
|
-
if options[:output]
|
|
114
|
-
PinterestDL.download(url, path: options[:output])
|
|
115
|
-
puts "Saved to #{options[:output]}"
|
|
116
|
-
else
|
|
117
|
-
puts url
|
|
118
|
-
end
|
|
119
|
-
when 'media'
|
|
120
|
-
puts JSON.pretty_generate(PinterestDL.get_media(target, quality: options[:quality]))
|
|
121
|
-
when 'search'
|
|
122
|
-
handle_pins(PinterestDL.search(target, limit: options[:limit], type: options[:type]), options)
|
|
123
|
-
when 'board'
|
|
124
|
-
handle_pins(PinterestDL.board(target, limit: options[:limit], type: options[:type]), options)
|
|
125
|
-
else
|
|
126
|
-
die "unknown command '#{command}'\n\n#{usage}"
|
|
127
|
-
end
|
|
128
|
-
rescue PinterestDL::Error, ArgumentError => e
|
|
129
|
-
die e.message
|
|
130
|
-
end
|
|
6
|
+
exit(PinterestDL::CLI.run(ARGV))
|
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,9 @@ 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/cli'
|
|
18
25
|
|
|
19
26
|
module PinterestDL
|
|
20
27
|
end
|