From d637f5b29c2153b0e4fde2f7183230100b2f6309 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 10 Jun 2019 02:29:55 -0700 Subject: Group by tags feature and improve appearance of sub list items --- youtube/subscriptions.py | 101 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 85 insertions(+), 16 deletions(-) (limited to 'youtube') diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index d43c8fd..7728c0c 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -151,6 +151,21 @@ def _get_channel_names(channel_ids): return result +def _channels_with_tag(cursor, tag, order=False): + ''' returns list of (channel_id, channel_name) ''' + + statement = '''SELECT yt_channel_id, channel_name + FROM subscribed_channels + WHERE subscribed_channels.id IN ( + SELECT tag_associations.sql_channel_id FROM tag_associations WHERE tag=? + ) + ''' + if order: + statement += '''ORDER BY channel_name''' + + return cursor.execute(statement, [tag]).fetchall() + + units = { 'year': 31536000, # 365*24*3600 'month': 2592000, # 30*24*3600 @@ -281,11 +296,8 @@ def check_tags(tags): with open_database() as connection: with connection as cursor: for tag in tags: - channel_id_name_list += cursor.execute('''SELECT yt_channel_id, channel_name - FROM subscribed_channels - WHERE subscribed_channels.id IN ( - SELECT tag_associations.sql_channel_id FROM tag_associations WHERE tag=? - )''', [tag]).fetchall() + channel_id_name_list += _channels_with_tag(cursor, tag) + channel_names.update(channel_id_name_list) check_channels_if_necessary([item[0] for item in channel_id_name_list]) @@ -354,30 +366,87 @@ def import_subscriptions(env, start_response): sub_list_item_template = Template('''
  • + $channel_name $tags -
  • ''') +tag_group_template = Template(''' +
  • +

    $tag

    +
      +$sub_list +
    +
  • +''') def get_subscription_manager_page(env, start_response): - - sub_list_html = '' with open_database() as connection: with connection as cursor: - for channel_name, channel_id in _get_subscribed_channels(): - sub_list_html += sub_list_item_template.substitute( - channel_url = util.URL_ORIGIN + '/channel/' + channel_id, - channel_name = html.escape(channel_name), - channel_id = channel_id, - tags = ', '.join(_get_tags(cursor, channel_id)), - ) + if env['parameters'].get('group_by_tags', '0')[0] == '1': + + sort_name = "Don't group" + sort_link = util.URL_ORIGIN + '/subscription_manager' + + main_list_html = '' + + else: + + sort_name = "Group by tags" + sort_link = util.URL_ORIGIN + '/subscription_manager?group_by_tags=1' + + main_list_html = '
      ' + for channel_name, channel_id in _get_subscribed_channels(): + main_list_html += sub_list_item_template.substitute( + channel_url = util.URL_ORIGIN + '/channel/' + channel_id, + channel_name = html.escape(channel_name), + channel_id = channel_id, + tags = ', '.join(_get_tags(cursor, channel_id)), + ) + main_list_html += '
    ' start_response('200 OK', [('Content-type','text/html'),]) return subscription_manager_template.substitute( header = html_common.get_header(), - sub_list = sub_list_html, + main_list = main_list_html, + sort_name = sort_name, + sort_link = sort_link, page_buttons = '', ).encode('utf-8') -- cgit v1.2.3