Fixed incorrect variable name and corrected an issue with reading as a list when the argument was a single URL

This commit is contained in:
Thord Johansson 2023-07-26 02:19:33 +02:00
parent 32b12536de
commit 8af3f8372e

15
stasher.py Normal file → Executable file
View File

@ -3,11 +3,12 @@
import os import os
import re import re
import sys import sys
import datetime
import requests import requests
import subprocess import subprocess
from lxml import html from lxml import html
from dotenv import load_dotenv from dotenv import load_dotenv
import argparse import argparse
# Load variables from .env file # Load variables from .env file
@ -65,7 +66,7 @@ def update_stash():
print(f"Update error: {e}") print(f"Update error: {e}")
exit(1) exit(1)
def download_file(file_url, download_dir, ytdlp_prefix): def download_file(file_url, download_dir, ytdlp_format):
extensions = "(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF|mp4|MP4)" extensions = "(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF|mp4|MP4)"
rgx_file = r"^.*\.{0}$".format(extensions) rgx_file = r"^.*\.{0}$".format(extensions)
rgx_filename = r"[A-Za-z0-9_]*\.{0}".format(extensions) rgx_filename = r"[A-Za-z0-9_]*\.{0}".format(extensions)
@ -113,8 +114,8 @@ def download_file(file_url, download_dir, ytdlp_prefix):
return False return False
else: else:
print(STASH_PRINT_PREFIX, file_url) print(STASH_PRINT_PREFIX, file_url)
download_path = os.path.join(download_dir, ytdlp_prefix) download_path = os.path.join(download_dir, ytdlp_format)
command = ['yt-dlp', file_url, '-o', download_path] command = ['yt-dlp', file_url, '-o', download_path, '--restrict-filenames']
try: try:
subprocess.run(command, check=True) subprocess.run(command, check=True)
return True return True
@ -138,7 +139,7 @@ def is_path_or_url(arg): #chatgpt
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Download files or update stash.") parser = argparse.ArgumentParser(description="Download files or update stash.")
parser.add_argument("url_or_path", metavar="URL_or_path", nargs="+", help="URL or file path to download") parser.add_argument("url_or_path", metavar="URL_or_path", nargs="?", help="URL or file path to download")
parser.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files if present") parser.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files if present")
parser.add_argument("-u", "--update", action="store_true", help="Update stash") parser.add_argument("-u", "--update", action="store_true", help="Update stash")
parser.add_argument("-n", "--no-update", action="store_true", help="Do not update stash") parser.add_argument("-n", "--no-update", action="store_true", help="Do not update stash")
@ -155,9 +156,7 @@ if __name__ == "__main__":
url_or_path = args.url_or_path url_or_path = args.url_or_path
valid_args = [] valid_args = [arg for arg in args.url_or_path if is_path_or_url(arg) is not None] if isinstance(args.url_or_path, list) else [args.url_or_path]
if not args.url_or_path is None:
valid_args = [arg for arg in args.url_or_path if is_path_or_url(arg) is not None]
if valid_args is None or len(valid_args) == 0: if valid_args is None or len(valid_args) == 0:
print("Valid URL or file path required") print("Valid URL or file path required")