Create a gist now

Instantly share code, notes, and snippets.

youtube uploading python script.
from gdata.youtube import YouTubeVideoEntry
from gdata.youtube.service import YouTubeService
import gdata
from gdata.service import BadAuthentication, CaptchaRequired
from optparse import OptionParser
import sys
import time
import os
debug = True
def log(line):
global debug
if debug:
print "uploader-log: %s\n" % line
def doit(the_time, the_filename):
# ****************************************
# *******begin channel config changes******
log("running doit with the_time=%s and the_filename=%s" % (the_time, the_filename))
title=the_time
# title=sys.argv[1] + "extra title stuff"
# couldn't get this to work. fine for now.
description=the_time
# would like these to be longer
#
keywords="broadcaster,brooklyn,brooklynmobile,videobooth,news,public,publicdomain"
dev_email="@gmail.com"
dev_password="yourgmailpasswordhere"
dev_application="brooklynmobiletwvee"
dev_key="AI39si5QN5lN5wn-xc4QlxgcWIVE-kPF2oaH_r2z6YECsvOHkPHU8isWn3NZEIBA-IXCz0SfuYAQN9UkJtSDat6NtCXhrt7gcg" #edlab
youtube_user="yourusernamehere"
youtube_password="yourpasswordhere"
# **********end channel config ********
# *************************************
# create video entry
# prepare a media group object to hold our video's meta-data
my_media_group = gdata.media.Group(
title=gdata.media.Title(text=title),
description=gdata.media.Description(description_type='plain',
text=description),
keywords=gdata.media.Keywords(text=keywords),
category=gdata.media.Category(
text='People',
scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
label='People & Blogs'),
player=None
)
log("created media_group")
entry = YouTubeVideoEntry(media=my_media_group)
log("created youtube entry")
service = YouTubeService(email=dev_email,
password=dev_password,
source=dev_application,
client_id=dev_application,
developer_key=dev_key)
log("created YouTubeService")
# seems like this isn't required (done by client login)
# try:
# service.ProgrammaticLogin()
# except Exception as e:
# print type(e)
# print e
# raise
# read token from file and use that if its still good
token_file = "youtube_login_token.txt"
login_token = None
try:
log("attempting to find previous login token in token file")
f = open(token_file, 'r')
log("token file found")
login_token = f.readline().strip()
f.close()
service.SetClientLoginToken(login_token)
log("set login token to %s" % login_token)
except Exception:
log("unable to set previous login token, continuing with default login settings")
pass
while True:
captcha_token = None
captcha_response = None
try:
log("trying login with captcha_token=%s and captcha_response=%s" % (captcha_token, captcha_response))
service.ClientLogin(youtube_user,youtube_password, captcha_token=captcha_token, captcha_response=captcha_response)
# all good, lets get out of here
log("login succeeded continuing on to upload")
break
except BadAuthentication:
log("BadAuthentication returned with credentials of user: %s password: %s" % (youtube_user, youtube_password))
print "BadAuthentication: Incorrect login credentials. Rejected by server."
raise Error
except CaptchaRequired:
log("CaptchaRequired returned from clientlogin(). requesting credentials")
print "CaptchaRequired: required captcha input from user for uploading please supply it.\n"
print "Captcha Address: " + self.captcha_url
os.system('open ' + self.captcha_url)
log("attempted to open url in browser window on mac using open command")
in_captcha = raw_input("Type in the captha you see:")
log("captcha input received from user (%s)" % in_captcha)
captcha_token = self.captcha_token
captcha_response = in_captcha.strip()
log("set new client login info from captcha, looping back")
# loop back now and try with new credentials
except Error:
log("Uh oh, big error doing ClientLogin: %s" % Error)
raise Error
# save latest version of login token to a file for the next run
try:
log("saving token (%s) to token file (%s)" % (service.GetClientLoginToken(), token_file))
f = open(token_file, 'w')
f.write(service.GetClientLoginToken())
f.close()
except Exception:
log("error occurred while writing to token file : %s " % Exception)
pass
# insert video entry
try:
log("starting to upload video entry")
service.InsertVideoEntry(entry, the_filename)
log("completed upload of video entry")
except Exception, e:
print type(e)
print e
raise
# call routine as function if this is run as a script
# allows us to catch any errors and report back to user
if __name__ == '__main__':
try:
# arg 1 is the time
# arg 2 is the filename
doit(sys.argv[1], sys.argv[2])
except Exception, e:
log("Some Error ocurred, returning failure to calling script (1)")
print type(e)
print e
sys.exit(1)
else:
log("%s uploaded to service successfully. Returning success to calling script (0)" % sys.argv[2])
sys.exit(0)
@Zeokat
Zeokat commented Mar 5, 2014

Still works this script?? Zeokat needs it for a project. Go to test it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment