def _uploadVideo(self,filename,video): ret=(False,0) if hasattr(video,'title'): yts=YouTubeService() yts.developer_key=getattr(settings, 'YOUTUBE_DEVELOPER_KEY') yts.SetAuthSubToken(getattr(settings, 'YOUTUBE_AUTHSUB_TOKEN')) res=UploadYoutubeVideo( title=self._getVideoTitleForYoutube(video), description=unicode(video.title).encode('utf-8'), category='News', filename=filename, ytService=yts ) if ( res.isOk and hasattr(res, 'newEntry') and hasattr(res.newEntry, 'id') and hasattr(res.newEntry.id, 'text') ): ret=(True,res.newEntry.id.text) else: self._warn('failed to upload, invalid res from UploadYoutubeVideo') self._warn('msg = '+str(res.errMsg)) self._warn('desc = '+str(res.errDesc)) else: self._warn('video does not have title ('+filename+')') return ret
def search(q, restrict=None): yt_service = YouTubeService() run_on_appengine(yt_service) yt_service.ssl = True yt_service.developer_key = settings.YOUTUBE_API_KEY query = YouTubeVideoQuery() query.vq = q if restrict: query.restriction = restrict query.max_results = 1 feed = yt_service.YouTubeQuery(query) return get_embed_code(feed.entry[0])
def update_playlist_dets(request, user, playlist): #initialize user lists and youtube service try: #initialize user lists and youtube service my_oauth_token = MyOAuthToken.objects.get(key=request.session['oauth_token_key']) filt_playlist_list = my_oauth_token.filtereduserplaylistlist except (MyOAuthToken.DoesNotExist): #get user to authenticate return HttpResponseRedirect(reverse(add_token)) else: yt = YouTubeService() #authenticate youtube service for logged-in user yt_scope = gdata.service.lookup_scopes('youtube') oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, 'anonymous', consumer_secret='anonymous') oauth_token = gdata.auth.OAuthToken(key=str(request.session['oauth_token_key']), secret=str(my_oauth_token.secret), scopes=yt_scope, oauth_input_params=oauth_input_params) yt.SetOAuthToken(oauth_token) sel_playlist = filt_playlist_list.playlistplus_set.get(yt_playlistid=playlist) yt.developer_key = '' for i in range(sel_playlist.length): video = sel_playlist.videoinplaylist_set.get(position=(i+1)) video.delete() #getting the new playlists vid_count=0 for i in range(8): j=(25*i)+1; playlist_feed = yt.GetYouTubePlaylistVideoFeed(uri='http://gdata.youtube.com/feeds/api/playlists/'+sel_playlist.yt_playlistid+'?start-index='+str(j)+"&v=2") # sel_playlist.yt_playlistid+ for entry in playlist_feed.entry: try: vid_id = entry.GetHtmlLink().href vid_duration = entry.media.duration.seconds # this throws an error but returns the right value - I dont know why except (AttributeError): #when the duration cannot be retrieved, it is because the video is inaccessible (dead) so deal with dead videos here #just going to ignore dead videos for now - they will not be entered into the database continue else: vid_count = vid_count+1 videoinplaylist = VideoInPlaylist(title=entry.title.text, yt_id=str(vid_id), duration=vid_duration, quality ='', isalive=True, isrestricted=False, playlistplus=sel_playlist, position=vid_count) #TODO: populate fields from the youtube data - quality, isalive, isrestricted videoinplaylist.save() sel_playlist.length=vid_count sel_playlist.save() ordered_videos = sel_playlist.videoinplaylist_set.order_by('position') return render_to_response('youtube/playlistdetails.html', {'filt_playlist_list': filt_playlist_list, 'playlistplus': sel_playlist, 'ordered_videos':ordered_videos}, context_instance=RequestContext(request))
def update_playlist_list(request, user): try: #initialize user lists and youtube service my_oauth_token = MyOAuthToken.objects.get(key=request.session['oauth_token_key']) filt_playlist_list = my_oauth_token.filtereduserplaylistlist except (MyOAuthToken.DoesNotExist): #get user to authenticate return HttpResponseRedirect(reverse(add_token)) else: yt = YouTubeService() #authenticate youtube service for logged-in user yt_scope = gdata.service.lookup_scopes('youtube') oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, 'anonymous', consumer_secret='anonymous') oauth_token = gdata.auth.OAuthToken(key=str(request.session['oauth_token_key']), secret=str(my_oauth_token.secret), scopes=yt_scope, oauth_input_params=oauth_input_params) yt.SetOAuthToken(oauth_token) yt.developer_key = '' #deleting the original data from the database playlist_set = PlaylistPlus.objects.filter(filtereduserplaylistlist=filt_playlist_list) for playlistplus in playlist_set: video_set = VideoInPlaylist.objects.filter(playlistplus=playlistplus) for videoinplaylist in video_set: videoinplaylist.delete() playlistplus.delete() playlistfeed = yt.GetYouTubePlaylistFeed(username='******') #getting the new playlists i=0 #for some reason the TotalResults object returns a number larger than the *actual* number of playlists. Maybe there is another attribute I have missed? for entry in playlistfeed.entry: playlist_tag = re.match(r'##enablist##', entry.title.text) if (playlist_tag==None): playlistplus = PlaylistPlus(filtereduserplaylistlist=filt_playlist_list, title=entry.title.text, isincluded=True, isprivate=False, yt_playlistid=entry.id.text, length=0) #TODO assign the proper values to isincluded, and isprivate, so they can be used in later features idlist = re.split('/', playlistplus.yt_playlistid) playlistplus.yt_playlistid = idlist[-1] playlistplus.save() i=i+1 else: continue filt_playlist_list.length=i filt_playlist_list.num_included = i filt_playlist_list.save() return HttpResponseRedirect('/user/'+user+'/playlists/')
def youtube_login(): """ Authenticate against YouTube. Returns a YouTubeService object """ yt_service = YouTubeService() yt_service.email = getattr(settings, 'YOUTUBE_EMAIL', '*****@*****.**') yt_service.password = getattr(settings, 'YOUTUBE_PASSWORD', 'your password') yt_service.source = getattr(settings, 'YOUTUBE_APP_LABEL', 'YouTube Tea Timer') yt_service.client_id = getattr(settings, 'YOUTUBE_APP_ID', 'youtube-tea-timer') yt_service.developer_key = getattr(settings, 'YOUTUBE_DEVELOPER_KEY', 'devkey') yt_service.ProgrammaticLogin() return yt_service
class YoutubeMusic: def __init__(self): self.service = YouTubeService() def search(self, artist): query = YouTubeVideoQuery() query.vq = artist query.orderby = 'relevance' query.racy = 'exclude' query.format = '5' query.max_results = 50 query.categories.append("/Music") feed = self.service.YouTubeQuery(query) results = [] for entry in feed.entry: if not self.is_valid_entry(artist, entry): continue results.append({ 'url': entry.media.player.url, 'title': smart_str(entry.media.title.text), 'duration': int(entry.media.duration.seconds), }) return {'artist': artist, 'results': results} def is_valid_entry(self, artist, entry): duration = int(entry.media.duration.seconds) title = smart_str(entry.media.title.text).lower() if entry.rating is not None and float(entry.rating.average) < 3.5: return False if entry.statistics is None or int(entry.statistics.view_count) < 1000: return False if duration < (2 * 60) or duration > (9 * 60): return False if artist.lower() not in title: return False if re.search(r"\b(perform|performance|concert|cover)\b", title): return False return True
def upnp_init(self): self.current_connection_id = None if self.server: self.server.connection_manager_server.set_variable( 0, 'SourceProtocolInfo', [f'http-get:*:{MPEG4_MIMETYPE}:*'], default=True, ) self.wmc_mapping = {'15': self.get_root_id()} self.yt_service = YouTubeService() self.yt_service.client_id = ( 'ytapi-JeanMichelSizun-youtubebackendpl-ruabstu7-0') self.yt_service.developer_key = ( 'AI39si7dv2WWffH-s3pfvmw8fTND-cPWeqF1DOcZ8rwTg' 'TPi4fheX7jjQXpn7SG61Ido0Zm_9gYR52TcGog9Pt3iG9Sa88-1yg') self.yt_service.email = self.login self.yt_service.password = self.password self.yt_service.source = 'Coherence UPnP backend' if len(self.login) > 0: d = threads.deferToThread(self.yt_service.ProgrammaticLogin)
def addNyYoutube(self, id='', REQUEST=None, contributor=None, **kwargs): """ Create a Youtube embeded video. """ if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', '')) recaptcha_response = schema_raw_data.get('g-recaptcha-response', '') id = make_id(self, id=id, title=schema_raw_data.get('title', ''), prefix='ep') if contributor is None: contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() ob = _create_NyYoutube_object(self, id, contributor) if schema_raw_data['iframe_width'] in ['', '0']: schema_raw_data['iframe_width'] = 640 if schema_raw_data['iframe_height'] in ['', '0']: schema_raw_data['iframe_height'] = 360 if len(schema_raw_data['youtube_id']) > 11: try: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('watch?v=')[1][:11] except IndexError: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('&v=')[1][:11] form_errors = ob.process_submitted_form(schema_raw_data, _lang, _override_releasedate=_releasedate) try: schema_raw_data['iframe_width'] = int(schema_raw_data['iframe_width']) except ValueError: form_errors['iframe_width'] = ['Integer value required.'] try: schema_raw_data['iframe_height'] = int( schema_raw_data['iframe_height']) except ValueError: form_errors['iframe_height'] = ['Integer value required.'] if not schema_raw_data['youtube_id']: form_errors['youtube_id'] = ['Youtube Id is mandatory'] if schema_raw_data['youtube_id']: yt_service = YouTubeService() try: yt_service.GetYouTubeVideoEntry( video_id=schema_raw_data['youtube_id']) except RequestError: form_errors['youtube_id'] = [ 'Invalid Youtube ID (inexisting video)' ] # check Captcha/reCaptcha if not self.checkPermissionSkipCaptcha(): captcha_validator = self.validateCaptcha(recaptcha_response, REQUEST) if captcha_validator: form_errors['captcha'] = captcha_validator if form_errors: if REQUEST is None: raise ValueError(form_errors.popitem()[1]) # pick a random error else: import transaction transaction.abort() # because we already called _crete_NyZzz_object ob._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/youtube_add_html' % self.absolute_url()) return # process parameters if self.glCheckPermissionPublishObjects(): approved, approved_by = (1, self.REQUEST.AUTHENTICATED_USER.getUserName()) else: approved, approved_by = 0, None ob.approveThis(approved, approved_by) ob.submitThis() if ob.discussion: ob.open_for_comments() self.recatalogNyObject(ob) notify(NyContentObjectAddEvent(ob, contributor, schema_raw_data)) # log post date auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) # redirect if case if REQUEST is not None: l_referer = REQUEST['HTTP_REFERER'].split('/')[-1] if (l_referer == 'youtube_manage_add' or l_referer.find('youtube_manage_add') != -1): return self.manage_main(self, REQUEST, update_menu=1) elif l_referer == 'youtube_add_html': self.setSession('referer', self.absolute_url()) return ob.object_submitted_message(REQUEST) REQUEST.RESPONSE.redirect('%s/messages_html' % self.absolute_url()) return ob.getId()
def service(self): yt_service = YouTubeService() yt_service.ssl = False yt_service.developer_key = getattr(settings, 'YOUTUBE_DEV_KEY', None) yt_service.client_id = getattr(settings, 'YOUTUBE_CLIENT_ID', None) return yt_service
from django.utils.translation import ugettext_lazy as _ from videos.types import video_type_registrar, VideoTypeError from statistic import update_subtitles_fetch_counter, video_view_counter, changed_video_set from widget import video_cache from utils.redis_utils import RedisSimpleField from django.template.defaultfilters import slugify from utils.amazon import S3EnabledImageField from django.utils.http import urlquote_plus from django.utils import simplejson as json from django.core.urlresolvers import reverse import time from django.utils.safestring import mark_safe from teams.tasks import update_team_video, update_team_video_for_sl from videos.tasks import check_alarm, detect_language, send_notification yt_service = YouTubeService() yt_service.ssl = False NO_SUBTITLES, SUBTITLES_FINISHED = range(2) VIDEO_TYPE_HTML5 = 'H' VIDEO_TYPE_YOUTUBE = 'Y' VIDEO_TYPE_BLIPTV = 'B' VIDEO_TYPE_GOOGLE = 'G' VIDEO_TYPE_FORA = 'F' VIDEO_TYPE_USTREAM = 'U' VIDEO_TYPE_VIMEO = 'V' VIDEO_TYPE_DAILYMOTION = 'D' VIDEO_TYPE_FLV = 'L' VIDEO_TYPE = ((VIDEO_TYPE_HTML5, 'HTML5'), (VIDEO_TYPE_YOUTUBE, 'Youtube'), (VIDEO_TYPE_BLIPTV, 'Blip.tv'), (VIDEO_TYPE_GOOGLE, 'video.google.com'),
def create_playlist(request, user, playlist): try: #initialize user lists and youtube service try: #check for user token - search in the database my_oauth_token = MyOAuthToken.objects.filter(key=request.session['oauth_token_key'])[0] filt_playlist_list = my_oauth_token.filtereduserplaylistlist except (MyOAuthToken.DoesNotExist): #get user to authenticate return HttpResponseRedirect(reverse(add_token)) else: yt = YouTubeService() #authenticate youtube service for logged-in user yt_scope = gdata.service.lookup_scopes('youtube') oauth_input_params = gdata.auth.OAuthInputParams(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, 'anonymous', consumer_secret='anonymous') oauth_token = gdata.auth.OAuthToken(key=str(request.session['oauth_token_key']), secret=str(my_oauth_token.secret), scopes=yt_scope, oauth_input_params=oauth_input_params) yt.SetOAuthToken(oauth_token) sel_playlist = filt_playlist_list.playlistplus_set.get(yt_playlistid=playlist) yt.developer_key = '' #should always have a value of this posted sel_videos=request.POST['sel_videos'] except(KeyError): #put more errors here return HttpResponseRedirect(reverse(playlistlist.views.playlist_dets, args=[user, sel_playlist.yt_playlistid])) #TODO return error messages to views else: try: shhhh=request.POST['shuffle'] except(KeyError): shuffle=False else: shuffle=True #retrieve the last number "sel_videos" of video objects in the selected list (from local database; not a youtube call) temp_video_list = list() for i in range(int(sel_videos)): temp_video = sel_playlist.videoinplaylist_set.get(position=(sel_playlist.length-i)) temp_video_list.append(temp_video) #shuffles the playlist if it was checked by the user # implement the fisher-Yates shuffle algorithm: # swapping each video in turn with another video from a random position in the part of the list that has not yet been passed through (including itself) if (shuffle==True): for i in range(int(sel_videos)): # generate random number between 0 and sel_videos-(i+1) ran_num = random.randint(0, int(sel_videos)-(i+1)) temp = temp_video_list[int(sel_videos)-(i+1)] temp_video_list[int(sel_videos)-(i+1)] = temp_video_list[ran_num] temp_video_list[ran_num] = temp created_playlist_tag = '##enablist## ' #check if a playlsit already exists. If it does, need to use different code to update rather than create playlist #looks for playlist with the same title as the playlist we are about to add curr_playlist_feed = yt.GetYouTubePlaylistFeed(username='******') playlist_exists=False for entry in curr_playlist_feed.entry: if (entry.title.text==created_playlist_tag+sel_playlist.title): playlist_exists=True playlist_uri = entry.id.text break else: continue response=True if (playlist_exists==True): #we delete the existing playlist, then continue to add the new playlist response = yt.DeletePlaylist(playlist_uri) #if deletion is not successful, sets response=False if (response==True): #create playlist at youtube new_private_playlistentry = yt.AddPlaylist(created_playlist_tag+sel_playlist.title, '', True) playlist_added=False #check that playlist was added successfully if isinstance(new_private_playlistentry, gdata.youtube.YouTubePlaylistEntry): playlist_added=True #retrieve playlist id for newly-added playlist new_playlist_id = new_private_playlistentry.id.text #this is incorrect new_playlist_id = new_playlist_id.split('/')[-1] new_playlist_feed = 'http://gdata.youtube.com/feeds/api/playlists/'+new_playlist_id if (playlist_added == True): #add our list of videos to the playlist for j in range(int(sel_videos)-1): #the ilst is in reverse order, so add videos in reverse i = j+1 temp_video_list[-i].yt_id = temp_video_list[-i].yt_id.split('&')[-2] temp_video_list[-i].yt_id = temp_video_list[-i].yt_id.split('=')[-1] #return HttpResponse('%s %s' % (new_playlist_id, temp_video_list[1].yt_id)) try: playlist_video_entry = yt.AddPlaylistVideoEntryToPlaylist(new_playlist_feed, temp_video_list[-i].yt_id) except(gdata.service.RequestError): sleep(5) try: playlist_video_entry = yt.AddPlaylistVideoEntryToPlaylist(new_playlist_feed, temp_video_list[-i].yt_id) except(gdata.service.RequestError): sleep(15) try: playlist_video_entry = yt.AddPlaylistVideoEntryToPlaylist(new_playlist_feed, temp_video_list[-i].yt_id) except(gdata.service.RequestError): return HttpResponse('Youtube is not playing ball right now. Please try again in a minute.') else: #delete the half-formed playlist created so far - if adding video fails? - no, will get overwritten anyway continue else: #delete the half-formed playlist created so far - if adding video fails? - no, will get overwritten anyway continue else: continue sleep(0.5) # deal with zero separately. this is a bit hack-y. oh well temp_video_list[0].yt_id = temp_video_list[0].yt_id.split('&')[-2] temp_video_list[0].yt_id = temp_video_list[0].yt_id.split('=')[-1] #return HttpResponse('%s %s' % (new_playlist_id, temp_video_list[1].yt_id)) playlist_video_entry = yt.AddPlaylistVideoEntryToPlaylist(new_playlist_feed, temp_video_list[0].yt_id) if (isinstance(playlist_video_entry, gdata.youtube.YouTubePlaylistVideoEntry)==False): #delete the half-formed playlist created so far - if adding video fails? return HttpResponse('Fail.') #once videos are added, display details of the playlist created - just send to youtube for now return HttpResponseRedirect('http://www.youtube.com/playlist?list='+new_playlist_id+'&feature=viewall') else: return HttpResponseRedirect(reverse(playlistlist.views.playlist_dets, args=[user, sel_playlist.yt_playlistid])) #TODO return error messages to views else: return HttpResponseRedirect(reverse(playlistlist.views.playlist_dets, args=[user, sel_playlist.yt_playlistid]))
def __init__(self): WebDataSource.__init__(self) self.youtube_service = YouTubeService()
def __init__(self, config): """Constructor.""" YouTubeService.__init__(self) googlecl.service.BaseServiceCL.__init__(self, SECTION_HEADER, config)
def __init__(self): self.service = YouTubeService()
def refresh_creds(self, credentials, sleep): global gd_client time.sleep(sleep) credentials.refresh(httplib2.Http()) now = datetime.utcnow() expires = credentials.token_expiry expires_seconds = (expires - now).seconds # print ("Expires %s from %s = %s" % (expires,now,expires_seconds) ) if self.service == 'docs': gd_client = DocsService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'picasa': gd_client = PhotosService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'finance': gd_client = FinanceService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'calendar': gd_client = CalendarService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'contacts': gd_client = ContactsService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'youtube': gd_client = YouTubeService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) elif self.service == 'blogger': gd_client = BloggerService(email='default', additional_headers={ 'Authorization': 'Bearer %s' % credentials.access_token }) d = threading.Thread(name='refresh_creds', target=self.refresh_creds, args=(credentials, expires_seconds - 10)) d.setDaemon(True) d.start() return gd_client
def saveProperties(self, REQUEST=None, **kwargs): """ """ if not self.checkPermissionEditObject(): raise EXCEPTION_NOTAUTHORIZED(EXCEPTION_NOTAUTHORIZED_MSG) if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', ''), self.releasedate) if schema_raw_data['iframe_width'] in ['', '0']: schema_raw_data['iframe_width'] = 640 if schema_raw_data['iframe_height'] in ['', '0']: schema_raw_data['iframe_height'] = 360 if len(schema_raw_data['youtube_id']) > 11: try: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('watch?v=')[1][:11] except IndexError: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('&v=')[1][:11] form_errors = self.process_submitted_form( schema_raw_data, _lang, _override_releasedate=_releasedate) try: schema_raw_data['iframe_width'] = int( schema_raw_data['iframe_width']) except ValueError: form_errors['iframe_width'] = ['Integer value required.'] try: schema_raw_data['iframe_height'] = int( schema_raw_data['iframe_height']) except ValueError: form_errors['iframe_height'] = ['Integer value required.'] if not schema_raw_data['youtube_id']: form_errors['youtube_id'] = ['Youtube Id is mandatory'] if schema_raw_data['youtube_id']: yt_service = YouTubeService() try: yt_service.GetYouTubeVideoEntry( video_id=schema_raw_data['youtube_id']) except RequestError: form_errors['youtube_id'] = [ 'Invalid Youtube ID (inexisting video)' ] if not form_errors: if self.discussion: self.open_for_comments() else: self.close_for_comments() self._p_changed = 1 self.recatalogNyObject(self) # log date contributor = self.REQUEST.AUTHENTICATED_USER.getUserName() auth_tool = self.getAuthenticationTool() auth_tool.changeLastPost(contributor) notify(NyContentObjectEditEvent(self, contributor)) if REQUEST: self.setSessionInfoTrans(MESSAGE_SAVEDCHANGES, date=self.utGetTodayDate()) REQUEST.RESPONSE.redirect('%s/edit_html?lang=%s' % (self.absolute_url(), _lang)) else: if REQUEST is not None: self._prepare_error_response(REQUEST, form_errors, schema_raw_data) REQUEST.RESPONSE.redirect('%s/edit_html?lang=%s' % (self.absolute_url(), _lang)) else: raise ValueError(form_errors.popitem()[1]) # pick an error
def manageProperties(self, REQUEST=None, **kwargs): """ """ if not self.checkPermissionEditObject(): raise EXCEPTION_NOTAUTHORIZED(EXCEPTION_NOTAUTHORIZED_MSG) if REQUEST is not None: schema_raw_data = dict(REQUEST.form) else: schema_raw_data = kwargs _lang = schema_raw_data.pop('_lang', schema_raw_data.pop('lang', None)) _releasedate = self.process_releasedate( schema_raw_data.pop('releasedate', ''), self.releasedate) _approved = int(bool(schema_raw_data.pop('approved', False))) if schema_raw_data['iframe_width'] in ['', '0']: schema_raw_data['iframe_width'] = 640 if schema_raw_data['iframe_height'] in ['', '0']: schema_raw_data['iframe_height'] = 360 if len(schema_raw_data['youtube_id']) > 11: try: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('watch?v=')[1][:11] except IndexError: schema_raw_data['youtube_id'] = schema_raw_data[ 'youtube_id'].split('&v=')[1][:11] form_errors = self.process_submitted_form( schema_raw_data, _lang, _override_releasedate=_releasedate) try: schema_raw_data['iframe_width'] = int( schema_raw_data['iframe_width']) except ValueError: form_errors['iframe_width'] = ['Integer value required.'] try: schema_raw_data['iframe_height'] = int( schema_raw_data['iframe_height']) except ValueError: form_errors['iframe_height'] = ['Integer value required.'] if not schema_raw_data['youtube_id']: form_errors['youtube_id'] = ['Youtube Id is mandatory'] if schema_raw_data['youtube_id']: yt_service = YouTubeService() try: yt_service.GetYouTubeVideoEntry( video_id=schema_raw_data['youtube_id']) except RequestError: form_errors['youtube_id'] = [ 'Invalid Youtube ID (inexisting video)' ] if form_errors: raise ValueError(form_errors.popitem()[1]) # pick a random error if _approved != self.approved: if _approved == 0: _approved_by = None else: _approved_by = self.REQUEST.AUTHENTICATED_USER.getUserName() self.approveThis(_approved, _approved_by) self._p_changed = 1 if self.discussion: self.open_for_comments() else: self.close_for_comments() self.recatalogNyObject(self) if REQUEST: REQUEST.RESPONSE.redirect('manage_edit_html?save=ok')
def __init__(self, developer_key, authsub_token): gdata_YouTubeService.__init__(self, developer_key=developer_key) self.SetAuthSubToken(authsub_token)
def get_yt_client(username, password, developer_key): from gdata.youtube.service import YouTubeService yt = YouTubeService() yt.ClientLogin(username, password, 'gg2yt') yt.developer_key = developer_key return yt
from base import VideoType, VideoTypeError from auth.models import CustomUser as User from datetime import datetime import random from django.utils.translation import ugettext_lazy as _ from lxml import etree from django.conf import settings from django.utils.http import urlquote import logging from utils.celery_utils import task logger = logging.getLogger('youtube') SUPPORTED_LANGUAGES_DICT = dict(settings.ALL_LANGUAGES) yt_service = YouTubeService() yt_service.ssl = False _('Private video') _('Undefined error') @task def save_subtitles_for_lang(lang, video_pk, youtube_id): from videos.models import Video lc = lang.get('lang_code') if not lc in SUPPORTED_LANGUAGES_DICT: return try:
class YouTube(WebDataSource): ''' searches youtube video library ''' YT_ATOM_RESULT_TO_DICT_MAPPING = { 'media.title.text': 'title', 'published.text': 'published', 'media.description.text': 'content', 'media.duration.seconds': 'duration', 'statistics.view_count': 'statistics_viewcount', 'statistics.favorite_count': 'statistics_favoritecount', 'rating.average': 'rating_average', 'rating.max': 'rating_max', 'rating.min': 'rating_min', 'rating.num_raters': 'rating_numraters', 'summary': 'summary', 'rights': 'rights', 'updated.text': 'last_modified', 'source': 'yt_source' } YT_COMMENTS_MAPPING = { 'id.text': 'id', 'title.text': 'title', 'published.text': 'published', 'updated.text': 'last_modified', 'content.text': 'content' } def __init__(self): WebDataSource.__init__(self) self.youtube_service = YouTubeService() def search(self, search_terms, location=None, max_results=MAX_RESULTS_PER_QUERY, max_age=None, orderby='published', max_comment_count=0): """ Searches for youtube videos. @param search_terms: list of search terms @param location: tuple latitude, longitue, e.g. 37.42307,-122.08427 @param max_results: @param max_age: datetime of the oldest entry @param orderby: order search results by (relevance, published, viewCount, rating) @param max_comment_count: maximum number of comments to fetch (default: 0) """ if not (isinstance(search_terms, list) or isinstance( search_terms, tuple) or isinstance(search_terms, set)): raise ValueError("Warning search requires a list of search terms, \ rather than a single term") # all youtube search parameter are here: # https://developers.google.com/youtube/2.0/reference?hl=de#Custom_parameters query = YouTubeVideoQuery() query.vq = ', '.join(search_terms) query.orderby = orderby query.racy = 'include' query.time = self.get_query_time(max_age) query.max_results = MAX_RESULTS_PER_QUERY if location: query.location = location return self.search_youtube(query, max_results, max_comment_count) @classmethod def get_query_time(cls, max_age): ''' converts a datetime or int (age in minutes) to the youtube specific query parameter (e.g. this_month, today ...) @param max_age: int or datetime object @return: youtube specific query_time ''' if not max_age: return 'all_time' if isinstance(max_age, datetime): # convert datetime to minutes max_age = (datetime.now() - max_age).total_seconds() / 60 if max_age <= 1440: query_time = 'today' elif max_age > 1440 and max_age <= 10080: query_time = 'this_week' else: query_time = 'this_month' return query_time def search_youtube(self, query, max_results=MAX_RESULTS_PER_QUERY, max_comment_count=0): ''' executes the youtube query and facilitates paging of the resultset @param query: YouTubeVideoQuery @param max_results: @param max_comment_count: maximum number of comments to fetch @return: list of dictionaries ''' result = [] feed = self.youtube_service.YouTubeQuery(query) while feed: for entry in feed.entry: try: yt_dict = self.convert_feed_entry(entry, max_comment_count) result.append(yt_dict) except Exception, e: logger.exception('Exception converting entry: %s' % e) if len(result) == max_results: return result if not feed.GetNextLink(): break feed = self.youtube_service.GetYouTubeVideoFeed( feed.GetNextLink().href) return result
class YouTubeBase(object): """ A simple class to get the info for a youtube feed """ def __init__(self, url=None): self.url = url self._service = None self.metadata = {} self.entries = [] self.process_url() def process_url(self): """ Overridden by subclasses """ pass def get_service(self): """ Return the cached service object or get it and cache it. """ if self._service is None: self._service = YouTubeService() self._service.email = settings.SERVICES['YOUTUBE']['EMAIL'] self._service.password = settings.SERVICES['YOUTUBE']['PASSWORD'] self._service.ProgrammaticLogin() return self._service @staticmethod def get_url_from_links(link_list, rel): """ From a list of link elements, return the url for the given rel type """ for item in link_list: if item.rel == rel: return item.href @staticmethod def get_default_content(content_list): """ return the default content from a content list """ for fmt in content_list: if 'isDefault' in fmt.extension_attributes: return fmt @staticmethod def get_biggest_thumbnail(thumb_list): """ return the biggest thumbnail item """ biggest = None big_size = 0 for item in thumb_list: if biggest is None: biggest = item big_size = int(item.width) * int(item.height) else: item_size = int(item.width) * int(item.height) if item_size > big_size: biggest = item big_size = item_size return biggest @staticmethod def convert_to_python(element): """ Convert to base python structures """ from atom.core import XmlElement from atom import AtomBase ignorable_keys = ['scheme', 'namespace'] if isinstance(element, (list, tuple)): output = [] for item in element: output.append(YouTubeBase.convert_to_python(item)) elif isinstance(element, dict): output = {} for key, val in element.items(): if key.startswith("_") or key in ignorable_keys or val is None: continue elif key == 'extension_attributes': output.update(val) elif key == 'extension_elements': for item in val: # Do something with item.attributes? # if item.attributes: # print item # raise if item.children: output[item.tag] = YouTubeBase.convert_to_python( item.children) else: output[item.tag] = item.text elif isinstance(val, (tuple, list)): if len(val) == 0: continue output[key] = [] for item in val: output[key].append(YouTubeBase.convert_to_python(item)) elif isinstance(val, (basestring, int, float, bool)): output[key] = val elif isinstance(val, dict): output[key] = YouTubeBase.convert_to_python(val) elif issubclass(val.__class__, (XmlElement, AtomBase)): output[key] = YouTubeBase.convert_to_python(val.__dict__) else: output[key] = val print "Unkonwn type: ", type(val), val.__class__ if len(output.keys()) == 1: return output.values()[0] elif set(output.keys()) == set(('text', 'type')): return output['text'] else: return output else: return YouTubeBase.convert_to_python(element.__dict__)