Files
X4Lo d12483d53c 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.
2026-11-22 03:57:49 +00:00

70 lines
2.9 KiB
Python

"""
Download every new post (and story) from a TikTok profile.
python main.py <username | @username | profile URL | secUid> [options]
For picking individual posts, use the web UI instead: python app.py
"""
import argparse
import logging
import sys
import core
def main(argv=None) -> int:
parser = argparse.ArgumentParser(description="Download new posts from a TikTok profile.")
parser.add_argument("profile", help="username, @username, profile URL, or secUid (MS4wLjABAAAA...)")
parser.add_argument("--cookies", metavar="FILE",
help=f"cookies.txt exported from your browser (default: {core.DEFAULT_COOKIES} if it exists)")
parser.add_argument("--no-cookies", action="store_true", help="don't use any cookies file")
parser.add_argument("--refresh", action="store_true", help="ignore the cached post list and fetch it again")
parser.add_argument("--no-stories", action="store_true", help="skip the profile's stories")
parser.add_argument("--retry-failed", action="store_true", help="only retry posts listed in failed.txt")
parser.add_argument("--limit", type=int, help="download at most N posts this run (newest first)")
parser.add_argument("--delay", type=float, default=core.DOWNLOAD_DELAY, help="seconds between posts")
parser.add_argument("-v", "--verbose", action="store_true", help="show gallery-dl output on errors")
args = parser.parse_args(argv)
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s",
level=logging.DEBUG if args.verbose else logging.INFO)
log = logging.getLogger("profile")
try:
cookies = None if args.no_cookies else core.resolve_cookies(args.cookies)
if cookies:
log.info(f"Using cookies from {cookies}")
listing = core.list_profile(args.profile, cookies, refresh=args.refresh,
include_stories=not args.no_stories)
except core.ProfileError as e:
log.error(str(e))
return 2
username = listing["profile"]["username"]
records = listing["stories"] + listing["posts"]
wanted = ("failed",) if args.retry_failed else ("new", "failed")
todo = [r for r in records if r["status"] in wanted]
if args.limit:
todo = todo[: args.limit]
log.info(f"@{username}: {len(records)} items, "
f"{sum(r['status'] == 'downloaded' for r in records)} already downloaded, {len(todo)} to download")
if not todo:
log.info("Everything is already up to date.")
return 0
job = core.DownloadJob(username, todo, cookies, delay=args.delay, on_event=log.info)
try:
job.run()
except KeyboardInterrupt:
job.stop()
log.warning("Interrupted.")
return 130
log.info(f"Files are in {core.profile_dir(username)}")
return 1 if job.snapshot()["counts"].get("failed") else 0
if __name__ == "__main__":
sys.exit(main())