From a484b189894ff387868deccf20bde7a58e5026c3 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 1 Sep 2024 16:02:07 +0200 Subject: [PATCH] feat: cache license info for better efficiency Introduced caching for license data retrieved from the MediaWiki API. This avoids redundant API requests by storing the license information for each base URL. Enhances performance and reduces the risk of hitting request limits or encountering errors due to multiple API calls. --- src/wikimore/app.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/wikimore/app.py b/src/wikimore/app.py index ddd7449..0ba41ea 100644 --- a/src/wikimore/app.py +++ b/src/wikimore/app.py @@ -116,6 +116,7 @@ def get_wikimedia_projects() -> ( app.wikimedia_projects, app.languages = get_wikimedia_projects() +app.licenses = {} logger.debug( f"Loaded {len(app.wikimedia_projects)} Wikimedia projects and {len(app.languages)} languages" @@ -426,19 +427,21 @@ def wiki_article( processed_html = str(body) - # Get license information from the article - mediawiki_api_request = urllib.request.Request( - f"{base_url}/w/rest.php/v1/page/{escape(quote(title.replace(" ", "_")), True)}", - headers=HEADERS, - ) + # Get license information for the article + if base_url not in app.licenses: + try: + mediawiki_api_request = urllib.request.Request( + f"{base_url}/w/rest.php/v1/page/{escape(quote(title.replace(" ", "_")), True)}", + headers=HEADERS, + ) + mediawiki_api_response = urllib.request.urlopen(mediawiki_api_request) + mediawiki_api_data = json.loads(mediawiki_api_response.read().decode()) + app.licenses[base_url] = license = mediawiki_api_data["license"] + except Exception as e: + license = None - try: - with urllib.request.urlopen(mediawiki_api_request) as response: - data = json.loads(response.read().decode()) - license = data["license"] - except Exception as e: - logger.error(f"Error fetching license information: {e}") - license = None + else: + license = app.licenses[base_url] # Render the article return render_template(