|
@@ -1,12 +1,9 @@
|
|
|
import os
|
|
import os
|
|
|
import datetime
|
|
import datetime
|
|
|
-import json
|
|
|
|
|
-from google_auth_oauthlib.flow import InstalledAppFlow
|
|
|
|
|
|
|
+import pickle
|
|
|
from googleapiclient.discovery import build
|
|
from googleapiclient.discovery import build
|
|
|
import subprocess
|
|
import subprocess
|
|
|
-
|
|
|
|
|
-# OAuth 2.0 scopes
|
|
|
|
|
-SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"]
|
|
|
|
|
|
|
+from google.auth.transport.requests import Request
|
|
|
|
|
|
|
|
# Output directory for Emby
|
|
# Output directory for Emby
|
|
|
OUTPUT_DIR = "/media/media/YouTube"
|
|
OUTPUT_DIR = "/media/media/YouTube"
|
|
@@ -14,10 +11,21 @@ OUTPUT_DIR = "/media/media/YouTube"
|
|
|
# Days to look back for videos
|
|
# Days to look back for videos
|
|
|
DAYS_BACK = 5
|
|
DAYS_BACK = 5
|
|
|
|
|
|
|
|
-def authenticate_youtube():
|
|
|
|
|
- """Authenticate the user and return a YouTube client."""
|
|
|
|
|
- flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", SCOPES)
|
|
|
|
|
- creds = flow.run_local_server(port=0)
|
|
|
|
|
|
|
+# Token file
|
|
|
|
|
+TOKEN_FILE = "youtube_token.pickle"
|
|
|
|
|
+
|
|
|
|
|
+def authenticate_with_token():
|
|
|
|
|
+ """Authenticate using the saved token."""
|
|
|
|
|
+ if not os.path.exists(TOKEN_FILE):
|
|
|
|
|
+ raise FileNotFoundError(f"Token file '{TOKEN_FILE}' not found. Please run the token retrieval script first.")
|
|
|
|
|
+
|
|
|
|
|
+ with open(TOKEN_FILE, "rb") as token:
|
|
|
|
|
+ creds = pickle.load(token)
|
|
|
|
|
+
|
|
|
|
|
+ # Refresh the token if needed
|
|
|
|
|
+ if creds and creds.expired and creds.refresh_token:
|
|
|
|
|
+ creds.refresh(Request())
|
|
|
|
|
+
|
|
|
youtube = build("youtube", "v3", credentials=creds)
|
|
youtube = build("youtube", "v3", credentials=creds)
|
|
|
return youtube
|
|
return youtube
|
|
|
|
|
|
|
@@ -85,8 +93,8 @@ def create_nfo_file(video_data, file_path):
|
|
|
|
|
|
|
|
def main():
|
|
def main():
|
|
|
# Authenticate YouTube API
|
|
# Authenticate YouTube API
|
|
|
- print("Authenticating...")
|
|
|
|
|
- youtube = authenticate_youtube()
|
|
|
|
|
|
|
+ print("Authenticating with token...")
|
|
|
|
|
+ youtube = authenticate_with_token()
|
|
|
|
|
|
|
|
if not os.path.exists(OUTPUT_DIR):
|
|
if not os.path.exists(OUTPUT_DIR):
|
|
|
os.makedirs(OUTPUT_DIR)
|
|
os.makedirs(OUTPUT_DIR)
|