Created new get_youtube_server that loads and youtube Credentials
This commit is contained in:
1 parent
c4f5018833
commit
ee93c06f7d
3 files changed
+21
-20
No files matched your search
+17
-10
@@ -3,15 +3,14 @@ import json
|
|||||||
|
|
||||||
from google.oauth2.credentials import Credentials
|
from google.oauth2.credentials import Credentials
|
||||||
from google.auth.transport.requests import Request
|
from google.auth.transport.requests import Request
|
||||||
|
from googleapiclient.discovery import build
|
||||||
|
|
||||||
CREDENTIALS = None
|
def get_youtube_service():
|
||||||
|
"""Get the YouTube service using the loaded credentials."""
|
||||||
def load_credentials() -> Credentials:
|
|
||||||
"""Load credentials from secrets.json"""
|
|
||||||
try:
|
try:
|
||||||
with open('secrets.json', 'r') as f:
|
with open('secrets.json', 'r') as f:
|
||||||
creds_data = json.load(f)
|
creds_data = json.load(f)
|
||||||
|
|
||||||
credentials = Credentials(
|
credentials = Credentials(
|
||||||
token=creds_data['token'],
|
token=creds_data['token'],
|
||||||
refresh_token=creds_data['refresh_token'],
|
refresh_token=creds_data['refresh_token'],
|
||||||
@@ -20,17 +19,25 @@ def load_credentials() -> Credentials:
|
|||||||
client_secret=creds_data['client_secret'],
|
client_secret=creds_data['client_secret'],
|
||||||
scopes=creds_data['scopes']
|
scopes=creds_data['scopes']
|
||||||
)
|
)
|
||||||
|
|
||||||
# Refresh token if expired
|
# Refresh token if expired
|
||||||
if credentials.expired:
|
if credentials.expired:
|
||||||
credentials.refresh(Request())
|
credentials.refresh(Request())
|
||||||
|
|
||||||
return credentials
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("Error: secrets.json not found! Please run setup.py first.")
|
print("Error: secrets.json not found! Please run setup.py first.")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error loading credentials: {str(e)}")
|
print(f"Error loading credentials: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
CREDENTIALS = load_credentials()
|
if credentials is None:
|
||||||
|
print("Error: Credentials not loaded. Please check secrets.json.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
youtube_service = build('youtube', 'v3', credentials=credentials)
|
||||||
|
return youtube_service
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating YouTube service: {str(e)}")
|
||||||
|
return None
|
||||||
@@ -97,10 +97,8 @@ def scan_channel_videos_for_tag(youtube: build, uploads_playlist_id: str, target
|
|||||||
|
|
||||||
def find_tags():
|
def find_tags():
|
||||||
|
|
||||||
if not config.CREDENTIALS:
|
|
||||||
return
|
youtube = config.get_youtube_service()
|
||||||
|
|
||||||
youtube = build('youtube', 'v3', credentials=config.CREDENTIALS)
|
|
||||||
|
|
||||||
# 1. Fetch your dynamic uploads playlist pointer
|
# 1. Fetch your dynamic uploads playlist pointer
|
||||||
uploads_id = get_my_uploads_playlist_id(youtube)
|
uploads_id = get_my_uploads_playlist_id(youtube)
|
||||||
|
|||||||
@@ -55,13 +55,9 @@ def upload_video(file_path: str, title: str, category: CategoryId, description:
|
|||||||
print(f"❌ Error: File not found: {file_path}")
|
print(f"❌ Error: File not found: {file_path}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Load credentials
|
|
||||||
if not config.CREDENTIALS:
|
|
||||||
print("❌ Error: YouTube API credentials not configured.")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Create YouTube API client
|
# Create YouTube API client
|
||||||
youtube = build('youtube', 'v3', credentials=config.CREDENTIALS)
|
youtube = config.get_youtube_service()
|
||||||
|
|
||||||
# Configure the status object dynamically
|
# Configure the status object dynamically
|
||||||
status_body = {
|
status_body = {
|
||||||
|
|||||||
Reference in new issue
Block a user