taxref: show a random photo if any are available

This commit is contained in:
dece 2022-05-19 14:34:41 +02:00
parent 85ce2902a7
commit 7a2e011272

View file

@ -1,3 +1,4 @@
import random
import urllib.parse import urllib.parse
import requests import requests
@ -71,3 +72,23 @@ class TaxrefPlugin(Plugin):
cd_ref=item_to_use["referenceId"], cd_ref=item_to_use["referenceId"],
) )
self.bot.say(target, reply) self.bot.say(target, reply)
# If there are media available, show one!
m_url = item_to_use.get("_links", {}) .get("media", {}) .get("href")
if not m_url:
return
response = requests.get(m_url)
if response.status_code != 200:
self.signal_failure(target)
return
media_data = response.json()
items = media_data.get("_embedded", {}).get("media", [])
if not items:
return
random_item = random.choice(items)
media_href = random_item.get("_links", {}).get("file", {}).get("href")
if not media_href:
return
self.bot.say(target, "📷 " + media_href)