Don't spam database with opening and closing when getting tags for many channels

This commit is contained in:
James Taylor 2019-06-08 02:35:21 -07:00
parent c8be729e6b
commit f5c76462d7

View File

@ -125,14 +125,12 @@ def _remove_tags(channel_ids, tags):
def _get_tags(channel_id): def _get_tags(cursor, channel_id):
with open_database() as connection: return [row[0] for row in cursor.execute('''SELECT tag
with connection as cursor: FROM tag_associations
return [row[0] for row in cursor.execute('''SELECT tag WHERE sql_channel_id = (
FROM tag_associations SELECT id FROM subscribed_channels WHERE yt_channel_id = ?
WHERE sql_channel_id = ( )''', (channel_id,))]
SELECT id FROM subscribed_channels WHERE yt_channel_id = ?
)''', (channel_id,))]
def _get_all_tags(): def _get_all_tags():
with open_database() as connection: with open_database() as connection:
@ -253,13 +251,15 @@ sub_list_item_template = Template('''
def get_subscription_manager_page(env, start_response): def get_subscription_manager_page(env, start_response):
sub_list_html = '' sub_list_html = ''
for channel_name, channel_id in _get_subscribed_channels(): with open_database() as connection:
sub_list_html += sub_list_item_template.substitute( with connection as cursor:
channel_url = util.URL_ORIGIN + '/channel/' + channel_id, for channel_name, channel_id in _get_subscribed_channels():
channel_name = html.escape(channel_name), sub_list_html += sub_list_item_template.substitute(
channel_id = channel_id, channel_url = util.URL_ORIGIN + '/channel/' + channel_id,
tags = ', '.join(_get_tags(channel_id)), channel_name = html.escape(channel_name),
) channel_id = channel_id,
tags = ', '.join(_get_tags(cursor, channel_id)),
)