Subscriptions: autocheck after subscribing to channel
This commit is contained in:
parent
0a590c3364
commit
c393031ac5
@ -79,29 +79,37 @@ def with_open_db(function, *args, **kwargs):
|
|||||||
with connection as cursor:
|
with connection as cursor:
|
||||||
return function(cursor, *args, **kwargs)
|
return function(cursor, *args, **kwargs)
|
||||||
|
|
||||||
|
def _is_subscribed(cursor, channel_id):
|
||||||
|
result = cursor.execute('''SELECT EXISTS(
|
||||||
|
SELECT 1
|
||||||
|
FROM subscribed_channels
|
||||||
|
WHERE yt_channel_id=?
|
||||||
|
LIMIT 1
|
||||||
|
)''', [channel_id]).fetchone()
|
||||||
|
return bool(result[0])
|
||||||
|
|
||||||
def is_subscribed(channel_id):
|
def is_subscribed(channel_id):
|
||||||
if not os.path.exists(database_path):
|
if not os.path.exists(database_path):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
return with_open_db(_is_subscribed, channel_id)
|
||||||
|
|
||||||
|
def _subscribe(channels):
|
||||||
|
''' channels is a list of (channel_id, channel_name) '''
|
||||||
|
channels = list(channels)
|
||||||
with open_database() as connection:
|
with open_database() as connection:
|
||||||
with connection as cursor:
|
with connection as cursor:
|
||||||
result = cursor.execute('''SELECT EXISTS(
|
channel_ids_to_check = [channel[0] for channel in channels if not _is_subscribed(cursor, channel[0])]
|
||||||
SELECT 1
|
|
||||||
FROM subscribed_channels
|
|
||||||
WHERE yt_channel_id=?
|
|
||||||
LIMIT 1
|
|
||||||
)''', [channel_id]).fetchone()
|
|
||||||
return bool(result[0])
|
|
||||||
|
|
||||||
|
rows = ( (channel_id, channel_name, 0, 0) for channel_id, channel_name in channels)
|
||||||
|
cursor.executemany('''INSERT OR IGNORE INTO subscribed_channels (yt_channel_id, channel_name, time_last_checked, next_check_time)
|
||||||
|
VALUES (?, ?, ?, ?)''', rows)
|
||||||
|
|
||||||
def _subscribe(cursor, channels):
|
if settings.autocheck_subscriptions:
|
||||||
''' channels is a list of (channel_id, channel_name) '''
|
# important that this is after the changes have been committed to database
|
||||||
|
# otherwise the autochecker (other thread) tries checking the channel before it's in the database
|
||||||
channels = ( (channel_id, channel_name, 0, 0) for channel_id, channel_name in channels)
|
channel_names.update(channels)
|
||||||
|
check_channels_if_necessary(channel_ids_to_check)
|
||||||
cursor.executemany('''INSERT OR IGNORE INTO subscribed_channels (yt_channel_id, channel_name, time_last_checked, next_check_time)
|
|
||||||
VALUES (?, ?, ?, ?)''', channels)
|
|
||||||
|
|
||||||
|
|
||||||
def delete_thumbnails(to_delete):
|
def delete_thumbnails(to_delete):
|
||||||
for thumbnail in to_delete:
|
for thumbnail in to_delete:
|
||||||
@ -611,7 +619,7 @@ def import_subscriptions():
|
|||||||
else:
|
else:
|
||||||
return '400 Bad Request: Unsupported file format: ' + mime_type + '. Only subscription.json files (from Google Takeouts) and XML OPML files exported from Youtube\'s subscription manager page are supported', 400
|
return '400 Bad Request: Unsupported file format: ' + mime_type + '. Only subscription.json files (from Google Takeouts) and XML OPML files exported from Youtube\'s subscription manager page are supported', 400
|
||||||
|
|
||||||
with_open_db(_subscribe, channels)
|
_subscribe(channels)
|
||||||
|
|
||||||
return flask.redirect(util.URL_ORIGIN + '/subscription_manager', 303)
|
return flask.redirect(util.URL_ORIGIN + '/subscription_manager', 303)
|
||||||
|
|
||||||
@ -757,7 +765,7 @@ def post_subscriptions_page():
|
|||||||
if action == 'subscribe':
|
if action == 'subscribe':
|
||||||
if len(request.values.getlist('channel_id')) != len(request.values.getlist('channel_name')):
|
if len(request.values.getlist('channel_id')) != len(request.values.getlist('channel_name')):
|
||||||
return '400 Bad Request, length of channel_id != length of channel_name', 400
|
return '400 Bad Request, length of channel_id != length of channel_name', 400
|
||||||
with_open_db(_subscribe, zip(request.values.getlist('channel_id'), request.values.getlist('channel_name')))
|
_subscribe(zip(request.values.getlist('channel_id'), request.values.getlist('channel_name')))
|
||||||
|
|
||||||
elif action == 'unsubscribe':
|
elif action == 'unsubscribe':
|
||||||
with_open_db(_unsubscribe, request.values.getlist('channel_id'))
|
with_open_db(_unsubscribe, request.values.getlist('channel_id'))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user