perf: fetch once and reuse account_id

This commit is contained in:
Ayo Ayco 2025-01-19 12:24:58 +01:00
parent a4d012ef51
commit 78c4e8469a
2 changed files with 15 additions and 11 deletions

View file

@ -2,14 +2,15 @@ from mastodon import Mastodon
from . import utils from . import utils
session_id = None session_id = None
account_id = None
def get_account_tagged_statuses(app, tag): def get_account_tagged_statuses(app, tag):
global account_id
mastodon = initialize_client(app) mastodon = initialize_client(app)
account = mastodon.me()
statuses = [] statuses = []
try: try:
statuses = mastodon.account_statuses( statuses = mastodon.account_statuses(
id=account.id, id=account_id,
tagged=tag, tagged=tag,
exclude_reblogs=True exclude_reblogs=True
) )
@ -20,6 +21,7 @@ def get_account_tagged_statuses(app, tag):
def initialize_client(app): def initialize_client(app):
global session_id global session_id
global account_id
mastodon = None mastodon = None
secret = None secret = None
try: try:
@ -62,5 +64,15 @@ def initialize_client(app):
else: else:
print('>>> Reused session: ', session_id) 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 return mastodon

View file

@ -74,16 +74,8 @@ async def tag(id):
attribution = get_attribution() attribution = get_attribution()
app = get_app_config() app = get_app_config()
statuses = mastodon.get_account_tagged_statuses(app, id) statuses = mastodon.get_account_tagged_statuses(app, id)
tags = []
masto = mastodon.initialize_client(app) return render_template('_tag.html', threads=statuses, tag=id, app=app, attribution=attribution, render_date=datetime.now())
# 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())
@threads.route('/<path:id>') @threads.route('/<path:id>')