Last active
December 15, 2015 18:08
-
-
Save level09/5300990 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def youtube_upload(package): | |
if package.youtube == '1' or package.youtube == '2': | |
dev_email= G_USER | |
dev_password= G_PASS | |
dev_application= PROD_NAME | |
dev_key= API_KEY | |
youtube_user= YT_USER | |
youtube_password= YT_PASS | |
exclude = set(string.punctuation) | |
s = ''.join(ch for ch in package.youtube_name if ch not in exclude) | |
title = description = s[:60] | |
keywords = 'Al Arabiya' | |
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, | |
#private=gdata.media.Private(), | |
) | |
entry = YouTubeVideoEntry(media=my_media_group) | |
service = YouTubeService(email=dev_email, | |
password=dev_password, | |
source=dev_application, | |
client_id=dev_application, | |
developer_key=dev_key) | |
token_file = "youtube_login_token.txt" | |
login_token = None | |
try: | |
f = open(token_file, 'r') | |
login_token = f.readline().strip() | |
f.close() | |
service.SetClientLoginToken(login_token) | |
except Exception: | |
print "unable to set previous login token, continuing with default login settings" | |
pass | |
while True: | |
captcha_token = None | |
captcha_response = None | |
try: | |
print "trying login with captcha_token=%s and captcha_response=%s" % (captcha_token, captcha_response) | |
service.ClientLogin(dev_email,dev_password, captcha_token=captcha_token, captcha_response=captcha_response) | |
# all good, lets get out of here | |
print "login succeeded continuing on to upload" | |
break | |
except BadAuthentication: | |
print "BadAuthentication returned with credentials of user: %s password: %s" % (youtube_user, youtube_password) | |
print 'Could not upload the video to youtube' | |
print "BadAuthentication: Incorrect login credentials. Rejected by server." | |
return package | |
except CaptchaRequired: | |
print "Captcha Required .. " | |
#print "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) | |
#print "attempted to open url in browser window on mac using open command" | |
#in_captcha = raw_input("Type in the captha you see:") | |
#print "captcha input received from user (%s)" % in_captcha | |
#captcha_token = self.captcha_token | |
#captcha_response = in_captcha.strip() | |
#print "set new client login info from captcha, looping back" | |
# loop back now and try with new credentials | |
except Exception, e: | |
print "Uh oh, big error doing ClientLogin: %s" % e.message | |
print 'Could not upload the video to youtube' | |
return package | |
try: | |
print "saving token (%s) to token file (%s)" % (service.GetClientLoginToken(), token_file) | |
f = open(token_file, 'w') | |
f.write(service.GetClientLoginToken()) | |
f.close() | |
except Exception: | |
print "error occurred while writing to token file : %s " % Exception | |
pass | |
try: | |
print "starting to upload video entry" | |
service.InsertVideoEntry(entry, package.get_video()) | |
print "completed upload of video entry" | |
return package | |
except Exception, e: | |
print 'Could not upload the video to youtube' | |
return package | |
else: | |
#no upload to youtube | |
print 'Youtube upload is not required !' | |
return package |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment