Skip to content

Commit

Permalink
wikipedia: Split description retrieval and say() function
Browse files Browse the repository at this point in the history
  • Loading branch information
SnoopJ committed Dec 29, 2022
1 parent 146cf26 commit 08ccaa5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions sopel/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ def mw_section(server, query, section):


def say_commons_image(bot, trigger, image):
desc = mw_commons_image_description(image)

if desc:
bot.say(desc, truncation=" […]")


def mw_commons_image_description(image):
"""
Retrieves the Wikimedia Commons description for the given image
"""
params = "&".join([
"action=query",
"prop=imageinfo",
Expand All @@ -293,21 +303,21 @@ def say_commons_image(bot, trigger, image):
pages = query_data["pages"]

if pageids == [-1] or len(pageids) > 1:
bot.say("no lol")
return False
# We either got no results, or multiple results, time to bail out
return None
else:
page = pages[pageids[0]]

raw_desc = page["imageinfo"][0]["extmetadata"]["ImageDescription"]["value"]

parser = WikiParser(image)
parser.feed(description)
parser.feed(raw_desc)
desc = parser.get_result()
desc = ' '.join(desc.split()) # collapse multiple whitespace chars

bot.say(desc, truncation=" […]")
return desc
except LookupError:
return False
return None


# Matches a wikipedia page (excluding spaces and #, but not /File: links), with a separate optional field for the section
Expand Down

0 comments on commit 08ccaa5

Please sign in to comment.