Beispiel #1
0
    def channel_videos(self, locale, channelid):
        offset, limit = request.args.get('start', 0), request.args.get('size', 20)
        order_by_position = request.args.get('position', 'f')

        vs = VideoSearch(locale)
        vs.add_term('channel', [channelid])
        if not order_by_position == 't':
            vs.add_sort('position', 'asc')
        vs.date_sort('desc')
        vs.add_sort('video.date_published', 'desc')
        vs.set_paging(offset, limit)

        ctx = {
            'videos': [],
            'image_cdn': app.config['IMAGE_CDN'],
            'referrer': request.args.get('referrer', request.referrer),
            'url': request.url,
            'path': request.path,
            'position': order_by_position,
        }

        for video in vs.results():
            c = {}
            c['id'] = video.id
            c['title'] = video.title
            try:
                c['date_added'] = video.date_added[:10]
            except TypeError:
                c['date_added'] = video.date_added.isoformat()[:10]
            c['thumbnail_url'] = video.video.thumbnail_url
            c['explanation'] = video.__dict__['_meta']['explanation']
            c['duration'] = video.video.duration
            c['source'] = Source.id_to_label(video.video.source)
            c['source_id'] = video.video.source_id
            c['subscriber_count'] = video.subscriber_count
            c['gbcount'] = video.locales['en-gb']['view_count']
            c['uscount'] = video.locales['en-us']['view_count']
            c['gbstarcount'] = video.locales['en-gb']['star_count']
            c['usstarcount'] = video.locales['en-us']['star_count']
            ctx['videos'].append(c)

        cs = ChannelSearch(locale)
        cs.add_id(channelid)
        channel = cs.channels()[0]
        ctx['channel'] = channel
        ctx['video_count'] = vs.total

        return self.render('admin/ranking.html', **ctx)
Beispiel #2
0
    def _format_results(self, videos, with_channels=True, with_stars=False, add_tracking=None):
        vlist = []
        channel_list = set()
        IMAGE_CDN = app.config.get('IMAGE_CDN', '')
        BASE_URL = url_for('basews.discover')

        def _format_user_data(user):
            return dict(
                id=user.resource_url.lstrip('/').split('/')[1],
                display_name=user.display_name,
                resource_url=urljoin(BASE_URL, user.resource_url),
                avatar_thumbnail_url=urljoin(IMAGE_CDN, user.avatar) if user.avatar else ''
            )

        for pos, v in enumerate(videos, self.paging[0]):
            published = v.video.date_published

            video = dict(
                id=v.id,
                channel=dict(
                    id=v.channel,
                    title=v.channel_title),
                title=v.title,
                label=v.label,
                date_added=_format_datetime(v.date_added),
                public=v.public,
                category='',
                video=dict(
                    id=v.video.id,
                    view_count=sum(l['view_count'] for l in v['locales'].values()),
                    star_count=sum(l['star_count'] for l in v['locales'].values()),
                    source=Source.id_to_label(v.video.source),
                    source_id=v.video.source_id,
                    source_username=v.video.source_username,
                    source_date_uploaded=published.isoformat() if hasattr(published, 'isoformat') else published,
                    duration=v.video.duration,
                    description=Video.cleaned_description(v.video.description),
                    thumbnail_url=urljoin(app.config.get('IMAGE_CDN', ''), v.video.thumbnail_url) if v.video.thumbnail_url else '',
                ),
                position=pos,
                child_instance_count=getattr(v, 'child_instance_count', 0)
            )
            video['video'].update(Video.extra_meta(v.video))
            if v.link_url:
                video['video'].update(link_url=v.link_url, link_title=v.link_title)
            if v.owner:
                video['channel']['owner'] = _format_user_data(v.owner)
            if v.owner and v.channel:
                video['channel']['resource_url'] = urljoin(BASE_URL, url_for('userws.channel_info', userid=video['channel']['owner']['id'], channelid=v.channel))
            if v.original_channel_owner:
                video['original_channel_owner'] = _format_user_data(v.original_channel_owner)

            if app.config.get('DOLLY'):
                video.update({
                    "comments": {
                        "total": getattr(v.comments, 'count', 0)
                    }
                })

            if v.category:
                video['category'] = max(v.category) if isinstance(v.category, list) else v.category
            if with_stars:
                video['recent_user_stars'] = v.get('recent_user_stars', [])
            if add_tracking:
                add_tracking(video)

            channel_list.add(v.channel)
            vlist.append(video)

        if with_channels and channel_list:
            ch = ChannelSearch(self.locale)
            ch.add_id(channel_list)
            channel_map = {c['id']: c for c in ch.channels(with_owners=True)}
            self.add_channels_to_videos(vlist, channel_map)

        return vlist