maubot-ticker/tickerbot.py

75 lines
2.9 KiB
Python
Raw Normal View History

2020-12-05 08:30:52 +00:00
from typing import Optional, Type
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
from maubot import Plugin, MessageEvent
from maubot.handlers import command
# not necessary, as it's imported by maubot already
#import asyncio
#import aiohttp
2020-12-05 08:30:52 +00:00
import json
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("apiKey")
helper.copy("stocktrigger")
helper.copy("cryptotrigger")
class TickerBot(Plugin):
async def start(self) -> None:
await super().start()
self.config.load_and_update()
@classmethod
def get_config_class(cls) -> Type[BaseProxyConfig]:
return Config
@command.new(name=lambda self: self.config["stocktrigger"],
help="Look up information about a stock by its ticker symbol")
@command.argument("ticker", pass_raw=True, required=True)
async def handler(self, evt: MessageEvent, ticker: str) -> None:
await evt.mark_read()
tickerUpper = ticker.upper()
url = f"https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/v2/get-quotes?symbols={tickerUpper}"
2020-12-05 08:30:52 +00:00
headers = {
'x-rapidapi-key': self.config["apiKey"],
'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com"
}
try:
response = await self.http.get(url, headers=headers)
resp_json = await response.json()
2020-12-05 08:30:52 +00:00
except Exception as e:
await evt.respond(f"request failed: {e.message}")
2020-12-05 08:30:52 +00:00
return None
try:
pruned_json = resp_json['quoteResponse']['result'][0]
openDifference = f"{pruned_json['regularMarketPrice'] - pruned_json['regularMarketOpen']:.2f}"
openPercentDiff = f"{(float(openDifference) / pruned_json['regularMarketOpen'] * 100):.2f}%"
2020-12-05 08:30:52 +00:00
except Exception as e:
await evt.respond("No results, double check that you've chosen a real ticker symbol")
2020-12-05 08:30:52 +00:00
self.log.exception(e)
return None
if float(openDifference) < 0:
color = "red"
arrow = "\U000025BC"
else:
color = "green"
arrow = "\U000025B2"
prettyMessage = "<br />".join(
[
f"<b>Current data for <a href=\"https://finance.yahoo.com/quote/{tickerUpper}\">\
{pruned_json['longName']}</a> ({tickerUpper}):</b>" ,
2020-12-05 08:30:52 +00:00
f"",
f"<b>Price:</b> <font color=\"{color}\">${str(pruned_json['regularMarketPrice'])}, \
{arrow}{openPercentDiff}</font> from market open @ ${str(pruned_json['regularMarketOpen'])}",
f"<b>52 Week High:</b> ${str(pruned_json['fiftyTwoWeekHigh'])}",
f"<b>52 Week Low:</b> ${str(pruned_json['fiftyTwoWeekLow'])}"
2020-12-05 08:30:52 +00:00
]
)
await evt.respond(prettyMessage, allow_html=True)