Skip to content

Instantly share code, notes, and snippets.

@ericreeves
Created January 23, 2024 00:26
Show Gist options
  • Save ericreeves/e59efb960930733a2eb37c432d790645 to your computer and use it in GitHub Desktop.
Save ericreeves/e59efb960930733a2eb37c432d790645 to your computer and use it in GitHub Desktop.
Script to Rename TubeArchivist Downloads Using the TubeArchivist API
import os
import requests
import sys
from datetime import datetime
# Configurable variables
archivist_api_token = os.getenv('ARCHIVIST_API_TOKEN')
archivist_host = os.getenv('ARCHIVIST_HOST')
# Function to sanitize file name
def sanitize_filename(filename):
return filename.replace(':', '').replace('?', '').replace('\'', '').replace('/', '')
# Function to reformat date in title
def reformat_date_in_title(title):
try:
parsed_date = datetime.strptime(title, "%B %d %Y")
return parsed_date.strftime("%Y-%m-%d")
except ValueError:
return title
# Function to get video title and last refresh date from TubeArchivist
def get_video_info_from_tubearchivist(video_id):
url = f"{archivist_host}/api/video/{video_id}"
headers = {'Authorization': f'Token {archivist_api_token}'}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
title = data.get('data', {}).get('title')
published = data.get('data', {}).get('published')
if title and published:
formatted_title = reformat_date_in_title(title)
return formatted_title, published
else:
return None, None
except Exception as e:
print(f"Error fetching data for video ID {video_id}: {e}")
return None, None
# Main function
def main():
if len(sys.argv) != 2:
print("Usage: python script.py <directory>")
sys.exit(1)
directory = sys.argv[1]
for filename in os.listdir(directory):
if filename.endswith('.mp4') and len(filename.split('.')[0]) == 11:
video_id = filename.split('.')[0]
video_title, published_date = get_video_info_from_tubearchivist(video_id)
if video_title and published_date:
base_new_filename = f"{published_date} - {sanitize_filename(video_title)}"
new_mp4_filename = f"{base_new_filename}.mp4"
new_att_filename = f"{base_new_filename}.en.vtt"
# Rename .mp4 file
# os.rename(os.path.join(directory, filename), os.path.join(directory, new_mp4_filename))
print(f'Renamed "{filename}" to "{new_mp4_filename}"')
# Rename .en.vtt file if exists
att_filename = filename.replace('.mp4', '.en.vtt')
if os.path.exists(os.path.join(directory, att_filename)):
# os.rename(os.path.join(directory, att_filename), os.path.join(directory, new_att_filename))
print(f'Renamed "{att_filename}" to "{new_att_filename}"')
else:
print(f'Video title or last refresh date not found for "{filename}"')
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment