From 78c4e8469a038b52936775c29eca6e9b1047f8f4 Mon Sep 17 00:00:00 2001 From: Ayo Ayco Date: Sun, 19 Jan 2025 12:24:58 +0100 Subject: [PATCH] perf: fetch once and reuse account_id --- mastodon.py | 16 ++++++++++++++-- threads.py | 10 +--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/mastodon.py b/mastodon.py index 349f891..2b147c7 100644 --- a/mastodon.py +++ b/mastodon.py @@ -2,14 +2,15 @@ from mastodon import Mastodon from . import utils session_id = None +account_id = None def get_account_tagged_statuses(app, tag): + global account_id mastodon = initialize_client(app) - account = mastodon.me() statuses = [] try: statuses = mastodon.account_statuses( - id=account.id, + id=account_id, tagged=tag, exclude_reblogs=True ) @@ -20,6 +21,7 @@ def get_account_tagged_statuses(app, tag): def initialize_client(app): global session_id + global account_id mastodon = None secret = None try: @@ -62,5 +64,15 @@ def initialize_client(app): else: print('>>> Reused session: ', session_id) + if account_id == None: + try: + account = mastodon.me() + account_id = account.id + print('>>> Set account ID: ', account_id) + except: + message = '>>> Failed to get mastodon account' + raise Exception(message) + else: + print('>>> Reused account ID:', account_id) return mastodon \ No newline at end of file diff --git a/threads.py b/threads.py index 77c97bd..8c12829 100755 --- a/threads.py +++ b/threads.py @@ -74,16 +74,8 @@ async def tag(id): attribution = get_attribution() app = get_app_config() statuses = mastodon.get_account_tagged_statuses(app, id) - tags = [] - masto = mastodon.initialize_client(app) - - # List featured hashtags - tags = masto.featured_tags() - - - - return render_template('_tag.html', threads=statuses, tag=id, app=app, tags=tags, attribution=attribution, render_date=datetime.now()) + return render_template('_tag.html', threads=statuses, tag=id, app=app, attribution=attribution, render_date=datetime.now()) @threads.route('/')