|
|
@@ -1,18 +1,19 @@
|
|
|
import os
|
|
|
+import time
|
|
|
import datetime
|
|
|
import pickle
|
|
|
from googleapiclient.discovery import build
|
|
|
import subprocess
|
|
|
from google.auth.transport.requests import Request
|
|
|
+import signal
|
|
|
+import sys
|
|
|
|
|
|
-# Output directory for Emby
|
|
|
-OUTPUT_DIR = "/media/media/YouTube"
|
|
|
-
|
|
|
-# Days to look back for videos
|
|
|
-DAYS_BACK = 5
|
|
|
-
|
|
|
-# Token file
|
|
|
-TOKEN_FILE = "youtube_token.pickle"
|
|
|
+# Constants
|
|
|
+OUTPUT_DIR = "/media/media/YouTube" # Change this to your Emby folder
|
|
|
+DAYS_BACK = 5 # Videos published in the last 5 days
|
|
|
+DAYS_TO_KEEP = 10 # Delete videos older than 10 days
|
|
|
+TOKEN_FILE = "youtube_token.pickle" # Token file from the OAuth script
|
|
|
+EXIT_TIME = datetime.time(6, 0) # Exit the script at 06:00
|
|
|
|
|
|
def authenticate_with_token():
|
|
|
"""Authenticate using the saved token."""
|
|
|
@@ -69,9 +70,10 @@ def get_recent_videos(youtube, channel_id):
|
|
|
return recent_videos
|
|
|
|
|
|
def download_video(video_id, output_path):
|
|
|
- """Download a YouTube video using yt-dlp."""
|
|
|
+ """Download a YouTube video as mp4 using yt-dlp."""
|
|
|
command = [
|
|
|
"yt-dlp",
|
|
|
+ "-f", "bestvideo+bestaudio[ext=mp4]/best[ext=mp4]/best", # Ensure highest quality as MP4
|
|
|
f"https://www.youtube.com/watch?v={video_id}",
|
|
|
"-o", os.path.join(output_path, "%(title)s.%(ext)s")
|
|
|
]
|
|
|
@@ -91,6 +93,31 @@ def create_nfo_file(video_data, file_path):
|
|
|
with open(file_path, "w", encoding="utf-8") as nfo_file:
|
|
|
nfo_file.write(nfo_content)
|
|
|
|
|
|
+def delete_old_videos(folder, days=10):
|
|
|
+ """Delete video files and corresponding NFO files older than `days`."""
|
|
|
+ now = time.time()
|
|
|
+ cutoff = now - (days * 86400) # Convert days to seconds
|
|
|
+
|
|
|
+ for root, dirs, files in os.walk(folder):
|
|
|
+ for file in files:
|
|
|
+ file_path = os.path.join(root, file)
|
|
|
+ if os.path.isfile(file_path):
|
|
|
+ # Check the file's last modification time
|
|
|
+ if os.path.getmtime(file_path) < cutoff:
|
|
|
+ print(f"Deleting: {file_path}")
|
|
|
+ os.remove(file_path)
|
|
|
+
|
|
|
+def video_exists(output_path, video_id):
|
|
|
+ """Check if a video already exists in the directory."""
|
|
|
+ return os.path.exists(os.path.join(output_path, video_id))
|
|
|
+
|
|
|
+def check_exit_time():
|
|
|
+ """Check if the current time is past the exit time."""
|
|
|
+ now = datetime.datetime.now().time()
|
|
|
+ if now >= EXIT_TIME:
|
|
|
+ print("Current time exceeds exit time. Terminating script.")
|
|
|
+ sys.exit(0)
|
|
|
+
|
|
|
def main():
|
|
|
# Authenticate YouTube API
|
|
|
print("Authenticating with token...")
|
|
|
@@ -98,6 +125,10 @@ def main():
|
|
|
|
|
|
if not os.path.exists(OUTPUT_DIR):
|
|
|
os.makedirs(OUTPUT_DIR)
|
|
|
+
|
|
|
+ # Clean up old videos
|
|
|
+ print(f"Deleting videos older than {DAYS_TO_KEEP} days in {OUTPUT_DIR}...")
|
|
|
+ delete_old_videos(OUTPUT_DIR, days=DAYS_TO_KEEP)
|
|
|
|
|
|
# Fetch subscribed channels
|
|
|
print("Fetching subscribed channels...")
|
|
|
@@ -109,9 +140,17 @@ def main():
|
|
|
videos = get_recent_videos(youtube, channel_id)
|
|
|
|
|
|
for video in videos:
|
|
|
+ # Check the exit time before processing each video
|
|
|
+ check_exit_time()
|
|
|
+
|
|
|
print(f"Processing video: {video['title']}")
|
|
|
- # Create a folder for the video
|
|
|
- video_dir = os.path.join(OUTPUT_DIR, video["title"].replace("/", "_"))
|
|
|
+ video_dir = os.path.join(OUTPUT_DIR, video["video_id"])
|
|
|
+
|
|
|
+ # Check if the video already exists
|
|
|
+ if os.path.exists(video_dir):
|
|
|
+ print(f"Skipping download, already exists: {video['title']} ({video['video_id']})")
|
|
|
+ continue
|
|
|
+
|
|
|
os.makedirs(video_dir, exist_ok=True)
|
|
|
|
|
|
# Download the video
|