Beispiel #1
0
def _get_video_data(youtube_data, playlist=None):
    """Extract data from youtube video json record and return Video model."""
    def get_category(categories):
        for category in categories:
            if category['scheme'].endswith('categories.cat'):
                return category['$t']   # TODO: map category
    media = youtube_data['media$group']
    video = Video(
        source_videoid=media['yt$videoid']['$t'],
        source_listid=playlist,
        source_username=media['media$credit'][0]['$t'],
        date_published=_parse_datetime(youtube_data['published']['$t']),
        title=youtube_data['title']['$t'],
        duration=int(media['yt$duration']['seconds']) if 'yt$duration' in media else -1,
    )
    video.source_category = get_category(media.get('media$category', []))
    video.source_view_count = int(youtube_data['yt$statistics']['viewCount']) if 'yt$statistics' in youtube_data else -1
    video.source_date_uploaded = media['yt$uploaded']['$t']
    access_control = dict(
        (i['action'], i['permission'] == 'allowed')
        for i in youtube_data.get('yt$accessControl', []))
    video.restricted = access_control.get('embed') is False
    if 'app$control' in youtube_data:
        if 'yt$incomplete' in youtube_data['app$control']:
            video.restricted = True
        else:
            state = youtube_data['app$control']['yt$state']
            if state['name'] == 'restricted':
                if state['reasonCode'] == 'limitedSyndication':
                    # see https://groups.google.com/d/msg/youtube-api-gdata/on504fCOEk0/oErUbCptWu4J
                    video.restricted = not any(c.get('yt$format') == 5 for c in
                                               media.get('media$content', []))
                else:
                    video.restricted = True
    for thumbnail in media.get('media$thumbnail', []):
        if 'time' not in thumbnail:
            video.thumbnails.append(
                VideoThumbnail(
                    url=thumbnail['url'],
                    width=thumbnail['width'],
                    height=thumbnail['height']))
    for restriction in media.get('media$restriction', []):
        if restriction['type'] == 'country':
            video.restrictions.extend(
                VideoRestriction(
                    relationship=restriction['relationship'],
                    country=country) for country in restriction['$t'].split())
    return video
Beispiel #2
0
def _get_video_data_v3(youtube_data, playlist=None):
    snippet = youtube_data['snippet']
    video = Video(
        source_videoid=youtube_data['id']['videoId'],
        source_listid=playlist,
        title=snippet['title'],
        # http://code.google.com/p/gdata-issues/issues/detail?id=4294
        # duration=snippet['duration'],
    )
    video.source_category = None
    video.source_view_count = None
    video.source_date_uploaded = snippet['publishedAt']
    video.restricted = None
    for label, thumbnail in snippet['thumbnails'].items():
        video.thumbnails.append(
            VideoThumbnail(
                url=thumbnail['url'],
                width=None,
                height=None))
    return video