Обновить test.py
This commit is contained in:
parent
5401209c72
commit
f19c5d0864
14
test.py
14
test.py
@ -18,19 +18,16 @@ log_file = "download_log.txt"
|
|||||||
successful_downloads_file = "successful_downloads.txt"
|
successful_downloads_file = "successful_downloads.txt"
|
||||||
failed_downloads_file = "failed_downloads.txt"
|
failed_downloads_file = "failed_downloads.txt"
|
||||||
|
|
||||||
|
|
||||||
def load_downloaded_urls(file):
|
def load_downloaded_urls(file):
|
||||||
if os.path.exists(file):
|
if os.path.exists(file):
|
||||||
with open(file, "r") as f:
|
with open(file, "r") as f:
|
||||||
return set(line.strip() for line in f if line.strip())
|
return set(line.strip() for line in f if line.strip())
|
||||||
return set()
|
return set()
|
||||||
|
|
||||||
|
|
||||||
def save_downloaded_url(file, url):
|
def save_downloaded_url(file, url):
|
||||||
with open(file, "a") as f:
|
with open(file, "a") as f:
|
||||||
f.write(f"{url}\n")
|
f.write(f"{url}\n")
|
||||||
|
|
||||||
|
|
||||||
def download_video(url):
|
def download_video(url):
|
||||||
if url in successful_downloads:
|
if url in successful_downloads:
|
||||||
print(f"Video {url} already downloaded. Skipping.")
|
print(f"Video {url} already downloaded. Skipping.")
|
||||||
@ -56,7 +53,7 @@ def download_video(url):
|
|||||||
url
|
url
|
||||||
]
|
]
|
||||||
print(f"Start: {' '.join(command)}")
|
print(f"Start: {' '.join(command)}")
|
||||||
subprocess.run(command, check=True, capture_output=True, text=True)
|
result = subprocess.run(command, check=True, capture_output=True, text=True)
|
||||||
end_time = datetime.now()
|
end_time = datetime.now()
|
||||||
log_download(url, start_time, end_time)
|
log_download(url, start_time, end_time)
|
||||||
save_downloaded_url(successful_downloads_file, url)
|
save_downloaded_url(successful_downloads_file, url)
|
||||||
@ -65,13 +62,14 @@ def download_video(url):
|
|||||||
print(f"ERROR VIDEO {url}: {error_message}")
|
print(f"ERROR VIDEO {url}: {error_message}")
|
||||||
if "This video has been removed" in error_message or "Video unavailable" in error_message:
|
if "This video has been removed" in error_message or "Video unavailable" in error_message:
|
||||||
save_downloaded_url(failed_downloads_file, url)
|
save_downloaded_url(failed_downloads_file, url)
|
||||||
|
if "Sign in to confirm you’re not a bot" in error_message:
|
||||||
|
print("Critical error detected: Sign-in required. Stopping execution.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
def log_download(url, start_time, end_time):
|
def log_download(url, start_time, end_time):
|
||||||
with open(log_file, "a") as log:
|
with open(log_file, "a") as log:
|
||||||
log.write(f"{url} Download.\nStart: {start_time}\nEnd: {end_time}\n\n")
|
log.write(f"{url} Download.\nStart: {start_time}\nEnd: {end_time}\n\n")
|
||||||
|
|
||||||
|
|
||||||
if not os.path.exists(url_file):
|
if not os.path.exists(url_file):
|
||||||
print(f"File {url_file} Empty.")
|
print(f"File {url_file} Empty.")
|
||||||
exit(1)
|
exit(1)
|
||||||
@ -82,12 +80,14 @@ failed_downloads = load_downloaded_urls(failed_downloads_file)
|
|||||||
with open(url_file, "r") as file:
|
with open(url_file, "r") as file:
|
||||||
video_urls = [line.strip() for line in file if line.strip()]
|
video_urls = [line.strip() for line in file if line.strip()]
|
||||||
|
|
||||||
|
video_urls = [url for url in video_urls if url not in successful_downloads]
|
||||||
|
|
||||||
os.makedirs(os.path.expanduser("~/Downloads/staging"), exist_ok=True)
|
os.makedirs(os.path.expanduser("~/Downloads/staging"), exist_ok=True)
|
||||||
os.makedirs(os.path.expanduser("~/Downloads/temp"), exist_ok=True)
|
os.makedirs(os.path.expanduser("~/Downloads/temp"), exist_ok=True)
|
||||||
os.makedirs(os.path.expanduser("~/Downloads/cache"), exist_ok=True)
|
os.makedirs(os.path.expanduser("~/Downloads/cache"), exist_ok=True)
|
||||||
|
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
|
with concurrent.futures.ThreadPoolExecutor(max_workers=num_threads) as executor:
|
||||||
futures = [executor.submit(download_video, url) for url in video_urls if url not in successful_downloads]
|
futures = [executor.submit(download_video, url) for url in video_urls]
|
||||||
for future in concurrent.futures.as_completed(futures):
|
for future in concurrent.futures.as_completed(futures):
|
||||||
try:
|
try:
|
||||||
future.result()
|
future.result()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user