Skip to content

Commit

Permalink
Merge pull request #15 from NikhilNarayana/develop
Browse files Browse the repository at this point in the history
Add statistics gathering with google spreadsheets.
  • Loading branch information
NikhilNarayana authored Oct 19, 2016
2 parents 4b9dcd7 + e7827cf commit 26e29db
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.pyc
*.mp4
thumbnail.png
start.py-oauth2-spreadsheet.json
2 changes: 1 addition & 1 deletion addTBAToDescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions addtoplaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion client_secrets.json
Original file line number Diff line number Diff line change
@@ -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"]}}
{"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"]}}
2 changes: 1 addition & 1 deletion form_values.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
,
,,,,,,,,,,,,,,,
1 change: 1 addition & 0 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import youtubeup as yup
import argparse
import csv
import pdb

render = web.template.render('webpage/')

Expand Down
4 changes: 2 additions & 2 deletions updatePlaylistThumbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 4 additions & 11 deletions updateThumbnail.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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)
Expand Down
22 changes: 18 additions & 4 deletions youtubeAuthenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
23 changes: 16 additions & 7 deletions youtubeup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import time
import argparse
import pdb

from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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" %
Expand Down

0 comments on commit 26e29db

Please sign in to comment.