Add profile downloader with web UI; optional cookies for downloader

Profile downloader (profile-downloader/), now built on gallery-dl:
- Local web UI (app.py): load a profile by username, URL, post link or
  secUid; stories shown above posts; select all/none/not downloaded,
  shift-click ranges, filters; live download progress with Stop
- CLI (main.py) replaces the hard-coded script: --cookies, --refresh,
  --retry-failed, --limit, --no-stories
- Photo posts save their slide images and music; cover thumbnails are
  no longer saved; filenames include seconds and the post ID so posts
  from the same minute cannot collide; partial files are cleaned up
- Backoff and retry when TikTok blocks requests, stopping after three
  blocked posts in a row; per-profile failed.txt
- Profile IDs are cached and looked up from a post page when the
  profile page is blocked; photo posts download straight from the
  post-list links
- README for the profile downloader

downloader.py:
- Optional --cookies FILE (or TIKDOWN_COOKIES)
- Failed-URL helpers accept a file path so the profile tool reuses them

Ignore cookies.txt and __pycache__ in every folder.
This commit is contained in:
X4Lo committed 2026-11-22 03:57:49 +00:00
1 parent 1c17487ce4
commit d12483d53c
9 files changed
+1436 -15

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
# Profile Downloader
Downloads posts and stories from a TikTok profile — through a local web UI where you pick
exactly which posts you want, or from the command line.
Built on [gallery-dl](https://github.com/mikf/gallery-dl), which (unlike yt-dlp) saves the
actual images of photo posts, not just their background music.
## Setup
```bash
pip install gallery-dl flask requests
```
### Cookies (recommended)
TikTok often refuses profile requests from logged-out visitors. Export your browser's
TikTok cookies to `cookies.txt` in this folder (for example with the "Get cookies.txt LOCALLY"
browser extension, while logged in to tiktok.com). It is picked up automatically; you can also
point to another file, or turn cookies off. `cookies.txt` is git-ignored — never share it, it
logs in as you.
## Web UI
```bash
python app.py
```
Opens <http://127.0.0.1:5050> (use `--port` to change it, `--no-browser` to not open a tab).
The server only listens on your own computer.
1. Enter a **username**, `@username`, profile URL, link to one of the profile's posts, or
**secUid** (`MS4wLjABAAAA…`), then **Load profile**.
2. **Stories** are pinned at the top, followed by all **posts**, newest first. Each card shows
the cover, date, caption, type (video length or number of photos) and whether it's already
downloaded or failed before.
3. Select posts:
- click a card (or press Space/Enter on it) to select or deselect it
- Shift-click to select a range
- **Select all** / **Deselect all** / **Select not downloaded** (these act on the current filter)
- filter by **Videos**, **Photos** or **Not downloaded**
4. **Download selected**. A panel at the bottom shows progress and any failures; **Stop** ends
the run after the current post.
The post list is cached for an hour. Tick **Fetch fresh list** to reload it (e.g. to see new
posts or stories).
## Command line
Downloads everything on the profile that isn't downloaded yet:
```bash
python main.py some.username
python main.py https://www.tiktok.com/@some.username --limit 20
python main.py some.username --retry-failed
```
| Option | Meaning |
| --- | --- |
| `--cookies FILE` | cookies file to use (default: `cookies.txt` here, if present) |
| `--no-cookies` | don't use cookies |
| `--refresh` | ignore the cached post list |
| `--no-stories` | skip stories |
| `--retry-failed` | only retry posts in `failed.txt` |
| `--limit N` | download at most N posts this run, newest first |
| `--delay SECONDS` | pause between posts (default 1.5, plus a random extra) |
| `-v` | show gallery-dl's output when something fails |
## Where files go
```
downloads/profile/<username>/
2026-11-08 16-53-54 7705844899718958356 01.jpg photo post, slide 1
2026-11-08 16-53-54 7705844899718958356 02.jpg photo post, slide 2
2026-11-08 16-53-54 7705844899718958356.mp3 photo post music
2026-08-06 00-57-14 7670716372560727317.mp4 video
.archive.txt IDs of downloaded posts (these are skipped / marked "Downloaded")
failed.txt links of posts that failed; removed again once they succeed
.profile.json the profile's IDs, so it doesn't need to be looked up again
.listing_cache.json cached post list
```
The date is the post's upload time in your local time zone. Cover thumbnails are not saved.
Files downloaded by the old version of this script (`2026-06-11 23-26 0001.image` and so on)
are left as they are, and those posts stay marked as downloaded.
## When TikTok blocks requests
TikTok rate-limits scrapers, usually with "403 Forbidden".
- **Downloads** wait 30 s, 90 s and 180 s and retry. After 3 posts in a row stay blocked, the
run stops so it doesn't burn through the whole list. Wait a while (or refresh your cookies)
and download again; already-downloaded posts are skipped.
- **Photo posts** are downloaded straight from the links in the post list, so they usually still
work while post pages are blocked. Videos need their post page.
- **Loading a profile by username** needs the profile page once. If that is blocked, it tries the
pages of posts you already downloaded, then asks you to paste a **post link** or the
**secUid**. After one successful load, the profile's IDs are remembered and the page isn't
needed again.
To find a secUid: open the profile in your browser, view the page source (Ctrl+U) and search
for `"secUid":"`.
## Settings
Environment variables:
| Variable | Default |
| --- | --- |
| `PROFILE_DOWNLOAD_DIR` | `downloads/profile` in this folder |
| `PROFILE_DELAY` | `1.5` seconds between posts |
| `PROFILE_UI_PORT` | `5050` |
## Files
| File | Purpose |
| --- | --- |
| `app.py` | web UI server (Flask) |
| `static/index.html` | the web page |
| `main.py` | command line |
| `core.py` | listing, downloading, archive, retries — shared by both |
Failed-link bookkeeping is shared with the single-post downloader (`../downloader.py`).