mirror of
https://git.lecygnenoir.info/LecygneNoir/prismedia.git
synced 2025-10-03 09:29:16 +02:00
store youtube credentials in a file to avoid asking creds for each uploads
This commit is contained in:
parent
3e0865ad43
commit
33687a1a90
3 changed files with 22 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -60,3 +60,4 @@ target/
|
|||
# Project
|
||||
youtube_secret.json
|
||||
peertube_secret
|
||||
.youtube_credentials.json
|
|
@ -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):
|
||||
|
|
|
@ -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'),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue