From 8addac4c4c2783e93616ae6a45c659a1a8cd47e1 Mon Sep 17 00:00:00 2001 From: Ayush Shukla Date: Wed, 29 Apr 2026 04:51:19 +0530 Subject: [PATCH] fix report generation crash and pull descriptions from commons api --- montage/rdb.py | 8 ++++++-- montage/utils.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/montage/rdb.py b/montage/rdb.py index b7052ff6..aac7672d 100644 --- a/montage/rdb.py +++ b/montage/rdb.py @@ -46,6 +46,7 @@ DoesNotExist, get_env_name, load_default_series, + get_commons_description, js_isoparse) from .imgutils import make_mw_img_url @@ -2203,10 +2204,13 @@ def alias_jurors(juror_ranking_map): winners = [] for fer in ranking_list: - # TODO: get entry description from commons cur = {} cur['ranking'] = fer.rank - cur['entry'] = fer.entry.to_dict() # TODO (need desc, etc.) + + # fix: replace broken to_dict() with to_details_dict() and fetch actual description from commons api + entry_dict = fer.entry.to_details_dict(with_uploader=True) + entry_dict['description'] = get_commons_description(entry_dict['name']) + cur['entry'] = entry_dict if not juror_alias_map: jrm = fer.juror_ranking_map diff --git a/montage/utils.py b/montage/utils.py index 99433060..963afe69 100644 --- a/montage/utils.py +++ b/montage/utils.py @@ -265,3 +265,38 @@ def process_weighted_choices(wcp): def fast_weighted_choice(nsw, values): return values[bisect.bisect(nsw, random.random()) - 1] + +def get_commons_description(filename): + """ + Fetch the description of a file from Wikimedia Commons API + """ + # clean up File: prefix if present + if filename.startswith('File:'): + filename = filename[5:] + + url = "https://commons.wikimedia.org/w/api.php" + params = { + 'action': 'query', + 'prop': 'imageinfo', + 'iiprop': 'extmetadata', + 'titles': 'File:' + filename, + 'format': 'json' + } + + try: + resp = requests_get(url, params=params) + data = resp.json() + pages = data.get('query', {}).get('pages', {}) + for page_id, page_info in pages.items(): + if 'imageinfo' in page_info: + ext_meta = page_info['imageinfo'][0].get('extmetadata', {}) + desc_obj = ext_meta.get('ImageDescription', {}) + desc_val = desc_obj.get('value', '') + if desc_val: + return desc_val + except Exception as e: + # hack to ignore API errors silently for now lol + pass + + return '' +