Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Nov 29, 2024
1 parent 0b8493a commit 5936896
Showing 1 changed file with 83 additions and 52 deletions.
135 changes: 83 additions & 52 deletions objects/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,101 @@ def log_system_details():
for key, value in os.environ.items():
print(f"{key}: {value}")

def save_metadata(yt, folder):
def save_progress(stream, bytes_remaining, folder):
try:
metadata = {
"title": yt.title if yt.title else "No Title",
"description": yt.description if yt.description else "No Description",
"url": yt.watch_url,
"duration_seconds": yt.length, # Add duration in seconds
"created_date": datetime.now().isoformat() # Track creation time
total_size = stream.filesize
downloaded = total_size - bytes_remaining
progress = {
"total_size": total_size,
"downloaded": downloaded,
"progress": round((downloaded / total_size) * 100, 2)
}
os.makedirs(folder, exist_ok=True)
progress_file_path = os.path.join(folder, "progress.json")
with open(progress_file_path, "w") as progress_file:
json.dump(progress, progress_file, indent=4)
print(f"Progress saved to '{progress_file_path}'.")
except Exception as e:
print(f"Error saving progress: {e}")
raise

def clean_old_folders(base_folder, days=7):
"""Delete folders older than a specified number of days."""
now = datetime.now()
cutoff = now - timedelta(days=days)

for folder in os.listdir(base_folder):
folder_path = os.path.join(base_folder, folder)
if os.path.isdir(folder_path):
metadata_path = os.path.join(folder_path, "metadata.json")
if os.path.exists(metadata_path):
try:
with open(metadata_path, "r") as meta_file:
metadata = json.load(meta_file)
created_date = datetime.fromisoformat(metadata.get("created_date"))
if created_date < cutoff:
print(f"Deleting folder '{folder_path}' (created on {created_date})")
subprocess.call(["rm", "-rf", folder_path])
except Exception as e:
print(f"Error processing folder '{folder_path}': {e}")

def get_metadata_safe(yt):
"""Safely retrieve metadata from YouTube object."""
metadata = {}
try:
metadata["title"] = yt.title if hasattr(yt, "title") and yt.title else "Unknown Title"
except Exception as e:
print(f"Error retrieving title: {e}")
metadata["title"] = "Unknown Title"

try:
metadata["description"] = yt.description if hasattr(yt, "description") and yt.description else "No Description"
except Exception as e:
print(f"Error retrieving description: {e}")
metadata["description"] = "No Description"

try:
metadata["url"] = yt.watch_url if hasattr(yt, "watch_url") else "Unknown URL"
except Exception as e:
print(f"Error retrieving URL: {e}")
metadata["url"] = "Unknown URL"

try:
metadata["duration_seconds"] = yt.length if hasattr(yt, "length") else 0
except Exception as e:
print(f"Error retrieving video length: {e}")
metadata["duration_seconds"] = 0

return metadata


def save_metadata(yt, folder):
"""Save metadata with fallback."""
try:
metadata = get_metadata_safe(yt)
metadata["created_date"] = datetime.now().isoformat() # Track creation time
os.makedirs(folder, exist_ok=True)
metadata_file_path = os.path.join(folder, "metadata.json")
with open(metadata_file_path, "w") as meta_file:
json.dump(metadata, meta_file, indent=4)
print(f"Metadata saved successfully to '{metadata_file_path}'.")
except Exception as e:
print(f"Error saving metadata: {e}")
raise


def save_thumbnail(yt, folder):
"""Save the highest resolution thumbnail available, with fallback handling."""
try:
video_id = yt.video_id
thumbnail_urls = [
f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg", # Highest resolution
f"https://img.youtube.com/vi/{video_id}/sddefault.jpg", # Standard definition
f"https://img.youtube.com/vi/{video_id}/hqdefault.jpg", # High quality
f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg", # Medium quality
yt.thumbnail_url # Default thumbnail
yt.thumbnail_url if hasattr(yt, "thumbnail_url") else None # Default thumbnail
]

thumbnail_urls = [url for url in thumbnail_urls if url] # Remove None entries
thumbnail_path = os.path.join(folder, "thumbs.jpg")
os.makedirs(folder, exist_ok=True)

Expand All @@ -116,56 +182,22 @@ def save_thumbnail(yt, folder):
print(f"Could not download any thumbnails for video '{yt.title}'.")
except Exception as e:
print(f"Error in save_thumbnail: {e}")
raise


def download_video(yt, folder):
"""Download the video at the highest resolution, with fallback."""
try:
video_stream = yt.streams.get_highest_resolution()
if video_stream is None:
print("No streams available to download.")
return
video_path = os.path.join(folder, "video.mp4")
yt.register_on_progress_callback(lambda stream, chunk, bytes_remaining: save_progress(stream, bytes_remaining, folder))
video_stream.download(output_path=folder, filename="video.mp4")
print(f"Video downloaded successfully to '{video_path}'.")
except Exception as e:
print(f"Error downloading video: {e}")
raise

def save_progress(stream, bytes_remaining, folder):
try:
total_size = stream.filesize
downloaded = total_size - bytes_remaining
progress = {
"total_size": total_size,
"downloaded": downloaded,
"progress": round((downloaded / total_size) * 100, 2)
}
os.makedirs(folder, exist_ok=True)
progress_file_path = os.path.join(folder, "progress.json")
with open(progress_file_path, "w") as progress_file:
json.dump(progress, progress_file, indent=4)
print(f"Progress saved to '{progress_file_path}'.")
except Exception as e:
print(f"Error saving progress: {e}")
raise

def clean_old_folders(base_folder, days=7):
"""Delete folders older than a specified number of days."""
now = datetime.now()
cutoff = now - timedelta(days=days)

for folder in os.listdir(base_folder):
folder_path = os.path.join(base_folder, folder)
if os.path.isdir(folder_path):
metadata_path = os.path.join(folder_path, "metadata.json")
if os.path.exists(metadata_path):
try:
with open(metadata_path, "r") as meta_file:
metadata = json.load(meta_file)
created_date = datetime.fromisoformat(metadata.get("created_date"))
if created_date < cutoff:
print(f"Deleting folder '{folder_path}' (created on {created_date})")
subprocess.call(["rm", "-rf", folder_path])
except Exception as e:
print(f"Error processing folder '{folder_path}': {e}")

def main():
if len(sys.argv) < 3:
Expand All @@ -185,9 +217,7 @@ def main():
log_system_details() # Log environment details
print(f"Attempting to access YouTube video: {url}")
yt = YouTube(url)
print(f"Video title: {yt.title}")
print(f"Available streams: {yt.streams}")


if action == "metadata":
save_metadata(yt, folder_name)
elif action == "thumbnail":
Expand All @@ -203,9 +233,10 @@ def main():

clean_old_folders(base_folder)
except Exception as e:
print(f"Error encountered: {e}")
print(f"Error encountered during processing: {e}")
import traceback
traceback.print_exc()


if __name__ == "__main__":
main()

0 comments on commit 5936896

Please sign in to comment.