Пример #1
0
def get_user_videos(username):
    yt_service = YouTubeService()
    uri = 'http://gdata.youtube.com/feeds/api/users/%s/uploads' % username
    feed = yt_service.GetYouTubeVideoFeed(uri)
    video_obj = {}
    video_obj['source'] = username

    while len(feed.entry) > 0:
        for video in feed.entry:
            video_obj['date'] = video.published.text
            video_obj['title'] = video.title.text
            video_obj['duration'] = int(video.media.duration.seconds)

            url = video.link[0].href

            url = None
            for link in video.link:
                if 'www.youtube.com/watch' in link.href:
                    url = link.href
                    break

            _id = md5.md5(url).hexdigest()
            video_obj['links'] = url
            video_obj['_id'] = _id
            video_content_coll.update({'_id': _id}, video_obj, upsert=True)
            print 'Added url %s with _id %s' % (url, _id)

        try:
            feed = yt_service.GetYouTubeVideoFeed(feed.GetNextLink().href)
        except:
            break
Пример #2
0
def GetAndPrintUserUploads(uri):
    yt_service = YouTubeService()
    
    #PrintVideoFeed(yt_service.GetYouTubeVideoFeed(uri))
    try:
        return yt_service.GetYouTubeVideoFeed(uri)
    except:
        return False
Пример #3
0
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