def supercharge(self): data = (request.form or request.args).copy() # Ugly reverse mapping of source labels source = data.get('source') if source: for id, label in Source.get_form_choices(): if source == label: data['source'] = id form = ImportForm(data, csrf_enabled=False) form.source.choices = list(Source.get_form_choices()) form.category.choices = [(-1, '')] +\ list(Category.get_form_choices(data.get('locale') or 'en-us', True)) errors = {} if form.validate(): if form.category.data < 0: errors.update({'category': 'Missing category'}) elif not form.user.data: errors.update({'user': '******'}) elif not form.channel.data: errors.update({'channel': 'Missing channel'}) elif form.type.data != 'video': errors.update({'type': 'Type should be Video'}) elif int(form.source.data) != 1: errors.update({'source': 'Source should be YouTube'}) else: supercharge(request.form['title'], form.category.data, form.channel.data, form.user.data, form.id.data) return jsonify([]), 202 return jsonify({'error': form.errors or errors}), 400
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)
def update_romeo_videos(data): vdata = data['video'] assert vdata['source'] == 'ooyala' source = Source.label_to_id(vdata['source']) # update existing or create new key = dict(source=source, source_videoid=vdata['source_id']) video = Video.query.filter_by(**key).first() or Video(**key).add() video.title = data['title'] video.description = vdata['description'] video.duration = vdata['duration'] video.date_published = datetime.strptime( vdata['source_date_uploaded'][:19], '%Y-%m-%dT%H:%M:%S') video.source_username = vdata['source_username'] video.link_url = vdata['link_url'] video.link_title = vdata['link_title'] video.category = data['category'] video.thumbnails = [VideoThumbnail(**t) for t in data['thumbnails']]
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
def _update_channel_videos(channel, data): playlist = youtube.parse_atom_playlist_data(data) source = Source.label_to_id('youtube') Video.add_videos(playlist.videos, source) channel.add_videos(playlist.videos)
def index(self): ctx = {} data = (request.form or request.args).copy() # Ugly reverse mapping of source labels source = data.get('source') if source: for id, label in Source.get_form_choices(): if source == label: data['source'] = id form = ImportForm(data, csrf_enabled=False) form.source.choices = list(Source.get_form_choices()) form.category.choices = [(-1, '')] +\ list(Category.get_form_choices(data.get('locale') or 'en-us', True)) user_form = UserForm(data, csrf_enabled=False) ctx['user_form'] = user_form ctx['form'] = form # pre-populate from parameters if request.args.get('tag'): form.tags.data = ','.join(request.args.getlist('tag')) if request.args.get('user'): user = list(User.query.filter_by(username=request.args.get('user')).values('id', 'username')) if user: form.user.data = user[0][0] else: form.user.data = "" if request.args.get('channeltitle'): channel = list(Channel.query.filter_by( title=request.args.get('channeltitle'), owner=form.user.data).values('id', 'title')) if channel: form.channel.data = channel[0][0] else: form.channel.data = '_new:' + request.args.get('channeltitle') if request.args.get('categoryname'): for choice in form.category.choices: if choice[1] == request.args.get('categoryname'): form.category.data = choice[0] if 'source' in data: if form.commit.data and not form.user.data: if user_form.username.data: if user_form.validate(): # update import form with new user form.user.data = self._create_user(user_form) else: return self.render('admin/import.html', **ctx) else: # User and channel will be created from source data assert form.channel.data == '' if form.validate(): if form.commit.data: count, channel = self._import_videos(form) if channel and channel.id: url = '%s?id=%s' % (url_for('channel.edit_view'), channel.id) else: url = url_for('video.index_view') if form.type.data == 'playlist': url += '?flt0_0=' + form.id.data flash('Imported %d videos' % count) return redirect(url) else: ctx['import_preview'] = form.import_data form.commit.data = 'true' return self.render('admin/import.html', **ctx)