Add support for multi link download
This commit is contained in:
1 parent
1cd6c34e89
commit
d7da49c922
1 file changed
+83
-16
+83
-16
@@ -3,6 +3,10 @@ import subprocess
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
from typing import List
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@@ -11,9 +15,12 @@ logging.basicConfig(
|
|||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOWNLOAD_DIR = './downloads'
|
DOWNLOAD_DIR = './'
|
||||||
YT_DLP_PATH = './yt-dlp'
|
YT_DLP_PATH = './yt-dlp'
|
||||||
|
|
||||||
|
# Rate limiting configuration
|
||||||
|
DOWNLOAD_DELAY = 1 # Delay in seconds between downloads
|
||||||
|
|
||||||
def download_video(url: str) -> str:
|
def download_video(url: str) -> str:
|
||||||
"""
|
"""
|
||||||
Downloads a TikTok video using yt-dlp.
|
Downloads a TikTok video using yt-dlp.
|
||||||
@@ -48,12 +55,14 @@ def download_video(url: str) -> str:
|
|||||||
if 'upload_date' in metadata:
|
if 'upload_date' in metadata:
|
||||||
# Format is typically YYYYMMDD
|
# Format is typically YYYYMMDD
|
||||||
upload_date = metadata['upload_date']
|
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)
|
# 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]}"
|
formatted_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}"
|
||||||
|
|
||||||
# Update the output template to include the date
|
# Update the output template to include the date
|
||||||
output_template = os.path.join(DOWNLOAD_DIR, f"{formatted_date}_%(id)s.%(ext)s")
|
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}")
|
# logger.info(f"Using date format: {formatted_date}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error parsing metadata: {str(e)}")
|
logger.error(f"Error parsing metadata: {str(e)}")
|
||||||
# Continue with the default template if there's an error
|
# Continue with the default template if there's an error
|
||||||
@@ -107,7 +116,7 @@ def download_video(url: str) -> str:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error finding recent files: {str(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):
|
if not filename or not os.path.exists(filename):
|
||||||
logger.error(f"Full yt-dlp output: {full_output}")
|
logger.error(f"Full yt-dlp output: {full_output}")
|
||||||
@@ -115,18 +124,76 @@ def download_video(url: str) -> str:
|
|||||||
|
|
||||||
return filename
|
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__':
|
if __name__ == '__main__':
|
||||||
import sys
|
if len(sys.argv) < 2:
|
||||||
if len(sys.argv) != 2:
|
print("Usage: python downloader.py <url_or_input_file>")
|
||||||
print("Usage: python downloader.py <tiktok_url>")
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
url = sys.argv[1]
|
input_arg = sys.argv[1]
|
||||||
try:
|
|
||||||
video_path = download_video(url)
|
# Check if the input is a file
|
||||||
if video_path:
|
if os.path.isfile(input_arg):
|
||||||
print(f"Video downloaded successfully: {video_path}")
|
try:
|
||||||
else:
|
with open(input_arg, 'r') as f:
|
||||||
print("Download failed.")
|
urls = f.readlines()
|
||||||
except Exception as e:
|
logger.info(f"Processing URLs from file: {input_arg}")
|
||||||
print(f"Error: {str(e)}")
|
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)
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user