Ejemplo n.º 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
Ejemplo n.º 2
0
def get_video_data(id, fetch_all_videos=True, fetch_metadata=False):
    """Return video data from youtube api as playlist of one."""
    data = _ooyala_feed('assets', id)
    video = Video(
        source_videoid=data['embed_code'],
        source_listid=None,
        source_username=None,
        date_published=_parse_datetime(data['updated_at']),
        title=data['name'],
        duration=data['duration'] / 1000,
    )
    video.source_date_uploaded = _parse_datetime(data['created_at'])
    video.restricted = bool(data['time_restrictions'])
    update_thumbnails(video)
    if fetch_metadata:
        video.meta = _ooyala_feed('assets', id, 'metadata')
        video.category = video.meta.get('category', None)
    return Videolist(1, [video])
Ejemplo n.º 3
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