""" Download every new post (and story) from a TikTok profile. python main.py [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())