From 09e00c93317c8b7fce8c872f19c14e644880b99f Mon Sep 17 00:00:00 2001 From: benonymity Date: Sun, 31 Dec 2023 02:37:16 -0500 Subject: [PATCH] fix: build, youtube, lint --- Dockerfile | 2 +- classes.py | 3 ++- requirements.txt | 17 +++++++---------- upload.py | 45 ++++++++++++++++++++++++--------------------- 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Dockerfile b/Dockerfile index cc3e907..f6e539e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ WORKDIR /app COPY . . -RUN pip3 install -r requirements.txt +RUN pip3 install -r requirements.txt --break-system-packages RUN apk del gcc libc-dev libffi-dev diff --git a/classes.py b/classes.py index 662d905..db746a7 100644 --- a/classes.py +++ b/classes.py @@ -297,8 +297,9 @@ def upload(self): # Youtube Upload: if os.path.exists(str(self.video)): if self.youtube: + auth = upload.getAuthenticatedService() video = upload.youtube( - self.video, self.title, self.text, self.speaker, self.date + auth, self.video, self.title, self.text, self.speaker, self.date ) else: video = None diff --git a/requirements.txt b/requirements.txt index b6692e3..0767d4d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,25 +1,22 @@ -beautifulsoup4==4.11.1 -boto3==1.26.59 +boto3==1.20.23 colorama==0.4.4 Flask==2.0.2 Flask_Cors==3.0.10 -Flask_RESTful==0.3.9 GitPython==3.1.27 +GitPython==3.1.40 google_api_python_client==2.51.0 -google_auth_oauthlib==0.5.1 -halo==0.0.31 -httplib2==0.20.2 +google_auth_oauthlib==0.4.6 oauth2client==4.1.3 pdfminer==20191125 +pdfminer.six==20221105 +protobuf==3.19.6 pydub==0.25.1 pyfiglet==0.8.post1 -pyftpdlib==1.5.6 -python-dotenv==0.20.0 +python-dotenv==1.0.0 python_dateutil==2.8.2 -requests==2.26.0 +Requests==2.31.0 sermonaudio==6.10.3 yt_dlp==2023.3.4 -pdfminer.six==20220524 gunicorn==20.1.0 typing_extensions==4.3.0 typing==3.7.4.3 diff --git a/upload.py b/upload.py index 0f05103..14798bf 100644 --- a/upload.py +++ b/upload.py @@ -3,14 +3,15 @@ from time import sleep import boto3 +import pickle import sermonaudio as sapy from dotenv import load_dotenv from git.repo import Repo from git.util import Actor from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials -from google_auth_oauthlib.flow import InstalledAppFlow -from googleapiclient.discovery import build +import google_auth_oauthlib.flow +import googleapiclient.discovery from googleapiclient.http import MediaFileUpload from sermonaudio.broadcaster.requests import Broadcaster from sermonaudio.models import SermonEventType @@ -156,31 +157,33 @@ def sermonaudio(file, title, series, text, speaker, date): log("❌\n") +def getAuthenticatedService(): + scopes = ["https://www.googleapis.com/auth/youtube.upload"] + + client_secrets_file = "data/client_secret.json" + + # Check if token.pickle exists, which stores user's access and refresh tokens + if os.path.exists("data/token.pickle"): + with open("data/token.pickle", "rb") as token: + credentials = pickle.load(token) + else: + # If not, perform OAuth 2.0 authorization flow + flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes) + credentials = flow.run_console() + with open("data/token.pickle", "wb") as token: + pickle.dump(credentials, token) + + # Create the YouTube API client + return googleapiclient.discovery.build("youtube", "v3", credentials=credentials) + + # Youtube upload function: Bit of a mess, but it works so I'm not complaining! This took ages to figure out. -def youtube(file, title, text, speaker, date): +def youtube(auth, file, title, text, speaker, date): log("Uploading to Youtube...") try: response = None error = None retry = 0 - creds = None - # Authentication - scope = ["https://www.googleapis.com/auth/youtube.upload"] - oauth_file = "data/oauth2.json" - if os.path.exists(oauth_file): - creds = Credentials.from_authorized_user_file(oauth_file, scope) - if not creds or not creds.valid: - if creds and creds.expired and creds.refresh_token: - creds.refresh(Request()) - else: - flow = InstalledAppFlow.from_client_secrets_file( - "data/client_secrets.json", scope - ) - creds = flow.run_local_server(port=8080) - # Save the credentials for the next run - with open(oauth_file, "w") as token: - token.write(creds.to_json()) - auth = build("youtube", "v3", credentials=creds) # Pretty date parts = date.split("-") pdate = parts[1].lstrip("0") + "/" + parts[2].lstrip("0") + "/" + parts[0][-2:]