Example #1
0
def channel_edit(channel):
    form = ChannelForm(obj=channel)
    if channel.userid == g.user.userid:
        form.type.choices = [(1, CHANNEL_TYPE[1])]
    else:
        choices = CHANNEL_TYPE.items()
        choices.pop(0)
        choices.pop(0)
        form.type.choices = choices
    if not channel.channel_logo_filename:
        del form.delete_logo
    if form.validate_on_submit():
        old_channel = channel
        form.populate_obj(channel)
        if form.delete_logo and form.delete_logo.data:
            try:
                if old_channel.channel_logo_filename:
                    os.remove(
                        os.path.join(app.static_folder, 'thumbnails',
                                     old_channel.channel_logo_filename))
                    flash(u"Removed channel logo", u"success")
                else:
                    flash(u"Channel doesn't have logo", u"info")
            except OSError:
                flash(u"Channel logo already Removed", u"info")
            channel.channel_logo_filename = None
        else:
            if 'channel_logo' in request.files and request.files[
                    'channel_logo']:
                try:
                    if old_channel.channel_logo_filename:
                        db.session.add(old_channel)
                        try:
                            os.remove(
                                os.path.join(
                                    app.static_folder, 'thumbnails',
                                    old_channel.channel_logo_filename))
                        except OSError:
                            old_channel.channel_logo_filename = None
                            flash(u"Unable to delete previous logo", u"error")
                    message = u"Unable to save image"
                    image = resize_image(request.files['channel_logo'])
                    channel.channel_logo_filename = thumbnails.save(image)
                    message = u"Channel logo uploaded"
                except OSError:
                    flash(message, u"error")
                    channel.channel_logo_filename = None
            else:
                message = u"Edited description for channel"
            flash(message, 'success')
        db.session.commit()
        return render_redirect(channel.url_for(), code=303)
    return render_form(form=form,
                       title=u"Edit channel",
                       submit=u"Save",
                       cancel_url=channel.url_for(),
                       ajax=False)
Example #2
0
def channel_edit(channel):
    form = ChannelForm(obj=channel)
    if channel.userid == g.user.userid:
        form.type.choices = [(1, CHANNEL_TYPE[1])]
    else:
        choices = CHANNEL_TYPE.items()
        choices.pop(0)
        choices.pop(0)
        form.type.choices = choices
    if not channel.channel_logo_filename:
        del form.delete_logo
    if form.validate_on_submit():
        old_channel = channel
        form.populate_obj(channel)
        if form.delete_logo and form.delete_logo.data:
            try:
                if old_channel.channel_logo_filename:
                    os.remove(os.path.join(app.static_folder, 'thumbnails', old_channel.channel_logo_filename))
                    flash(u"Removed channel logo", u"success")
                else:
                    flash(u"Channel doesn't have logo", u"info")
            except OSError:
                flash(u"Channel logo already Removed", u"info")
            channel.channel_logo_filename = None
        else:
            if request.files['channel_logo']:
                try:
                    if old_channel.channel_logo_filename:
                        db.session.add(old_channel)
                        try:
                            os.remove(os.path.join(app.static_folder, 'thumbnails', old_channel.channel_logo_filename))
                        except OSError:
                            old_channel.channel_logo_filename = None
                            flash(u"Unable to delete previous logo", u"error")
                    message = u"Unable to save image"
                    image = resize_image(request.files['channel_logo'])
                    channel.channel_logo_filename = thumbnails.save(image)
                    message = u"Channel logo uploaded"
                except OSError:
                    flash(message, u"error")
                    channel.channel_logo_filename = None
            else:
                message = u"Edited description for channel"
            flash(message, 'success')
        db.session.commit()
        return render_redirect(channel.url_for(), code=303)
    return render_form(form=form, title=u"Edit channel", submit=u"Save",
        cancel_url=channel.url_for(), ajax=False)
Example #3
0
def channel_edit(channel):
    form = ChannelForm(obj=channel)
    if channel.userid == current_auth.user.userid:
        form.type.choices = [(1, CHANNEL_TYPE[1])]
    else:
        choices = CHANNEL_TYPE.items()
        choices.pop(0)
        choices.pop(0)
        form.type.choices = choices
    if not channel.channel_logo_filename:
        del form.delete_logo
    if request.method == 'GET':
        html_form = render_form(form=form, title=_("Edit channel"), submit=_("Save"),
        cancel_url=channel.url_for(), ajax=False, with_chrome=False)
        return {'channel': dict(channel.current_access()), 'form': html_form}
    if form.validate_on_submit():
        old_channel = channel
        form.populate_obj(channel)
        if form.delete_logo and form.delete_logo.data:
            try:
                if old_channel.channel_logo_filename:
                    os.remove(os.path.join(app.static_folder, 'thumbnails', old_channel.channel_logo_filename))
                    message = "Removed channel logo"
            except OSError:
                channel.channel_logo_filename = None
                message = "Channel logo already Removed"
        else:
            if 'channel_logo' in request.files and request.files['channel_logo']:
                try:
                    if old_channel.channel_logo_filename:
                        db.session.add(old_channel)
                        try:
                            os.remove(os.path.join(app.static_folder, 'thumbnails', old_channel.channel_logo_filename))
                        except OSError:
                            old_channel.channel_logo_filename = None
                            message = "Unable to delete previous logo"
                    image = resize_image(request.files['channel_logo'])
                    channel.channel_logo_filename = thumbnails.save(image)
                    message = "Channel logo uploaded"
                except OSError:
                    message = "Unable to save image"
                    channel.channel_logo_filename = None
            else:
                message = "Edited description for channel"
        db.session.commit()
        return {'status': 'ok', 'doc': _(message), 'result': {}}
    return {'status': 'error', 'errors': form.errors}, 400
Example #4
0
def playlist_edit(channel, playlist):
    form = PlaylistForm(obj=playlist)
    form.channel = channel
    if not playlist.banner_ad_filename:
        del form.delete_banner_ad
    message = None
    old_playlist_banner_ad_filename = playlist.banner_ad_filename
    old_playlist_name = playlist.name
    try:
        if form.validate_on_submit():
            form.populate_obj(playlist)
            if not playlist.name:
                playlist.make_name()
            playlist.banner_ad = playlist.banner_image
            if old_playlist_name != playlist.name:
                redirect_to = PlaylistRedirect.query.filter_by(
                    name=old_playlist_name, channel=channel).first()
                if redirect_to:
                    redirect_to.playlist = playlist
                else:
                    redirect_to = PlaylistRedirect(name=old_playlist_name,
                                                   channel=channel,
                                                   playlist=playlist)
                    db.session.add(redirect_to)
            if playlist.banner_ad:
                if playlist.banner_ad_filename != old_playlist_banner_ad_filename:
                    remove_banner_ad(old_playlist_banner_ad_filename)
                flash(u"Added new banner ad", u"success")
                playlist.banner_ad_filename = thumbnails.save(
                    return_werkzeug_filestorage(playlist.banner_ad,
                                                playlist.title))
                message = True
            if form.delete_banner_ad and form.delete_banner_ad.data:
                flash(u"Removed banner ad", u"success")
                message = True
                db.session.add(playlist)
                remove_banner_ad(playlist.banner_ad_filename)
                playlist.banner_ad_filename = None
                playlist.banner_ad_url = ""
            db.session.commit()
            if not message:
                flash(u"Edited playlist '%s'" % playlist.title, 'success')
            return render_redirect(playlist.url_for(), code=303)
    except UploadNotAllowed, e:
        flash(e.message, u'error')
Example #5
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get(
                    'https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json'
                    % video_id)
                if r.json is None:
                    raise DataProcessingError(
                        "Unable to fetch data, please check the youtube url")
                else:
                    if new:
                        video.title = r.json['entry']['title']['$t']
                        video.description = escape(
                            r.json['entry']['media$group']['media$description']
                            ['$t'])
                for item in r.json['entry']['media$group']['media$thumbnail']:
                    if item['yt$name'] == 'mqdefault':
                        thumbnail_url_request = requests.get(item['url'])
                        filestorage = return_werkzeug_filestorage(
                            thumbnail_url_request,
                            filename=secure_filename(
                                r.json['entry']['title']['$t']))
                        video.thumbnail_path = thumbnails.save(filestorage)
                video.video_sourceid = video_id
                video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError(
                    "Supplied youtube URL doesn't contain video information")
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #6
0
                def inner(start_index=1, max_result=50, total=0):
                    """Retireves youtube playlist videos recursively

                    :param start_index: Index to start for fetching videos in playlist
                    :param max_result: Maximum results to return
                    :param total: variable to keep track of total videos fetched
                    """
                    r = requests.get('http://gdata.youtube.com/feeds/api/playlists/%s?v=2&alt=json&max-result=50&start-index=%d' % (playlist_id, start_index))
                    if r.json is None:
                        raise DataProcessingError("Unable to fetch data, please check the youtube url")
                    else:
                        # fetch playlist info
                        playlist.title = r.json['feed']['title']['$t']
                        if 'media$description' in r.json['feed']['media$group']:
                            playlist.description = escape(r.json['feed']['media$group']['media$description']['$t'])
                        for item in r.json['feed'].get('entry', []):
                            # If the video is private still youtube provides the title but doesn't
                            # provide thumbnail & urls, check for private video
                            is_private = item.get('app$control')
                            if is_private is not None and is_private['yt$state']['reasonCode']:
                                continue
                            video = Video(playlist=playlist)
                            video.title = item['title']['$t']
                            video.video_url = item['media$group']['media$player']['url']
                            if 'media$description' in item['media$group']:
                                video.description = escape(item['media$group']['media$description']['$t'])
                            for video_content in item['media$group']['media$thumbnail']:
                                if video_content['yt$name'] == 'mqdefault':
                                    thumbnail_url_request = requests.get(video_content['url'])
                                    filestorage = return_werkzeug_filestorage(thumbnail_url_request,
                                        filename=secure_filename(item['title']['$t']))
                                    video.thumbnail_path = thumbnails.save(filestorage)
                            video.video_sourceid = item['media$group']['yt$videoid']['$t']
                            video.video_source = u"youtube"
                            video.make_name()
                            playlist.videos.append(video)
                        #When no more data is present to retrieve in playlist 'feed' is absent in json
                        if 'entry' in r.json['feed']:
                            total += len(r.json['feed']['entry'])
                            if total <= r.json['feed']['openSearch$totalResults']:
                                # check for empty playlist
                                if not r.json['feed'].get('entry', []):
                                    raise DataProcessingError("Empty Playlist")
                                inner(start_index=total+1, total=total)
Example #7
0
def playlist_edit(channel, playlist):
    form = PlaylistForm(obj=playlist)
    form.channel = channel
    if not playlist.banner_ad_filename:
        del form.delete_banner_ad
    message = None
    old_playlist_banner_ad_filename = playlist.banner_ad_filename
    old_playlist_name = playlist.name
    try:
        if form.validate_on_submit():
            form.populate_obj(playlist)
            if not playlist.name:
                playlist.make_name()
            playlist.banner_ad = playlist.banner_image
            if old_playlist_name != playlist.name:
                redirect_to = PlaylistRedirect.query.filter_by(name=old_playlist_name, channel=channel).first()
                if redirect_to:
                    redirect_to.playlist = playlist
                else:
                    redirect_to = PlaylistRedirect(name=old_playlist_name, channel=channel, playlist=playlist)
                    db.session.add(redirect_to)
            if playlist.banner_ad:
                if playlist.banner_ad_filename != old_playlist_banner_ad_filename:
                    remove_banner_ad(old_playlist_banner_ad_filename)
                flash(u"Added new banner ad", u"success")
                playlist.banner_ad_filename = thumbnails.save(return_werkzeug_filestorage(playlist.banner_ad, playlist.title))
                message = True
            if form.delete_banner_ad and form.delete_banner_ad.data:
                flash(u"Removed banner ad", u"success")
                message = True
                db.session.add(playlist)
                remove_banner_ad(playlist.banner_ad_filename)
                playlist.banner_ad_filename = None
                playlist.banner_ad_url = ""
            db.session.commit()
            if not message:
                flash(u"Edited playlist '%s'" % playlist.title, 'success')
            return render_redirect(playlist.url_for(), code=303)
    except UploadNotAllowed, e:
        flash(e.message, u'error')
Example #8
0
def playlist_edit(channel, playlist):
    form = PlaylistForm(obj=playlist)
    form.channel = channel
    if request.method == 'GET':
        html_form = render_form(form=form, title=_("Edit Playlist"), submit=_("Save"),
            cancel_url=playlist.url_for(), ajax=False, with_chrome=False)
        return {'playlist': dict(playlist.current_access()), 'form': html_form}
    if not playlist.banner_ad_filename:
        del form.delete_banner_ad
    old_playlist_banner_ad_filename = playlist.banner_ad_filename
    old_playlist_name = playlist.name
    try:
        if form.validate_on_submit():
            form.populate_obj(playlist)
            if not playlist.name:
                playlist.make_name()
            playlist.banner_ad = playlist.banner_image
            if old_playlist_name != playlist.name:
                redirect_to = PlaylistRedirect.query.filter_by(name=old_playlist_name, channel=channel).first()
                if redirect_to:
                    redirect_to.playlist = playlist
                else:
                    redirect_to = PlaylistRedirect(name=old_playlist_name, channel=channel, playlist=playlist)
                    db.session.add(redirect_to)
            if playlist.banner_ad:
                if playlist.banner_ad_filename != old_playlist_banner_ad_filename:
                    remove_banner_ad(old_playlist_banner_ad_filename)
                playlist.banner_ad_filename = thumbnails.save(return_werkzeug_filestorage(playlist.banner_ad, playlist.title))
            if form.delete_banner_ad and form.delete_banner_ad.data:
                db.session.add(playlist)
                remove_banner_ad(playlist.banner_ad_filename)
                playlist.banner_ad_filename = None
                playlist.banner_ad_url = ""
            db.session.commit()
            return {'status': 'ok', 'doc': _("Edited playlist {title}.".format(title=playlist.title)), 'result': {'url': playlist.url_for()}}, 200
        return {'status': 'error', 'errors': form.errors}, 400
    except UploadNotAllowed as e:
        return {'status': 'error', 'errors': {'error': [e.message]}}, 400
Example #9
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
                if r.json is None:
                    raise DataProcessingError("Unable to fetch data, please check the youtube url")
                else:
                    if new:
                        video.title = r.json['entry']['title']['$t']
                        video.description = escape(r.json['entry']['media$group']['media$description']['$t'])
                for item in r.json['entry']['media$group']['media$thumbnail']:
                    if item['yt$name'] == 'mqdefault':
                        thumbnail_url_request = requests.get(item['url'])
                        filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(r.json['entry']['title']['$t']))
                        video.thumbnail_path = thumbnails.save(filestorage)
                video.video_sourceid = video_id
                video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("Supplied youtube URL doesn't contain video information")
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #10
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
                jsondata = r.json() if callable(r.json) else r.json
                if jsondata is None:
                    raise DataProcessingError("Unable to fetch data, please check the youtube url")
                else:
                    if new:
                        video.title = jsondata['entry']['title']['$t']
                        video.description = markdown(jsondata['entry']['media$group']['media$description']['$t'])
                for item in jsondata['entry']['media$group']['media$thumbnail']:
                    if item['yt$name'] == 'mqdefault':
                        thumbnail_url_request = requests.get(item['url'])
                        filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata['entry']['title']['$t']))
                        video.thumbnail_path = thumbnails.save(filestorage)
                video.video_sourceid = video_id
                video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("Supplied youtube URL doesn't contain video information")
        elif parsed.netloc in ['vimeo.com', 'www.vimeo.com']:
            try:
                components = parsed.path.split('/')
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Video Id. Example: https://vimeo.com/42595773")
                    r = requests.get("https://vimeo.com/api/v2/video/%s.json" % (video_id))
                    jsondata = r.json() if callable(r.json) else r.json
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the vimeo url")
                    else:
                        if jsondata[0][u'embed_privacy'] != u'anywhere':
                            raise DataProcessingError("Video is not public to import.")
                        if new:
                            video.title, video.description = jsondata[0]['title'], bleach.clean(jsondata[0]['description'], tags=SANITIZE_TAGS, attributes=SANITIZE_ATTRIBUTES)
                        if jsondata[0]['thumbnail_medium']:
                            thumbnail_url_request = requests.get(jsondata[0]['thumbnail_large'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata[0]['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source, video.video_url = video_id, u"vimeo", jsondata[0]['url']
                else:
                    raise DataProcessingError("Invalid Vimeo url. Example: https://vimeo.com/42595773")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("")
        elif parsed.netloc in ["ustream.tv", "www.ustream.tv"]:
            try:
                components = [item for item in parsed.path.split("/") if item != ""]
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Ustream Id. Example: https://www.ustream.tv/channel/6320346")
                    try:
                        r = requests.get("https://api.ustream.tv/json/channel/%s/getInfo" % (components[1]), params={"key": app.config['USTREAM_KEY']})
                    except KeyError:
                        raise DataProcessingError("Ustream Developer key is missing")
                    jsondata = r.json() if callable(r.json) else r.json
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the ustream url")
                    else:
                        if new:
                            video.title, video.description = jsondata['results']['title'], markdown(jsondata['results']['description']) or ""
                        if jsondata['results']['imageUrl']:
                            thumbnail_url_request = requests.get(jsondata['results']['imageUrl']['medium'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata['results']['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source = video_id, u"ustream"
                else:
                    raise DataProcessingError("Invalid ustream url. Example: https://www.ustream.tv/channel/6320346")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError as e:
                raise DataProcessingError(e)
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #11
0
def process_playlist(playlist, playlist_url):
    """
    Get metadata for the playlist from the corresponding site
    """
    # Parse the playlist url
    if playlist_url:
        parsed = urlparse(escape(playlist_url))
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                stream_playlist = playlist.channel.playlist_for_stream(create=True)
                # first two character of playlist id says what type of playlist, ignore them
                playlist_id = parse_qs(parsed.query)['list'][0][2:]
                youtube = build('youtube', 'v3', developerKey=app.config['YOUTUBE_API_KEY'])
                playlistitems_list_request = youtube.playlistItems().list(
                    playlistId=playlist_id,
                    part='snippet',
                    maxResults=50
                )
                playlist_info_request = youtube.playlists().list(
                    id=playlist_id,
                    part='snippet'
                )
                if playlist_info_request:
                    playlist_infos = playlist_info_request.execute()
                    for playlist_info in playlist_infos['items']:
                        playlist.title = playlist.title or playlist_info['snippet']['title']
                        if playlist_info['snippet']['description']:
                            playlist.description = playlist_info['snippet']['description']
                while playlistitems_list_request:
                    playlistitems_list_response = playlistitems_list_request.execute()
                    for playlist_item in playlistitems_list_response['items']:
                        with db.session.no_autoflush:
                            video = Video.query.filter_by(video_source=u'youtube', channel=playlist.channel, video_sourceid=playlist_item['snippet']['resourceId']['videoId']).first()
                        if video:
                            if video not in stream_playlist.videos:
                                stream_playlist.videos.append(video)
                            if video not in playlist.videos:
                                playlist.videos.append(video)
                        else:
                            video = Video(playlist=playlist if playlist is not None else stream_playlist)
                            video.title = playlist_item['snippet']['title']
                            video.video_url = 'https://www.youtube.com/watch?v='+playlist_item['snippet']['resourceId']['videoId']
                            if playlist_item['snippet']['description']:
                                video.description = markdown(playlist_item['snippet']['description'])
                            for thumbnail in playlist_item['snippet']['thumbnails']['medium']:
                                thumbnail_url_request = requests.get(playlist_item['snippet']['thumbnails']['medium']['url'])
                                filestorage = return_werkzeug_filestorage(thumbnail_url_request,
                                    filename=secure_filename(playlist_item['snippet']['title']) or 'name-missing')
                                video.thumbnail_path = thumbnails.save(filestorage)
                            video.video_sourceid = playlist_item['snippet']['resourceId']['videoId']
                            video.video_source = u'youtube'
                            video.make_name()
                            playlist.videos.append(video)
                            with db.session.no_autoflush:
                                if video not in stream_playlist.videos:
                                    stream_playlist.videos.append(video)
                    playlistitems_list_request = youtube.playlistItems().list_next(
                        playlistitems_list_request, playlistitems_list_response)
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("Supplied youtube URL doesn't contain video information")
            except HttpError:
                raise DataProcessingError("HTTPError while parsing YouTube playlist")
        else:
            raise ValueError("Unsupported video site")
    else:
        raise ValueError("Video URL is missing")
Example #12
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get('https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % video_id)
                jsondata = r.json() if callable(r.json) else r.json
                if jsondata is None:
                    raise DataProcessingError("Unable to fetch data, please check the youtube url")
                else:
                    if new:
                        video.title = jsondata['entry']['title']['$t']
                        video.description = markdown(jsondata['entry']['media$group']['media$description']['$t'])
                for item in jsondata['entry']['media$group']['media$thumbnail']:
                    if item['yt$name'] == 'mqdefault':
                        thumbnail_url_request = requests.get(item['url'])
                        filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata['entry']['title']['$t']))
                        video.thumbnail_path = thumbnails.save(filestorage)
                video.video_sourceid = video_id
                video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("Supplied youtube URL doesn't contain video information")
        elif parsed.netloc in ['vimeo.com', 'www.vimeo.com']:
            try:
                components = parsed.path.split('/')
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Video Id. Example: https://vimeo.com/42595773")
                    r = requests.get("https://vimeo.com/api/v2/video/%s.json" % (video_id))
                    jsondata = r.json() if callable(r.json) else r.json
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the vimeo url")
                    else:
                        if jsondata[0][u'embed_privacy'] != u'anywhere':
                            raise DataProcessingError("Video is not public to import.")
                        if new:
                            video.title, video.description = jsondata[0]['title'], bleach.clean(jsondata[0]['description'], tags=SANITIZE_TAGS, attributes=SANITIZE_ATTRIBUTES)
                        if jsondata[0]['thumbnail_medium']:
                            thumbnail_url_request = requests.get(jsondata[0]['thumbnail_large'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata[0]['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source, video.video_url = video_id, u"vimeo", jsondata[0]['url']
                else:
                    raise DataProcessingError("Invalid Vimeo url. Example: https://vimeo.com/42595773")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("")
        elif parsed.netloc in ["ustream.tv", "www.ustream.tv"]:
            try:
                components = [item for item in parsed.path.split("/") if item != ""]
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Ustream Id. Example: https://www.ustream.tv/channel/6320346")
                    try:
                        r = requests.get("https://api.ustream.tv/json/channel/%s/getInfo" % (components[1]), params={"key": app.config['USTREAM_KEY']})
                    except KeyError:
                        raise DataProcessingError("Ustream Developer key is missing")
                    jsondata = r.json() if callable(r.json) else r.json
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the ustream url")
                    else:
                        if new:
                            video.title, video.description = jsondata['results']['title'], markdown(jsondata['results']['description']) or ""
                        if jsondata['results']['imageUrl']:
                            thumbnail_url_request = requests.get(jsondata['results']['imageUrl']['medium'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata['results']['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source = video_id, u"ustream"
                else:
                    raise DataProcessingError("Invalid ustream url. Example: https://www.ustream.tv/channel/6320346")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError as e:
                raise DataProcessingError(e)
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #13
0
                def inner(start_index=1, max_result=50, total=0):
                    """Retireves youtube playlist videos recursively

                    :param start_index: Index to start for fetching videos in playlist
                    :param max_result: Maximum results to return
                    :param total: variable to keep track of total videos fetched
                    """
                    r = requests.get(
                        'http://gdata.youtube.com/feeds/api/playlists/%s?v=2&alt=json&max-result=50&start-index=%d'
                        % (playlist_id, start_index))
                    if r.json is None:
                        raise DataProcessingError(
                            "Unable to fetch data, please check the youtube url"
                        )
                    else:
                        # fetch playlist info
                        playlist.title = r.json['feed']['title']['$t']
                        if 'media$description' in r.json['feed'][
                                'media$group']:
                            playlist.description = escape(
                                r.json['feed']['media$group']
                                ['media$description']['$t'])
                        for item in r.json['feed'].get('entry', []):
                            # If the video is private still youtube provides the title but doesn't
                            # provide thumbnail & urls, check for private video
                            is_private = item.get('app$control')
                            if is_private is not None and is_private[
                                    'yt$state']['reasonCode']:
                                continue
                            video = Video(playlist=playlist)
                            video.title = item['title']['$t']
                            video.video_url = item['media$group'][
                                'media$player']['url']
                            if 'media$description' in item['media$group']:
                                video.description = escape(
                                    item['media$group']['media$description']
                                    ['$t'])
                            for video_content in item['media$group'][
                                    'media$thumbnail']:
                                if video_content['yt$name'] == 'mqdefault':
                                    thumbnail_url_request = requests.get(
                                        video_content['url'])
                                    filestorage = return_werkzeug_filestorage(
                                        thumbnail_url_request,
                                        filename=secure_filename(
                                            item['title']['$t']))
                                    video.thumbnail_path = thumbnails.save(
                                        filestorage)
                            video.video_sourceid = item['media$group'][
                                'yt$videoid']['$t']
                            video.video_source = u"youtube"
                            video.make_name()
                            playlist.videos.append(video)
                        #When no more data is present to retrieve in playlist 'feed' is absent in json
                        if 'entry' in r.json['feed']:
                            total += len(r.json['feed']['entry'])
                            if total <= r.json['feed'][
                                    'openSearch$totalResults']:
                                # check for empty playlist
                                if not r.json['feed'].get('entry', []):
                                    raise DataProcessingError("Empty Playlist")
                                inner(start_index=total + 1, total=total)
Example #14
0
def process_playlist(playlist, playlist_url):
    """
    Get metadata for the playlist from the corresponding site
    """
    # Parse the playlist url
    if playlist_url:
        parsed = urlparse(escape(playlist_url))
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                stream_playlist = playlist.channel.playlist_for_stream(
                    create=True)
                # first two character of playlist id says what type of playlist, ignore them
                playlist_id = parse_qs(parsed.query)['list'][0][2:]
                youtube = build('youtube',
                                'v3',
                                developerKey=app.config['YOUTUBE_API_KEY'])
                playlistitems_list_request = youtube.playlistItems().list(
                    playlistId=playlist_id, part='snippet', maxResults=50)
                playlist_info_request = youtube.playlists().list(
                    id=playlist_id, part='snippet')
                if playlist_info_request:
                    playlist_infos = playlist_info_request.execute()
                    for playlist_info in playlist_infos['items']:
                        playlist.title = playlist.title or playlist_info[
                            'snippet']['title']
                        if playlist_info['snippet']['description']:
                            playlist.description = playlist_info['snippet'][
                                'description']
                while playlistitems_list_request:
                    playlistitems_list_response = playlistitems_list_request.execute(
                    )
                    for playlist_item in playlistitems_list_response['items']:
                        with db.session.no_autoflush:
                            video = Video.query.filter_by(
                                video_source=u'youtube',
                                channel=playlist.channel,
                                video_sourceid=playlist_item['snippet']
                                ['resourceId']['videoId']).first()
                        if video:
                            if video not in stream_playlist.videos:
                                stream_playlist.videos.append(video)
                            if video not in playlist.videos:
                                playlist.videos.append(video)
                        else:
                            video = Video(playlist=playlist if playlist
                                          is not None else stream_playlist)
                            video.title = playlist_item['snippet']['title']
                            video.video_url = 'https://www.youtube.com/watch?v=' + playlist_item[
                                'snippet']['resourceId']['videoId']
                            if playlist_item['snippet']['description']:
                                video.description = markdown(
                                    playlist_item['snippet']['description'])
                            for thumbnail in playlist_item['snippet'][
                                    'thumbnails']['medium']:
                                thumbnail_url_request = requests.get(
                                    playlist_item['snippet']['thumbnails']
                                    ['medium']['url'])
                                filestorage = return_werkzeug_filestorage(
                                    thumbnail_url_request,
                                    filename=secure_filename(
                                        playlist_item['snippet']['title'])
                                    or 'name-missing')
                                video.thumbnail_path = thumbnails.save(
                                    filestorage)
                            video.video_sourceid = playlist_item['snippet'][
                                'resourceId']['videoId']
                            video.video_source = u'youtube'
                            video.make_name()
                            playlist.videos.append(video)
                            with db.session.no_autoflush:
                                if video not in stream_playlist.videos:
                                    stream_playlist.videos.append(video)
                    playlistitems_list_request = youtube.playlistItems(
                    ).list_next(playlistitems_list_request,
                                playlistitems_list_response)
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError(
                    "Supplied youtube URL doesn't contain video information")
            except HttpError:
                raise DataProcessingError(
                    "HTTPError while parsing YouTube playlist")
        else:
            raise ValueError("Unsupported video site")
    else:
        raise ValueError("Video URL is missing")
Example #15
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get(
                    'https://www.googleapis.com/youtube/v3/videos?part=snippet&id={video_id}&key={api_key}'
                    .format(video_id=video_id,
                            api_key=app.config['YOUTUBE_API_KEY']))
                try:
                    jsondata = r.json()
                except ValueError as e:
                    app.logger.error(
                        "Error while fetching video details\n\nError: {error}\nResponse body: {response}"
                        .format(error=e.message, response=r.text))
                    raise DataProcessingError(
                        "Unable to parse video data, please try after sometime"
                    )
                if jsondata is None or len(jsondata['items']) == 0:
                    raise DataProcessingError(
                        "Unable to fetch data, please check the youtube url")
                else:
                    jsondata = jsondata['items'][0]
                    if new:
                        video.title = jsondata['snippet']['title']
                        video.description = markdown(
                            jsondata['snippet']['description'])
                    thumbnail_url_request = requests.get(
                        jsondata['snippet']['thumbnails']['medium']['url'])
                    filestorage = return_werkzeug_filestorage(
                        thumbnail_url_request,
                        filename=secure_filename(video.title))
                    video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid = video_id
                    video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError(
                    "Supplied youtube URL doesn't contain video information")
        elif parsed.netloc in ['vimeo.com', 'www.vimeo.com']:
            try:
                components = parsed.path.split('/')
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError(
                            "Invalid Video Id. Example: https://vimeo.com/42595773"
                        )
                    r = requests.get("https://vimeo.com/api/v2/video/%s.json" %
                                     (video_id))
                    jsondata = r.json()
                    if jsondata is None:
                        raise DataProcessingError(
                            "Unable to fetch, please check the vimeo url")
                    else:
                        if jsondata[0][u'embed_privacy'] != u'anywhere':
                            raise DataProcessingError(
                                "Video is not public to import.")
                        if new:
                            video.title, video.description = jsondata[0][
                                'title'], bleach.clean(
                                    jsondata[0]['description'],
                                    tags=SANITIZE_TAGS,
                                    attributes=SANITIZE_ATTRIBUTES)
                        if jsondata[0]['thumbnail_medium']:
                            thumbnail_url_request = requests.get(
                                jsondata[0]['thumbnail_large'])
                            filestorage = return_werkzeug_filestorage(
                                thumbnail_url_request,
                                filename=secure_filename(jsondata[0]['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source, video.video_url = video_id, u"vimeo", jsondata[
                        0]['url']
                else:
                    raise DataProcessingError(
                        "Invalid Vimeo url. Example: https://vimeo.com/42595773"
                    )
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("")
        elif parsed.netloc in ["ustream.tv", "www.ustream.tv"]:
            try:
                components = [
                    item for item in parsed.path.split("/") if item != ""
                ]
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError(
                            "Invalid Ustream Id. Example: https://www.ustream.tv/channel/6320346"
                        )
                    try:
                        r = requests.get(
                            "https://api.ustream.tv/json/channel/%s/getInfo" %
                            (components[1]),
                            params={"key": app.config['USTREAM_KEY']})
                    except KeyError:
                        raise DataProcessingError(
                            "Ustream Developer key is missing")
                    jsondata = r.json()
                    if jsondata is None:
                        raise DataProcessingError(
                            "Unable to fetch, please check the ustream url")
                    else:
                        if new:
                            video.title, video.description = jsondata[
                                'results']['title'], markdown(
                                    jsondata['results']['description']) or ""
                        if jsondata['results']['imageUrl']:
                            thumbnail_url_request = requests.get(
                                jsondata['results']['imageUrl']['medium'])
                            filestorage = return_werkzeug_filestorage(
                                thumbnail_url_request,
                                filename=secure_filename(
                                    jsondata['results']['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source = video_id, u"ustream"
                else:
                    raise DataProcessingError(
                        "Invalid ustream url. Example: https://www.ustream.tv/channel/6320346"
                    )
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError as e:
                raise DataProcessingError(e)
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #16
0
def process_video(video, new=False):
    """
    Get metadata for the video from the corresponding site
    """
    # Parse the video url
    if video.video_url:
        parsed = urlparse(video.video_url)
        # Check video source and get corresponding data
        if parsed.netloc in ['youtube.com', 'www.youtube.com']:
            try:
                video_id = parse_qs(parsed.query)['v'][0]
                r = requests.get('https://www.googleapis.com/youtube/v3/videos?part=snippet&id={video_id}&key={api_key}'.format(
                    video_id=video_id, api_key=app.config['YOUTUBE_API_KEY']
                    ))
                try:
                    jsondata = r.json()
                except ValueError as e:
                    app.logger.error("Error while fetching video details\n\nError: {error}\nResponse body: {response}".format(
                        error=e.message, response=r.text))
                    raise DataProcessingError("Unable to parse video data, please try after sometime")
                if jsondata is None or len(jsondata['items']) == 0:
                    raise DataProcessingError("Unable to fetch data, please check the youtube url")
                else:
                    jsondata = jsondata['items'][0]
                    if new:
                        video.title = jsondata['snippet']['title']
                        video.description = markdown(jsondata['snippet']['description'])
                    thumbnail_url_request = requests.get(jsondata['snippet']['thumbnails']['medium']['url'])
                    filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(video.title))
                    video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid = video_id
                    video.video_source = u"youtube"
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("Supplied youtube URL doesn't contain video information")
        elif parsed.netloc in ['vimeo.com', 'www.vimeo.com']:
            try:
                components = parsed.path.split('/')
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Video Id. Example: https://vimeo.com/42595773")
                    r = requests.get("https://vimeo.com/api/v2/video/%s.json" % (video_id))
                    jsondata = r.json()
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the vimeo url")
                    else:
                        if jsondata[0][u'embed_privacy'] != u'anywhere':
                            raise DataProcessingError("Video is not public to import.")
                        if new:
                            video.title, video.description = jsondata[0]['title'], bleach.clean(jsondata[0]['description'], tags=SANITIZE_TAGS, attributes=SANITIZE_ATTRIBUTES)
                        if jsondata[0]['thumbnail_medium']:
                            thumbnail_url_request = requests.get(jsondata[0]['thumbnail_large'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata[0]['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source, video.video_url = video_id, u"vimeo", jsondata[0]['url']
                else:
                    raise DataProcessingError("Invalid Vimeo url. Example: https://vimeo.com/42595773")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError:
                raise DataProcessingError("")
        elif parsed.netloc in ["ustream.tv", "www.ustream.tv"]:
            try:
                components = [item for item in parsed.path.split("/") if item != ""]
                if len(components) == 2:
                    try:
                        video_id = int(components[-1])
                    except ValueError:
                        raise ValueError("Invalid Ustream Id. Example: https://www.ustream.tv/channel/6320346")
                    try:
                        r = requests.get("https://api.ustream.tv/json/channel/%s/getInfo" % (components[1]), params={"key": app.config['USTREAM_KEY']})
                    except KeyError:
                        raise DataProcessingError("Ustream Developer key is missing")
                    jsondata = r.json()
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch, please check the ustream url")
                    else:
                        if new:
                            video.title, video.description = jsondata['results']['title'], markdown(jsondata['results']['description']) or ""
                        if jsondata['results']['imageUrl']:
                            thumbnail_url_request = requests.get(jsondata['results']['imageUrl']['medium'])
                            filestorage = return_werkzeug_filestorage(thumbnail_url_request, filename=secure_filename(jsondata['results']['title']))
                            video.thumbnail_path = thumbnails.save(filestorage)
                    video.video_sourceid, video.video_source = video_id, u"ustream"
                else:
                    raise DataProcessingError("Invalid ustream url. Example: https://www.ustream.tv/channel/6320346")
            except requests.ConnectionError:
                raise DataProcessingError("Unable to establish connection")
            except gaierror:
                raise DataProcessingError("Unable to resolve the hostname")
            except KeyError as e:
                raise DataProcessingError(e)
        else:
            raise ValueError("Unsupported video site")

    else:
        raise ValueError("Video URL is missing")
Example #17
0
                def inner(start_index=1, max_result=50, total=0):
                    """Retireves youtube playlist videos recursively

                    :param start_index: Index to start for fetching videos in playlist
                    :param max_result: Maximum results to return
                    :param total: variable to keep track of total videos fetched
                    """
                    r = requests.get('http://gdata.youtube.com/feeds/api/playlists/%s?v=2&alt=json&max-result=50&start-index=%d' % (playlist_id, start_index))
                    jsondata = r.json() if callable(r.json) else r.json
                    if jsondata is None:
                        raise DataProcessingError("Unable to fetch data, please check the youtube url")
                    else:
                        # fetch playlist info
                        # prevent overwriting title during Extend playlist
                        playlist.title = playlist.title or jsondata['feed']['title']['$t']
                        if 'media$description' in jsondata['feed']['media$group']:
                            playlist.description = markdown(jsondata['feed']['media$group']['media$description']['$t'])
                        for item in jsondata['feed'].get('entry', []):
                            if item.get('app$control', {}).get('yt$state', {}).get('reasonCode'):  # Is it private?
                                continue
                            videos = Video.query.filter_by(video_source=u"youtube", video_sourceid=item['media$group']['yt$videoid']['$t']).all()
                            if videos:
                                # If video isn't present in current playlist, copy the video parameters
                                if not filter(lambda video: video.playlist == playlist, videos):
                                    new_video = Video(playlist=playlist if playlist is not None else stream_playlist)
                                    video = videos[0]
                                    new_video.name = video.name
                                    new_video.title = video.title
                                    new_video.video_url = video.video_url
                                    new_video.description = markdown(video.description)
                                    new_video.thumbnail_path = video.thumbnail_path
                                    new_video.video_source = u"youtube"
                                    new_video.video_sourceid = video.video_sourceid
                                    playlist.videos.append(new_video)
                                    if new_video not in stream_playlist.videos:
                                        stream_playlist.videos.append(new_video)
                            else:
                                video = Video(playlist=playlist if playlist is not None else stream_playlist)
                                video.title = item['title']['$t']
                                video.video_url = item['media$group']['media$player']['url']
                                if 'media$description' in item['media$group']:
                                    video.description = markdown(item['media$group']['media$description']['$t'])
                                for video_content in item['media$group']['media$thumbnail']:
                                    if video_content['yt$name'] == 'mqdefault':
                                        thumbnail_url_request = requests.get(video_content['url'])
                                        filestorage = return_werkzeug_filestorage(thumbnail_url_request,
                                            filename=secure_filename(item['title']['$t']) or 'name-missing')
                                        video.thumbnail_path = thumbnails.save(filestorage)
                                video.video_sourceid = item['media$group']['yt$videoid']['$t']
                                video.video_source = u"youtube"
                                video.make_name()
                                playlist.videos.append(video)
                                if video not in stream_playlist.videos:
                                    stream_playlist.videos.append(video)
                        #When no more data is present to retrieve in playlist 'feed' is absent in json
                        if 'entry' in jsondata['feed']:
                            total += len(jsondata['feed']['entry'])
                            if total <= jsondata['feed']['openSearch$totalResults']:
                                # check for empty playlist
                                if not jsondata['feed'].get('entry', []):
                                    raise DataProcessingError("Empty Playlist")
                                inner(start_index=total + 1, total=total)