From d7da49c9229c18ac71bb76e067bc992c00823df9 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Mon, 12 May 2025 02:58:48 +0100 Subject: [PATCH] Add support for multi link download --- downloader.py | 99 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 16 deletions(-) diff --git a/downloader.py b/downloader.py index 4e1c06c..bbe730d 100644 --- a/downloader.py +++ b/downloader.py @@ -3,6 +3,10 @@ import subprocess import json import re import logging +import time +import sys +from typing import List +from urllib.parse import urlparse # Setup logging logging.basicConfig( @@ -11,9 +15,12 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) -DOWNLOAD_DIR = './downloads' +DOWNLOAD_DIR = './' YT_DLP_PATH = './yt-dlp' +# Rate limiting configuration +DOWNLOAD_DELAY = 1 # Delay in seconds between downloads + def download_video(url: str) -> str: """ Downloads a TikTok video using yt-dlp. @@ -48,12 +55,14 @@ def download_video(url: str) -> str: if 'upload_date' in metadata: # Format is typically YYYYMMDD upload_date = metadata['upload_date'] + uploader = metadata['uploader'] + uploader_id = metadata['uploader_id'] # You can reformat it if you want (e.g., YYYY-MM-DD) formatted_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}" # Update the output template to include the date - output_template = os.path.join(DOWNLOAD_DIR, f"{formatted_date}_%(id)s.%(ext)s") - logger.info(f"Using date format: {formatted_date}") + output_template = os.path.join(DOWNLOAD_DIR, f"{formatted_date}_%(id)s_{uploader}_{uploader_id}.%(ext)s") + # logger.info(f"Using date format: {formatted_date}") except Exception as e: logger.error(f"Error parsing metadata: {str(e)}") # Continue with the default template if there's an error @@ -107,7 +116,7 @@ def download_video(url: str) -> str: except Exception as e: logger.error(f"Error finding recent files: {str(e)}") - logger.info(f"Found filename: {filename}") + # logger.info(f"Found filename: {filename}") if not filename or not os.path.exists(filename): logger.error(f"Full yt-dlp output: {full_output}") @@ -115,18 +124,76 @@ def download_video(url: str) -> str: return filename +def is_valid_tiktok_url(url: str) -> bool: + """ + Validates a TikTok URL. + + Args: + url (str): The URL to validate. + + Returns: + bool: True if valid, False otherwise. + """ + parsed = urlparse(url) + return ( + parsed.scheme in ['http', 'https'] and + 'tiktok.com' in parsed.netloc and + '/video/' in parsed.path + ) + +def process_urls(urls: List[str]): + """ + Processes a list of TikTok URLs for downloading. + + Args: + urls (List[str]): List of TikTok video URLs. + """ + valid_urls = [url.strip() for url in urls if is_valid_tiktok_url(url.strip())] + if not valid_urls: + logger.error("No valid TikTok URLs found.") + print("No valid TikTok URLs found.") + return + + for i, url in enumerate(valid_urls, start=1): + logger.info(f"Processing URL {i}/{len(valid_urls)}: {url}") + # print(f"Processing URL {i}/{len(valid_urls)}: {url}") + try: + video_path = download_video(url) + if video_path: + logger.info(f"Downloaded successfully: {video_path}") + # print(f"Downloaded successfully: {video_path}") + else: + logger.error(f"Failed to download: {url}") + # print(f"Failed to download: {url}") + except Exception as e: + logger.error(f"Error processing {url}: {str(e)}") + # print(f"Error processing {url}: {str(e)}") + + # Apply rate limiting + if i < len(valid_urls): + logger.info(f"Waiting {DOWNLOAD_DELAY} seconds before next download...") + time.sleep(DOWNLOAD_DELAY) + if __name__ == '__main__': - import sys - if len(sys.argv) != 2: - print("Usage: python downloader.py ") + if len(sys.argv) < 2: + print("Usage: python downloader.py ") sys.exit(1) - url = sys.argv[1] - try: - video_path = download_video(url) - if video_path: - print(f"Video downloaded successfully: {video_path}") - else: - print("Download failed.") - except Exception as e: - print(f"Error: {str(e)}") \ No newline at end of file + input_arg = sys.argv[1] + + # Check if the input is a file + if os.path.isfile(input_arg): + try: + with open(input_arg, 'r') as f: + urls = f.readlines() + logger.info(f"Processing URLs from file: {input_arg}") + process_urls(urls) + except Exception as e: + logger.error(f"Error reading input file: {str(e)}") + print(f"Error reading input file: {str(e)}") + else: + # Assume it's a single URL + urls = [input_arg] + process_urls(urls) + + \ No newline at end of file