Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions montage/rdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
DoesNotExist,
get_env_name,
load_default_series,
get_commons_description,
js_isoparse)

from .imgutils import make_mw_img_url
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions montage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ''