def testBuildUrl(self): url = 'base' q = collections.OrderedDict([('id', 12345), ('none', None), ('str', 'string'), ('uni', u'בדיקה')]) self.assertEqual( build_url(url, q), 'base?id=12345&none=&str=string&uni=%D7%91%D7%93%D7%99%D7%A7%D7%94' )
def __init__(self, q=None, max_results=20, author=None, orderby='published', videos_json=None, youtube_id_url=None, limit_time='all_time'): """ perform search on youtube parameters: q: (string) the query to search for max_results: (int) maximum number of results to return author: (string) limit to videos uploaded by this youtube user orderby: (string) how to order the videos, possible values: relevance, published, viewCount, rating limit_time: (string) limit to videos uploaded in a certain timeframe possible values: today, this_week, this_month, all_time """ self.videos = [] if videos_json is None and youtube_id_url is not None: videos_json = urllib.urlopen(youtube_id_url + '?alt=json').read() if videos_json is None and q is not None: params = { 'q': q, 'max-results': max_results, 'alt': 'json', 'orderby': orderby } if author is not None: params['author'] = author if limit_time is not None: params['time'] = limit_time url = build_url(self.GDATA_YOUTUBE_VIDEOS_URL, params) videos_json = urllib.urlopen(url).read() if videos_json is not None and len(videos_json) > 0: try: videos_json = videos_json[videos_json. find('{'):videos_json.rfind('}') + 1] yvideos = json.loads(videos_json) except: print "youtube_id_url=" print youtube_id_url print "url" print url print "videos_json=" print videos_json raise yentries = parse_dict(yvideos, {'feed': 'entry'}) if yentries is None: yentry = parse_dict(yvideos, 'entry') if yentry is None: yentries = [] else: yentries = [yentry] for yentry in yentries: video = self._parse_youtube_entry(yentry) self.videos.append(video)
def __init__( self,q=None,max_results=20,author=None,orderby='published', videos_json=None,youtube_id_url=None, limit_time='all_time' ): """ perform search on youtube parameters: q: (string) the query to search for max_results: (int) maximum number of results to return author: (string) limit to videos uploaded by this youtube user orderby: (string) how to order the videos, possible values: relevance, published, viewCount, rating limit_time: (string) limit to videos uploaded in a certain timeframe possible values: today, this_week, this_month, all_time """ self.videos=[] if videos_json is None and youtube_id_url is not None: videos_json=urllib.urlopen(youtube_id_url+'?alt=json').read() if videos_json is None and q is not None: params={ 'q':q, 'max-results':max_results, 'alt':'json', 'orderby':orderby } if author is not None: params['author']=author if limit_time is not None: params['time']=limit_time url=build_url(self.GDATA_YOUTUBE_VIDEOS_URL,params) videos_json=urllib.urlopen(url).read() if videos_json is not None and len(videos_json)>0: try: videos_json=videos_json[videos_json.find('{'):videos_json.rfind('}')+1] yvideos=json.loads(videos_json) except: print "youtube_id_url=" print youtube_id_url print "url" print url print "videos_json=" print videos_json raise yentries=parse_dict(yvideos,{'feed':'entry'}) if yentries is None: yentry=parse_dict(yvideos,'entry') if yentry is None: yentries=[] else: yentries=[yentry] for yentry in yentries: video=self._parse_youtube_entry(yentry) self.videos.append(video)
def testBuildUrl(self): url='base' q=collections.OrderedDict([('id',12345),('none',None),('str','string'),('uni',u'בדיקה')]) self.assertEqual(build_url(url,q),'base?id=12345&none=&str=string&uni=%D7%91%D7%93%D7%99%D7%A7%D7%94')