From e7827cf3549d20053bb1a398011adb4ecafaf45f Mon Sep 17 00:00:00 2001 From: Nikhil Narayana Date: Tue, 18 Oct 2016 21:49:08 -0400 Subject: [PATCH] Add statistics gathering with google spreadsheets. Rename get_authenticated_service to get_youtube_service. New client secrets.json. Remove more useless code. --- .gitignore | 1 + addTBAToDescription.py | 2 +- addtoplaylist.py | 4 ++-- client_secrets.json | 2 +- form_values.csv | 2 +- start.py | 1 + updatePlaylistThumbnails.py | 4 ++-- updateThumbnail.py | 15 ++++----------- youtubeAuthenticate.py | 22 ++++++++++++++++++---- youtubeup.py | 23 ++++++++++++++++------- 10 files changed, 47 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index b24692c..69dab68 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.pyc *.mp4 thumbnail.png +start.py-oauth2-spreadsheet.json diff --git a/addTBAToDescription.py b/addTBAToDescription.py index c53e816..598c6e7 100755 --- a/addTBAToDescription.py +++ b/addTBAToDescription.py @@ -139,7 +139,7 @@ def POST(self): form = dataform() if not form.validates(): return render.forms(form) - youtube = get_authenticated_service() + youtube = get_youtube_service() form.vURL.set_value(run(youtube, form.d.vURL, form.d.pID, form.d.ecode, form.d.mcode, form.d.mnum, form.d.end)) #The return value is always the vURL for the next match if form.d.end == "Only for batch updates": diff --git a/addtoplaylist.py b/addtoplaylist.py index 7dc3b3d..53248e3 100755 --- a/addtoplaylist.py +++ b/addtoplaylist.py @@ -6,7 +6,7 @@ from apiclient.errors import HttpError from oauth2client.tools import argparser -from youtubeAuthenticate import get_authenticated_service +from youtubeAuthenticate import get_youtube_service #Default Variables @@ -38,7 +38,7 @@ def add_video_to_playlist(youtube,videoID,playlistID): if __name__ == '__main__': argparser.add_argument("--vID",required=True,help="Video ID to add to playlist") args = argparser.parse_args() - youtube = get_authenticated_service(args) + youtube = get_youtube_service(args) try: add_video_to_playlist(youtube,args.vID,PLAYLIST_ID) except HttpError, e: diff --git a/client_secrets.json b/client_secrets.json index 94e3595..ed63cf1 100644 --- a/client_secrets.json +++ b/client_secrets.json @@ -1 +1 @@ -{"installed":{"client_id":"57921972910-lnkq4fq1b0qkj2airpmb1aj7mqoktpnt.apps.googleusercontent.com","project_id":"indianafirst-youtube-uploader","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"enEN4fDQUh1GuLKBoZJViWc9","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} \ No newline at end of file +{"installed":{"client_id":"686305491898-va0do5oqap09ibr26dllachprgtmjmpm.apps.googleusercontent.com","project_id":"frc-match-uploader","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"OQxTs6rMuTl_6QCuQFfVRzD3","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} \ No newline at end of file diff --git a/form_values.csv b/form_values.csv index 41622b4..cc888a9 100644 --- a/form_values.csv +++ b/form_values.csv @@ -1 +1 @@ -, \ No newline at end of file +,,,,,,,,,,,,,,, \ No newline at end of file diff --git a/start.py b/start.py index 932a4a2..0124d4f 100755 --- a/start.py +++ b/start.py @@ -5,6 +5,7 @@ import youtubeup as yup import argparse import csv +import pdb render = web.template.render('webpage/') diff --git a/updatePlaylistThumbnails.py b/updatePlaylistThumbnails.py index 7dc8d30..cdd375b 100644 --- a/updatePlaylistThumbnails.py +++ b/updatePlaylistThumbnails.py @@ -3,7 +3,7 @@ from apiclient.errors import HttpError from oauth2client.tools import argparser from updateThumbnail import update_thumbnail -from youtubeAuthenticate import get_authenticated_service +from youtubeAuthenticate import get_youtube_service #Thumbnail file to use THUMBNAIL = "" @@ -34,7 +34,7 @@ def update_thumbnails(youtube,pID,thumbnail): argparser.add_argument("--pID",help="PlaylistID of videos to change thumbnails for",default=PLAYLISTID) argparser.add_argument("--tnail",help="Thumbnail filename, with extension, for playlist",default=THUMBNAIL) args = argparser.parse_args() - youtube = get_authenticated_service() + youtube = get_youtube_service() try: update_thumbnails(youtube,args.pID,args,thumbnail) except HttpError, e: diff --git a/updateThumbnail.py b/updateThumbnail.py index 465a33f..181843d 100644 --- a/updateThumbnail.py +++ b/updateThumbnail.py @@ -1,18 +1,11 @@ -#!/usr/bin/python - -import httplib -import httplib2 -import os -import random -import sys -import time +#!/usr/bin/env python from apiclient.errors import HttpError from oauth2client.tools import argparser -from youtubeAuthenticate import get_authenticated_service +from youtubeAuthenticate import get_youtube_service #Default Variables -THUMBNAIL = "2016 Walker Warren.png" +THUMBNAIL = "" VALID_PRIVACY_STATUSES = ("public", "private", "unlisted") @@ -28,7 +21,7 @@ def update_thumbnail(youtube, video_id, file): argparser.add_argument("--file", help="Thumbnail file to upload", default=THUMBNAIL) args = argparser.parse_args() - youtube = get_authenticated_service(args) + youtube = get_youtube_service(args) try: update_thumbnail(youtube,args.vID,args.file) diff --git a/youtubeAuthenticate.py b/youtubeAuthenticate.py index 91ed8f7..b8a98fd 100644 --- a/youtubeAuthenticate.py +++ b/youtubeAuthenticate.py @@ -41,12 +41,10 @@ YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" -VALID_PRIVACY_STATUSES = ("public", "private", "unlisted") - YOUTUBE_UPLOAD_SCOPE = """https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.force-ssl""" +SPREADSHEETS_SCOPE = "https://www.googleapis.com/auth/spreadsheets" - -def get_authenticated_service(): +def get_youtube_service(): flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_UPLOAD_SCOPE, message=MISSING_CLIENT_SECRETS_MESSAGE) @@ -63,6 +61,22 @@ def get_authenticated_service(): http=credentials.authorize(httplib2.Http())) +def get_spreadsheet_service(): + flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=SPREADSHEETS_SCOPE) + flow.user_agent = "FRC YouTube Uploader" + + storage = Storage("%s-oauth2-spreadsheet.json" % sys.argv[0]) + credentials = storage.get() + + flags = argparser.parse_args(args=[]) + + if credentials is None or credentials.invalid: + credentials = run_flow(flow, storage, flags) + + http = credentials.authorize(httplib2.Http()) + discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?''version=v4') + return build('sheets', 'v4', http=http, discoveryServiceUrl=discoveryUrl) + def get_retry_status_codes(): return RETRIABLE_STATUS_CODES diff --git a/youtubeup.py b/youtubeup.py index c55b316..3cc27d2 100755 --- a/youtubeup.py +++ b/youtubeup.py @@ -7,6 +7,7 @@ import sys import time import argparse +import pdb from apiclient.errors import HttpError from apiclient.http import MediaFileUpload @@ -15,6 +16,7 @@ from addtoplaylist import add_video_to_playlist from updateThumbnail import update_thumbnail from youtubeAuthenticate import * +from datetime import * # Default Variables - comments above DEFAULT_VIDEO_CATEGORY = 28 @@ -259,17 +261,19 @@ def init(args): if args.tiebreak is True: args.mnum = tiebreak_mnum(args.mnum, args.mcode) - youtube = get_authenticated_service() + youtube = get_youtube_service() + spreadsheet = get_spreadsheet_service() - if int(args.end) > int(args.mnum): - upload_multiple_videos(youtube, args) + if type(args.end) is int: + if int(args.end) > int(args.mnum): + upload_multiple_videos(youtube, args) else: try: - initialize_upload(youtube, args) + initialize_upload(youtube, spreadsheet, args) except HttpError, e: print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) -def initialize_upload(youtube, options): +def initialize_upload(youtube, spreadsheet, options): if options.tba: print "Initializing upload for %s match %s" % (options.mcode, options.mnum) tags = None @@ -332,9 +336,9 @@ def initialize_upload(youtube, options): create_filename(options), chunksize=-1, resumable=True) ) - resumable_upload(insert_request, options, mcode, youtube) + resumable_upload(insert_request, options, mcode, youtube, spreadsheet) -def resumable_upload(insert_request, options, mcode, youtube): +def resumable_upload(insert_request, options, mcode, youtube, spreadsheet): response = None error = None retry = 0 @@ -355,6 +359,11 @@ def resumable_upload(insert_request, options, mcode, youtube): request_body = json.dumps({mcode: response['id']}) if options.tba is True: post_video(options.tbaID, options.tbaSecret, request_body, options.ecode) + spreadsheetID = "18flsXvAcYvQximmeyG0-9lhYtb5jd_oRtKzIN7zQDqk" + rowRange = "Data!A1:E1" + values = [[str(datetime.now()),"https://www.youtube.com/watch?v=%s" % response['id'], str(options.tba), options.ename, options.prodteam]] + body = {'values': values} + appendSpreadsheet = spreadsheet.spreadsheets().values().append(spreadsheetId=spreadsheetID, range=rowRange, valueInputOption="RAW", body=body).execute() else: exit("The upload failed with an unexpected response: %s" %