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))
def delete_sub_not_in_list(subs): delete_channels = db_session.query(Channel).filter( ~Channel.id.in_(subs)).all() for channel in delete_channels: if channel.subscribed or channel.subscribed is None: channel.subscribed = False create_logger(__name__).warning( "Setting unsubscribed for channel: {} - {}".format( channel.title, channel.__dict__)) stmt = update_channel_from_remote(channel) engine.execute(stmt)
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
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
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