Ejemplo n.º 1
0
 def secure_discover(self):
     result = _discover_response()
     if g.authorized:
         result.update(dict(
             user=url_for('userws.own_user_info', userid=g.authorized.userid),
             subscription_updates=url_for('userws.recent_videos', userid=g.authorized.userid),
         ))
     return result
Ejemplo n.º 2
0
def share_redirect_root():
    channel, video = request.args.get('c'), request.args.get('v')
    if not channel:
        abort(404)
    url = url_for('channel', slug='-', channelid=channel)
    if video:
        url += '?video=' + video
    share_url = url_for('share_redirect_root') + '?c=%s&v=%s' % (channel, video)
    head_only = _is_share_bot()
    return _share_redirect(locals())
Ejemplo n.º 3
0
def oembed():
    # See http://oembed.com/
    if request.args.get('format', 'json').lower() != 'json':
        return JsonReponse(dict(message="json format requred"), 501)

    try:
        endpoint, url_args = url_to_endpoint(request.args['url'])
        assert endpoint == 'embed', 'Invalid url'
        video_data = embed(url_args['contentid'])['video_data']
    except NotFound:
        return JsonReponse(dict(message='Not found'), 404)
    except Exception as e:
        return JsonReponse(dict(message=e.message), 400)
    else:
        embed_url = url_for(endpoint, **url_args)

    iframe_html = ('<iframe src="%s" width="1280" height="720" ' +
                   'frameborder="0" allowfullscreen></iframe>') % embed_url

    data = dict(
        version='1.0',
        type='video',
        title=video_data['title'],
        description=video_data['video']['description'],
        thumbnail_url=video_data['video']['thumbnail_url'],
        duration=video_data['video']['duration'],
        provider_name='Wonder PL',
        provider_url='http://wonderpl.com/',
        width=1280,
        height=720,
        html=iframe_html,
    )
    return JsonReponse(data)
Ejemplo n.º 4
0
 def get_resource_url(self, own=False):
     return url_for(
         "userws.video_instance_comment_item",
         userid="-",
         channelid="-",
         videoid=self.video_instance,
         commentid=self.id,
     )
Ejemplo n.º 5
0
 def wrapper(*args, **kwargs):
     response = func(*args, **kwargs)
     proxy_url = url_for('iframe_proxy')
     params = dict(result=json.dumps(response))
     callback = request.args.get('_callback') or request.form.get('_callback')
     if callback:
         params['_callback'] = callback
     return redirect(proxy_url + '?' + urlencode(params))
Ejemplo n.º 6
0
def share_redirect(linkid):
    link = ShareLink.query.get_or_404(linkid)
    is_bot = _is_share_bot()
    data = link.process_redirect(increment_click_count=not is_bot)
    data.update(
        head_only=is_bot,
        share_url=url_for('share_redirect', linkid=linkid),
    )
    return _share_redirect(data)
Ejemplo n.º 7
0
 def _ping_hub(self, mode):
     callback_url = url_for('pubsubhubbubws.callback')
     data = {
         'hub.callback': callback_url + '?id=' + str(self.id),
         'hub.mode': mode,
         'hub.topic': self.topic,
         'hub.verify': 'async',
         'hub.lease_seconds': LEASE_SECONDS,
         'hub.verify_token': self.verify_token,
         'hub.secret': self.secret,
     }
     response = requests.post(self.hub, data=data)
     response.raise_for_status()
     return response
Ejemplo n.º 8
0
def send_password_reset(userid):
    user = User.query.get(userid)
    if not user.email:
        app.logger.warning("Can't reset password for %s: no email address", user.id)
        return

    token = create_access_token(user.id, '', 86400)
    url = url_for('reset_password') + '?token=' + token
    template = email_env.get_template('reset.html')
    body = template.render(
        reset_link=url,
        user=user,
        email_sender=app.config['DEFAULT_EMAIL_SOURCE'],
    )
    send_email(user.email, body)
Ejemplo n.º 9
0
def web_channel_data(channelid, load_video=None):
    channel_data = ws_request('/ws/-/channels/%s/' % channelid, size=40)
    selected_video = None
    if load_video:
        for instance in channel_data['videos']['items']:
            if instance['id'] == load_video:
                selected_video = instance
        # Not in the first 40 - try fetching separately:
        if not selected_video:
            try:
                video_data = ws_request(
                    '/ws/-/channels/%s/videos/%s/' % (channelid, load_video))
            except NotFound:
                pass
            else:
                if 'error' not in video_data:
                    selected_video = video_data
    channel_data['canonical_url'] = url_for(
        'channel', slug=slugify(channel_data['title']) or '-', channelid=channelid)
    if selected_video:
        channel_data['canonical_url'] += '?video=' + selected_video['id']
    return dict(channel_data=channel_data, selected_video=selected_video, api_urls=json.dumps(ws_request('/ws/')))
Ejemplo n.º 10
0
 def process_redirect(self, increment_click_count=True):
     """Construct redirect url for link and record the click."""
     channel = Channel.query.with_entities(Channel.id, Channel.title)
     if self.object_type == 'video_instance':
         channel = channel.filter(
             (VideoInstance.channel == Channel.id) &
             (VideoInstance.id == self.object_id))
         params = dict(video=self.object_id)
     else:
         channel = channel.filter_by(id=self.object_id)
         params = dict()
     # TODO: Do something more friendly than throw a 404?
     channelid, title = channel.first_or_404()
     url = url_for('channel', slug=slugify(title) or '-', channelid=channelid)
     if params:
         # TODO: add utm tracking params for google analytics
         url += '?' + urlencode(params)
     if increment_click_count:
         ShareLink.increment_click_count(self.id)
     return {'url': url,
             'channel': channelid,
             'video': params.get('video', None),
             'user': self.user}
Ejemplo n.º 11
0
def embed(contentid):
    if app.config.get('DOLLY'):
        ctx = dict(
            canonical_url=url_for('embed', contentid=contentid),
            use_inline_scripts=request.args.get('inline', '')[:1] not in ('f', '0'),
        )
        if contentid.startswith('vi'):
            video_data = ws_request('/ws/-/channels/-/videos/%s/' % contentid)
        else:
            try:
                int(contentid)
            except Exception:
                abort(404)
            else:
                video_data = romeo_ws_request('/api/v/%s' % contentid)
        # XXX: Need to factor this out to somewhere cleaner
        if request.is_secure:
            video_data['video']['thumbnail_url'] =\
                video_data['video']['thumbnail_url'].\
                replace('http://ak.c.ooyala', 'https://ec.c.ooyala')
        return dict(video_data=video_data, **ctx)
    else:
        videoid = request.args.get('video', None)
        return web_channel_data(contentid, load_video=videoid)
Ejemplo n.º 12
0
def injector():
    return dict(iframe_url=url_for('bookmarklet')), 200, {'Content-Type': 'application/javascript'}
Ejemplo n.º 13
0
@expose_web('/welcome_email', cache_age=3600)
def welcome_email():
    from rockpack.mainsite.core.email import env
    return env.get_template('welcome.html').render(web=True)


@expose_web('/', 'web/home.html', cache_age=3600)
def homepage():
    api_urls = json.dumps(ws_request('/ws/'))
    channels = ws_request('/ws/channels/', size=8)
    return dict(api_urls=api_urls, injectorUrl=url_for('injector'), top_channels=channels)

if app.config.get('SECURE_SUBDOMAIN'):
    app.add_url_rule('/', 'secure_home_redirect',
                     lambda: redirect(url_for('homepage')), subdomain=app.config['SECURE_SUBDOMAIN'])

if app.config.get('ADMIN_SUBDOMAIN'):
    app.add_url_rule('/', 'admin_redirect',
                     lambda: redirect('/admin/'), subdomain=app.config['ADMIN_SUBDOMAIN'])


@expose_web('/wonder-discover', 'web/home.html', cache_age=3600)
def wonder_discover():
    return dict()


@expose_web('/fullweb', 'web/fullweb.html', cache_age=3600)
def fullweb():
    if app.config.get('ENABLE_FULLWEB', False):
        api_urls = json.dumps(ws_request('/ws/'))
Ejemplo n.º 14
0
    def _format_results(self, users, include_promo=False, add_tracking=None):
        user_list = range(self.paging[1])
        IMAGE_CDN = app.config.get('IMAGE_CDN', '')
        BASE_URL = url_for('basews.discover')

        position = 0
        for user in users:
            u = dict(
                id=user.id,
                username=user.username,
                display_name=user.display_name,
                resource_url=urljoin(BASE_URL, user.resource_url),
                avatar_thumbnail_url=urljoin(IMAGE_CDN, user.avatar_thumbnail_url) if user.avatar_thumbnail_url else '',
                profile_cover_url=urljoin(IMAGE_CDN, user.profile_cover_url) if user.profile_cover_url else '',
                description=user.description or "",
                subscriber_count=user.subscriber_count,
                subscription_count=user.subscription_count,
                promotion=user.promotion
                # categories=getattr(user, 'category', []) or []
            )

            if user.brand:
                u.update(
                    brand=True,
                    site_url=user.site_url,
                )

            has_promotion = False
            for p in user.promotion or []:
                if p.startswith(self._promotion_prefix):
                    has_promotion = True
                    break

            if self.promoted_category is not None and has_promotion:
                # All these should be at the top so we have all the
                # promoted slots filled before we process the non
                # promoted ones
                try:
                    promo_position = self._get_position(u)
                except UserSearch.PositionOutsideOffsetError:
                    # We don't want to include this user
                    pass
                else:
                    if promo_position is not None:
                        u['position'] = promo_position
                        user_list[promo_position] = u
            else:
                promo_position = None
                position = self._check_position(user_list, position, self.paging[1] - 1)
                u['position'] = position + self.paging[0]
                user_list[position] = u

                # Incrementing the counter for
                # non-promoted channels
                position += 1

            if add_tracking:
                add_tracking(u, extra=('promoted-%d' % promo_position)
                             if promo_position is not None else None)

        # We may have empty positions so lets strip these
        user_list = filter(lambda x: not isinstance(x, int), user_list)

        if getattr(self, '_real_paging', False):
            # XXX: promotion hack - return the actual
            # amount requested initially
            user_list = user_list[self._real_paging[0]:self._real_paging[0] + self._real_paging[1]]

        return user_list
Ejemplo n.º 15
0
 def get_resource_url(self, own=False):
     view = "userws.owner_channel_info" if own else "userws.channel_info"
     return url_for(view, userid=self.owner, channelid=self.id)
Ejemplo n.º 16
0
 def _item_link(self, endpoint, id, label, param='id'):
     return ('<a href="%s?%s=%s">%s</a>' % (url_for(endpoint), param, id, label)).encode('utf8')
Ejemplo n.º 17
0
 def old_share_redirect(linkid):
     return redirect(url_for('share_redirect', linkid=linkid), 301)
Ejemplo n.º 18
0
 def get_resource_url(self, own=False):
     view = 'userws.delete_cover_art_item' if own else 'userws.redirect_cover_art_item'
     return url_for(view, userid=self.owner, ref=self.cover)
Ejemplo n.º 19
0
def _format_video_queue_thumbnail(view, context, item, name):
    t = u'<a href="%s?id=%s"><img src="%s" width="160" height="90"/></a>'
    return Markup(t % (url_for('video_instance.edit_view'), item.source_instance, item.source_instance_rel.default_thumbnail))
Ejemplo n.º 20
0
 def get_resource_url(self, own=False):
     view = 'userws.delete_subscription_item'
     return url_for(view, userid=self.user, channelid=self.channel)
Ejemplo n.º 21
0
 def get_resource_url(self, own=True):
     return url_for('userws.get_flag_item', userid=self.user, flag=self.flag)
Ejemplo n.º 22
0
 def get_resource_url(self, own=False):
     view = 'userws.own_user_info' if own else 'userws.user_info'
     return url_for(view, userid=self.id)
Ejemplo n.º 23
0
 def get_resource_url(self, own=True):
     return url_for('userws.get_external_account', userid=self.user, id=self.id)
Ejemplo n.º 24
0
 def get_resource_url(self, own=False):
     return url_for("userws.channel_video_instance", userid="-", channelid=self.channel, videoid=self.id)
Ejemplo n.º 25
0
    def _format_results(self, channels, with_owners=False, with_videos=False, video_paging=(0, 100, ), add_tracking=None):
        channel_list = range(self.paging[1])  # We don't know the channel size so default to paging
        channel_id_list = []
        owner_list = set()
        IMAGE_CDN = app.config.get('IMAGE_CDN', '')
        BASE_URL = url_for('basews.discover')

        def _check_position(position, max_check):
            if position > max_check:
                return None

            if not isinstance(channel_list[position], int):
                position += 1
                position = _check_position(position, max_check)
            return position

        position = 0
        for channel in channels:
            ch = dict(
                id=channel.id,
                owner=channel.owner,
                category=channel.category,
                subscriber_count=channel.subscriber_count,
                description=channel.description,
                title=channel.title,
                date_added=_format_datetime(channel.date_added),
                date_published=_format_datetime(channel.date_published),
                public=channel.public,
                cover=dict(
                    thumbnail_url=urljoin(IMAGE_CDN, channel.cover.thumbnail_url) if channel.cover.thumbnail_url else '',
                    aoi=channel.cover.aoi
                ),
                videos=dict(total=channel.video_count)
            )
            if channel.favourite:
                if channel.title == 'Watch Later':
                    ch['watchlater'] = True
                else:
                    ch['favourites'] = True
            if channel.verified:
                ch['verified'] = True

            for k, v in channel.iteritems():
                if isinstance(v, (str, unicode)) and k.endswith('_url'):
                    url = v
                    if url:
                        if k == 'resource_url':
                            url = urljoin(BASE_URL, v)
                        elif k != 'ecommerce_url':
                            url = urljoin(IMAGE_CDN, v)
                    ch[k] = url

                if k == 'category':
                    if not channel[k]:
                        ch[k] = None
                    elif isinstance(channel[k], list):
                        ch[k] = int(channel[k][0])  # First item is subcat
                    else:
                        ch[k] = int(channel[k])

            if with_owners:
                owner_list.add(channel.owner)
            if with_videos:
                channel_id_list.append(channel.id)

            # ASSUMPTION: We should have all the promoted channels at the top, so positions of
            # the promoted will already be known by the time we're at the regular channels.
            # (lets also hope this assumption isn't anyones mother)
            if self.promoted_category is not None and channel.promotion:
                promote_pattern = '|'.join([str(self.locale), str(self.promoted_category)]) + '|'
                # Could be a promoted channel
                # for a different category
                promoted_for_category = False

                for p in channel.promotion:
                    if p.startswith(promote_pattern):
                        promoted_for_category = True
                        locale, category, pos = p.split('|')
                        pos = int(pos) - 1  # position isn't zero indexed, so adjust
                        if pos < self.paging[0]:
                            # Don't include channels where its position
                            # is less than the offset position
                            continue

                        # Calculate new offseted position and assign
                        ch['position'] = pos - self.paging[0]
                        channel_list[ch['position']] = ch
                        if add_tracking:
                            add_tracking(ch, 'promoted-%d' % pos)

                if promoted_for_category:
                    continue

            position = _check_position(position, self.paging[1] - 1)
            ch['position'] = position + self.paging[0]
            channel_list[position] = ch
            if add_tracking:
                add_tracking(ch)

            # Start incrementing the counter for
            # non-promoted channels
            position += 1

        # A bug in promotions means that
        # we may have an empty position or so.
        # We usually won't hit this, but it's a
        # shit hack, so fix it.
        if position < len(channel_list):
            new_list = []
            for channel in channel_list:
                if not isinstance(channel, int):
                    new_list.append(channel)
            channel_list = new_list

        if with_owners and owner_list:
            self._fetch_and_attach_owners(channel_list, owner_list)

        if with_videos and channel_id_list:
            self._fetch_and_attach_videos(channel_list, channel_id_list, video_paging)

        if getattr(self, '_real_paging', False):
            # XXX: promotion hack - return the actual
            # amount requested initially
            channel_list = channel_list[self._real_paging[0]:self._real_paging[0] + self._real_paging[1]]
        return channel_list
Ejemplo n.º 26
0
 def get_resource_url(self, own=False):
     return url_for('share_redirect', linkid=self.id)
Ejemplo n.º 27
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
Ejemplo n.º 28
0
def homepage():
    api_urls = json.dumps(ws_request('/ws/'))
    channels = ws_request('/ws/channels/', size=8)
    return dict(api_urls=api_urls, injectorUrl=url_for('injector'), top_channels=channels)
Ejemplo n.º 29
0
def _discover_response():
    locale = request.args.get('locale', app.config['ENABLED_LOCALES'][0])
    return dict(
        categories=url_for('categoryws.category_list', locale=locale),
        popular_channels=url_for('channelws.channel_list', locale=locale),
        popular_videos=url_for('videows.video_list', locale=locale),
        channel_search=url_for('searchws.search_channels', locale=locale),
        channel_search_terms=url_for('completews.complete_channel_terms', locale=locale),
        video_search=url_for('searchws.search_videos', locale=locale),
        video_search_terms=url_for('completews.complete_video_terms', locale=locale),
        cover_art=url_for('coverartws.rockpack_cover_art', locale=locale),
        login=url_for('loginws.login'),
        register=url_for('registrationws.register'),
        login_register_external=url_for('loginws.external'),
        reset_password=url_for('resetws.reset_password'),
        refresh_token=url_for('tokenws.token'),
        share_url=url_for('sharews.create_share_link'),
        user_search=url_for('searchws.search_users'),
        base_api=url_for('basews.discover'),
    )