2020-03-01 21:40:31 +02:00
|
|
|
import os
|
2020-03-15 18:38:17 +02:00
|
|
|
import re
|
2020-03-01 21:40:31 +02:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from nio import AsyncClient, UploadError
|
|
|
|
from nio import UploadResponse
|
|
|
|
|
|
|
|
from modules.common.module import BotModule
|
|
|
|
|
|
|
|
|
|
|
|
class Apod:
|
|
|
|
def __init__(self, title, explanation, date, hdurl, media_type, url):
|
|
|
|
self.hdurl = hdurl
|
|
|
|
self.title = title
|
|
|
|
self.explanation = explanation
|
|
|
|
self.date = date
|
|
|
|
self.media_type = media_type
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_from_json(json):
|
|
|
|
return Apod(json.get("title"), json.get("explanation"), json.get("date"), json.get("hdurl"),
|
|
|
|
json.get("media_type"), json.get("url"))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "title: {} || explanation: {} || date: {} || original-url: {}".format(self.title,
|
|
|
|
self.explanation,
|
|
|
|
self.date,
|
|
|
|
self.hdurl)
|
|
|
|
|
|
|
|
|
|
|
|
class MatrixModule(BotModule):
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
super().__init__(name)
|
|
|
|
self.api_key = os.getenv("APOD_API_KEY", "DEMO_KEY")
|
2020-03-15 19:49:24 +02:00
|
|
|
self.update_api_urls()
|
2020-03-15 18:38:17 +02:00
|
|
|
self.matrix_uri_cache = dict()
|
|
|
|
self.APOD_DATE_PATTERN = r"^\d\d\d\d-\d\d-\d\d$"
|
2020-03-01 21:40:31 +02:00
|
|
|
|
2020-03-15 19:49:24 +02:00
|
|
|
def update_api_urls(self):
|
|
|
|
self.apod_api_url = f"https://api.nasa.gov/planetary/apod?api_key={self.api_key}&hd=true"
|
|
|
|
self.apod_by_date_api_url = self.apod_api_url + "&date="
|
|
|
|
|
2020-03-01 21:40:31 +02:00
|
|
|
async def matrix_message(self, bot, room, event):
|
|
|
|
self.logger.debug(f"room: {room.name} sender: {event.sender} wants latest astronomy picture of the day")
|
|
|
|
|
2020-03-15 18:38:17 +02:00
|
|
|
args = event.body.split()
|
|
|
|
|
|
|
|
if len(args) == 1:
|
|
|
|
await self.send_apod(bot, room, self.apod_api_url)
|
2020-03-15 19:49:24 +02:00
|
|
|
elif len(args) == 2:
|
2020-03-15 19:11:00 +02:00
|
|
|
if args[1] == "stats":
|
|
|
|
await self.send_stats(bot, room)
|
|
|
|
elif args[1] == "clear":
|
|
|
|
bot.must_be_admin(room, event)
|
|
|
|
await self.clear_uri_cache(bot, room)
|
|
|
|
elif args[1] == "help":
|
|
|
|
await self.command_help(bot, room)
|
2020-03-15 18:38:17 +02:00
|
|
|
else:
|
2020-03-15 19:11:00 +02:00
|
|
|
date = args[1]
|
|
|
|
if re.match(self.APOD_DATE_PATTERN, date) is not None:
|
2020-03-15 19:49:24 +02:00
|
|
|
uri = self.apod_by_date_api_url + date
|
2020-03-15 19:11:00 +02:00
|
|
|
await self.send_apod(bot, room, uri)
|
|
|
|
else:
|
|
|
|
await bot.send_text(room, "invalid date. accpeted: YYYY-MM-DD")
|
2020-03-15 19:49:24 +02:00
|
|
|
elif len(args) == 3:
|
|
|
|
if args[1] == "apikey":
|
|
|
|
await self.update_api_key(bot, room, event, args[2])
|
2020-03-15 18:38:17 +02:00
|
|
|
|
|
|
|
async def send_apod(self, bot, room, uri):
|
|
|
|
self.logger.debug(f"send request using uri {uri}")
|
|
|
|
response = requests.get(uri)
|
2020-03-01 21:40:31 +02:00
|
|
|
if response.status_code == 200:
|
|
|
|
apod = Apod.create_from_json(response.json())
|
|
|
|
|
2020-03-15 18:38:17 +02:00
|
|
|
self.logger.debug(apod)
|
|
|
|
if apod.media_type == "image":
|
|
|
|
await self.upload_and_send_image(room, bot, apod)
|
2020-03-01 21:40:31 +02:00
|
|
|
else:
|
2020-03-15 18:38:17 +02:00
|
|
|
await self.send_unknown_mediatype(room, bot, apod)
|
|
|
|
elif response.status_code == 400:
|
|
|
|
self.logger.error("unable to request apod api. status: %d text: %s", response.status_code, response.text)
|
|
|
|
await bot.send_text(room, response.json().get("msg"))
|
2020-03-01 21:40:31 +02:00
|
|
|
else:
|
2020-03-15 18:38:17 +02:00
|
|
|
self.logger.error("unable to request apod api. response: [status: %d text: %s]", response.status_code, response.text)
|
2020-03-01 21:40:31 +02:00
|
|
|
await bot.send_text(room, "sorry. something went wrong accessing the api :(")
|
|
|
|
|
|
|
|
async def send_unknown_mediatype(self, room, bot, apod):
|
2020-03-15 19:56:11 +02:00
|
|
|
self.logger.debug(f"unknown media_type: {apod.media_type}. sending raw information")
|
2020-03-15 20:28:02 +02:00
|
|
|
await bot.send_text(room, f"{apod.title}")
|
2020-03-16 08:37:52 +02:00
|
|
|
await bot.send_text(room, f"{apod.explanation} || date: {apod.date} || original-url: {apod.url}")
|
2020-03-01 21:40:31 +02:00
|
|
|
|
|
|
|
async def upload_and_send_image(self, room, bot, apod):
|
2021-04-21 11:06:58 +03:00
|
|
|
send_again = True
|
|
|
|
await bot.send_text(room, f"{apod.title} ({apod.date})")
|
2020-03-15 18:38:17 +02:00
|
|
|
if apod.date in self.matrix_uri_cache:
|
|
|
|
matrix_uri = self.matrix_uri_cache.get(apod.date)
|
|
|
|
self.logger.debug(f"already uploaded picture {matrix_uri} for date {apod.date}")
|
|
|
|
else:
|
2021-04-21 11:06:58 +03:00
|
|
|
matrix_uri = await bot.upload_and_send_image(room, apod.hdurl, f"{apod.title}")
|
|
|
|
send_again = False
|
2020-03-15 18:38:17 +02:00
|
|
|
|
2020-03-01 21:40:31 +02:00
|
|
|
if matrix_uri is not None:
|
2020-03-15 18:38:17 +02:00
|
|
|
self.matrix_uri_cache[apod.date] = matrix_uri
|
2020-03-01 21:40:31 +02:00
|
|
|
bot.save_settings()
|
2021-04-21 11:06:58 +03:00
|
|
|
if send_again:
|
|
|
|
await bot.send_image(room, matrix_uri, f"{apod.title}")
|
2020-03-01 21:40:31 +02:00
|
|
|
else:
|
2020-12-20 21:42:18 +02:00
|
|
|
await bot.send_text(room, "Sorry. Something went wrong uploading the image to Matrix server :(")
|
2020-03-26 20:41:26 +02:00
|
|
|
await bot.send_text(room, f"{apod.explanation}")
|
2020-03-01 21:40:31 +02:00
|
|
|
|
|
|
|
def get_settings(self):
|
|
|
|
data = super().get_settings()
|
2020-03-15 18:38:17 +02:00
|
|
|
data["matrix_uri_cache"] = self.matrix_uri_cache
|
2020-03-15 19:49:24 +02:00
|
|
|
data["api_key"] = self.api_key
|
2020-03-01 21:40:31 +02:00
|
|
|
return data
|
|
|
|
|
|
|
|
def set_settings(self, data):
|
|
|
|
super().set_settings(data)
|
2020-03-15 18:38:17 +02:00
|
|
|
if data.get("matrix_uri_cache"):
|
|
|
|
self.matrix_uri_cache = data["matrix_uri_cache"]
|
2020-03-15 19:49:24 +02:00
|
|
|
if data.get("api_key"):
|
|
|
|
self.api_key = data["api_key"]
|
|
|
|
self.update_api_urls()
|
2020-03-01 21:40:31 +02:00
|
|
|
|
|
|
|
def help(self):
|
|
|
|
return 'Sends latest Astronomy Picture of the Day to the room. (https://apod.nasa.gov/apod/astropix.html)'
|
2020-03-15 19:11:00 +02:00
|
|
|
|
|
|
|
async def send_stats(self, bot, room):
|
|
|
|
msg = f"collected {len(self.matrix_uri_cache)} upload matrix uri's"
|
|
|
|
await bot.send_text(room, msg)
|
|
|
|
|
|
|
|
async def clear_uri_cache(self, bot, room):
|
|
|
|
self.matrix_uri_cache.clear()
|
|
|
|
bot.save_settings()
|
|
|
|
await bot.send_text(room, "cleared uri cache")
|
|
|
|
|
|
|
|
async def command_help(self, bot, room):
|
|
|
|
msg = """commands:
|
|
|
|
- YYYY-MM-DD - date of the APOD image to retrieve (ex. 2020-03-15)
|
|
|
|
- stats - show information about uri cache
|
|
|
|
- clear - clear uri cache (Must be done as admin)
|
2020-03-15 19:49:24 +02:00
|
|
|
- apikey [api-key] - set the nasa api key (Must be done as bot owner)
|
2020-03-15 19:11:00 +02:00
|
|
|
- help - show command help
|
|
|
|
"""
|
|
|
|
await bot.send_text(room, msg)
|
2020-03-15 19:49:24 +02:00
|
|
|
|
|
|
|
async def update_api_key(self, bot, room, event, apikey):
|
|
|
|
bot.must_be_owner(event)
|
|
|
|
self.api_key = apikey
|
|
|
|
self.update_api_urls()
|
|
|
|
bot.save_settings()
|
|
|
|
await bot.send_text(room, 'Api key set')
|
|
|
|
|