From 33687a1a90e813aa03bb746620caccf95a72545a Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Fri, 2 Mar 2018 19:03:32 +0100 Subject: [PATCH] store youtube credentials in a file to avoid asking creds for each uploads --- .gitignore | 3 ++- lib/yt_upload.py | 21 ++++++++++++++++++++- ptyt_upload.py | 1 - 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5e4c57a..211ce11 100644 --- a/.gitignore +++ b/.gitignore @@ -59,4 +59,5 @@ target/ # Project youtube_secret.json -peertube_secret \ No newline at end of file +peertube_secret +.youtube_credentials.json \ No newline at end of file diff --git a/lib/yt_upload.py b/lib/yt_upload.py index 7d1f2fe..6c9f3a8 100644 --- a/lib/yt_upload.py +++ b/lib/yt_upload.py @@ -8,9 +8,12 @@ import httplib2 import os import random import time +import copy +import json import google.oauth2.credentials import google_auth_oauthlib.flow + from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaFileUpload @@ -34,6 +37,7 @@ RETRIABLE_STATUS_CODES = [500, 502, 503, 504] CLIENT_SECRETS_FILE = 'youtube_secret.json' +CREDENTIALS_PATH = ".youtube_credentials.json" SCOPES = ['https://www.googleapis.com/auth/youtube.upload'] API_SERVICE_NAME = 'youtube' API_VERSION = 'v3' @@ -42,7 +46,22 @@ API_VERSION = 'v3' # Authorize the request and store authorization credentials. def get_authenticated_service(): flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) - credentials = flow.run_console() + if os.path.exists(CREDENTIALS_PATH): + with open(CREDENTIALS_PATH, 'r') as f: + credential_params = json.load(f) + credentials = google.oauth2.credentials.Credentials( + credential_params["token"], + refresh_token=credential_params["_refresh_token"], + token_uri=credential_params["_token_uri"], + client_id=credential_params["_client_id"], + client_secret=credential_params["_client_secret"] + ) + else: + credentials = flow.run_local_server() + with open(CREDENTIALS_PATH, 'w') as f: + p = copy.deepcopy(vars(credentials)) + del p["expiry"] + json.dump(p, f) return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) def initialize_upload(youtube, options): diff --git a/ptyt_upload.py b/ptyt_upload.py index f099078..b6a8a77 100755 --- a/ptyt_upload.py +++ b/ptyt_upload.py @@ -54,7 +54,6 @@ if __name__ == '__main__': import pt_upload options = docopt(__doc__, version=VERSION) - print options schema = Schema({ '--file': And(str, lambda s: validateVideo(s), error='file is not supported, please use mp4'),