Your script appears to be set up correctly to process URLs from a file, create folders if they don't exist, and download videos using a tool called htv. However, there are several areas where issues could arise:
- URLs Format: Ensure that the
urls.txt file is correctly formatted with folder names and URLs.
- Folder Creation: Ensure the script has necessary permissions to create directories in the
download_path.
- Command Execution: Verify that the
htv command is correctly installed and is in the system's PATH.
- Error Handling: Ensure that any exceptions or subprocess errors provide useful output.
- Current Folder Handling: Check that the script correctly identifies and uses the current folder.
Here's a modified version of the script that adds some debug logging to help identify where the issue might be:
import os
import subprocess
# Paths
script_path = r'V:\app\scripts\htv'
download_path = r'V:\media\htv'
urls_file = os.path.join(script_path, 'urls.txt')
unsupported_file = os.path.join(script_path, 'unsupported.txt')
def download_videos():
# Read the URLs from the file
with open(urls_file, 'r') as file:
lines = file.readlines()
# Prepare for storing results
unsupported_urls = []
current_folder = None
for line in lines:
line = line.strip()
if not line:
continue
if line.startswith('http://') or line.startswith('https://'):
url = line
print(f"Processing URL: {url}")
if current_folder:
folder_name = current_folder.lstrip('-') # Remove the leading dash from folder name
download_folder = os.path.join(download_path, folder_name)
# Create the folder if it doesn't exist
if not os.path.exists(download_folder):
try:
os.makedirs(download_folder)
print(f"Created folder: {download_folder}")
except Exception as e:
print(f"Error creating folder {download_folder}: {e}")
unsupported_urls.append(url)
continue
try:
subprocess.run(['htv', download_folder, url], check=True)
print(f"Downloaded {url} to {download_folder}")
except subprocess.CalledProcessError as e:
print(f"Error downloading {url}: {e}")
unsupported_urls.append(url)
else:
print(f"No folder specified for URL: {url}")
unsupported_urls.append(url)
else:
current_folder = line
print(f"Current folder set to: {current_folder}")
# Write the unsupported URLs to a file
with open(unsupported_file, 'w') as file:
for url in unsupported_urls:
file.write(f"{url}\n")
print(f"Unsupported URLs saved to {unsupported_file}")
if __name__ == '__main__':
download_videos()
Possible Issues and Debugging Tips:
-
URLs File Format:
-
Folder Permissions:
- Ensure that the script has the necessary permissions to create directories in
V:\media\htv.
-
htv Command Verification:
- Open a command prompt and try running
htv manually to see if it works as expected. Ensure that it’s installed and accessible via the PATH environment variable.
-
Runtime Errors:
- The modified script prints debug statements for each significant step. These debug prints should help you trace the workflow and identify where it may be failing.
-
Logging Errors:
- Additional logging can help identify permission issues or missing dependencies which can usually ensure that folder creation, command execution, and file writes occur without issues.
Check the script output as it runs and see where it might be encountering issues. The print statements added should give you a clear idea of the flow and where it might be failing.