Exemplo n.º 1
0
def add_subscription_local(youtube_auth, channel_id, by_username=False):
    """
    Add a YouTube subscription (Local/DB).
    :param by_username:
    :param youtube_auth:
    :param channel_id:
    :return:
    """
    # FIXME: Somewhat duplicate code of get_remote_subscriptions, move to own function -- START
    # Get ID of uploads playlist
    # channel_uploads_playlist_id = get_channel_uploads_playlist_id(youtube_auth, channel_id)
    if by_username:
        channel_response = get_channel_by_username(youtube_auth, channel_id)
    else:
        channel_response = get_channel_by_id(youtube_auth, channel_id)
    channel_uploads_playlist_id = channel_response['contentDetails'][
        'relatedPlaylists']['uploads']
    channel = Channel(channel_response,
                      channel_uploads_playlist_id,
                      channel_list_response=True)
    db_channel = engine_execute_first(get_channel_by_id_stmt(channel))
    if db_channel:
        engine_execute(update_channel_from_remote(channel))
    else:
        # TODO: change to sqlalchemy core stmt
        create_logger(__name__ + ".subscriptions").info(
            "Added channel {} - {}".format(channel.title, channel.id))
        db_session.add(channel)

    db_session.commit()
    # FIXME: Somewhat duplicate code of get_remote_subscriptions, move to own function -- END

    logger.info("Added subscription (Local/DB): {} / {}".format(
        channel_id, channel.title))
Exemplo n.º 2
0
def add_subscription_remote(channel_id):
    """
    Add a YouTube subscription (On YouTube).

    DEPRECATED: Google doesn't let you, see supported op table https://developers.google.com/youtube/v3/getting-started
    :param youtube_oauth:
    :param channel_id:
    :return: returns response or raises exception
    """
    youtube_oauth = load_youtube()
    response = youtube_oauth.subscriptions().insert(
        part='snippet',
        body=dict(snippet=dict(resourceId=dict(channelId=channel_id))))
    try:
        response.execute()
    except HttpError as exc_http:
        _msg = "Failed adding subscription to '{}', HTTP Error {}".format(
            channel_id, exc_http.resp.status)
        logger.error("{}: {}".format(_msg, exc_http.content),
                     exc_info=exc_http)
        raise exc_http

    except Exception as exc:
        _msg = "Unhandled exception occurred when adding subscription to '{}'".format(
            channel_id)
        logger.critical("{} | response={}".format(_msg, response.__dict__),
                        exc_info=exc)
        raise exc

    # FIXME: Somewhat duplicate code of get_remote_subscriptions, move to own function -- START
    # Get ID of uploads playlist
    channel_uploads_playlist_id = response['items'][0]['contentDetails'][
        'relatedPlaylists']['uploads']
    channel = Channel(channel_id, channel_uploads_playlist_id)
    db_channel = engine_execute_first(get_channel_by_id_stmt(channel))
    if db_channel:
        engine_execute(update_channel_from_remote(channel))
        # subs.append(channel)
    else:
        # TODO: change to sqlalchemy core stmt
        create_logger(__name__ + ".subscriptions").info(
            "Added channel {} - {}".format(channel.title, channel.id))
        db_session.add(channel)
        # subs.append(channel)

    db_session.commit()
    # FIXME: Somewhat duplicate code of get_remote_subscriptions, move to own function -- END

    logger.info("Added subscription: {} / {}".format(
        channel_id, response['snippet']['title']))
    return response
Exemplo n.º 3
0
def get_remote_subscriptions(youtube_oauth):
    """
    Get a list of the authenticated user's subscriptions.
    :param youtube_oauth:
    :param stats:
    :param info: debug lite
    :param debug:
    :param traverse_pages:
    :return: [total, subs, statistics]
    """
    subscription_list_request = youtube_oauth.subscriptions().list(
        part='snippet', mine=True, maxResults=50)
    subs = []
    # Retrieve the list of subscribed channels for authenticated user's channel.
    update_stmts = []
    channel_ids = []
    while subscription_list_request:
        subscription_list_response = subscription_list_request.execute()

        # Grab information about each subscription page
        for page in tqdm(subscription_list_response['items'],
                         desc="Adding and updating channels by page",
                         disable=read_config('Debug', 'disable_tqdm')):
            # Get channel
            channel_response = channels_list(
                youtube_oauth,
                part='contentDetails',
                id=page['snippet']['resourceId']['channelId'])

            # Get ID of uploads playlist
            channel_uploads_playlist_id = channel_response['items'][0][
                'contentDetails']['relatedPlaylists']['uploads']
            channel = Channel(page['snippet'], channel_uploads_playlist_id)
            db_channel = engine_execute_first(get_channel_by_id_stmt(channel))
            if db_channel:
                engine_execute(update_channel_from_remote(channel))
                subs.append(channel)
            else:
                # TODO: change to sqlalchemy core stmt
                create_logger(__name__ + ".subscriptions").info(
                    "Added channel {} - {}".format(channel.title, channel.id))
                db_session.add(channel)
                subs.append(channel)
            channel_ids.append(channel.id)
        subscription_list_request = youtube_oauth.playlistItems().list_next(
            subscription_list_request, subscription_list_response)
    delete_sub_not_in_list(channel_ids)
    db_session.commit()
    return subs
Exemplo n.º 4
0
 def new_download_tile(self, new_tile):
     lock.acquire()
     result = db_session.query(DBDownloadTile).filter(
         DBDownloadTile.video_id == format(
             new_tile.video.video_id)).first()
     if not result:
         download_tile = DBDownloadTile(new_tile)
         if not download_tile.video:
             self.logger.error("No video in new tile: {}".format(
                 download_tile.__dict__),
                               exc_info=True)
             return
         db_session.add(download_tile)
         db_session.commit()
     db_session.remove()
     lock.release()
Exemplo n.º 5
0
def get_remote_subscriptions(youtube_oauth):
    """
    Get a list of the authenticated user's subscriptions.
    :param youtube_oauth:
    :return: [subs]
    """
    if youtube_oauth is None:
        logger.critical("YouTube API OAuth object was NoneType, aborting!")
        return None
    subscription_list_request = youtube_oauth.subscriptions().list(part='snippet', mine=True,
                                                                   maxResults=50)
    subs = []
    # Retrieve the list of subscribed channels for authenticated user's channel.
    channel_ids = []
    while subscription_list_request:
        subscription_list_response = subscription_list_request.execute()

        # Grab information about each subscription page
        for page in subscription_list_response['items']:
            # Get channel
            channel_response = channels_list(youtube_oauth, part='contentDetails',
                                             id=page['snippet']['resourceId']['channelId'])

            # Get ID of uploads playlist
            channel_uploads_playlist_id = channel_response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
            channel = Channel(page['snippet'], channel_uploads_playlist_id)
            db_channel = engine_execute_first(get_channel_by_id_stmt(channel))
            if db_channel:
                engine_execute(update_channel_from_remote(channel))
                subs.append(channel)
            else:
                # TODO: change to sqlalchemy core stmt
                create_logger(__name__ + ".subscriptions").info(
                    "Added channel {} - {}".format(channel.title, channel.id))
                db_session.add(channel)
                subs.append(channel)
            channel_ids.append(channel.id)
        subscription_list_request = youtube_oauth.playlistItems().list_next(
            subscription_list_request, subscription_list_response)
    delete_sub_not_in_list(channel_ids)
    db_session.commit()
    return subs
Exemplo n.º 6
0
def run_channels_test():
    logger.info('Running Channels Test')
    subscriptions = get_subscriptions(cached_subs)
    youtube_keys = load_keys(len(subscriptions))
    test_threads = []
    results = []
    logger.info("Channels Test: Starting miss and pages tests")
    for subscription, youtube_key in (subscriptions, youtube_keys):
        test = RunTestsThreaded(subscription, youtube_key, results)
        test.start()
        test_threads.append(test)

    logger.info("Channels Test: Waiting for test threads")
    for thread in test_threads:
        thread.join()

    for result in results:
        test = Test(result[0], result[1], result[2], result[3])
        db_session.add(test)
    db_session.commit()
    db_session.remove()