From 68674fc53887e0b24a3b5d045458fad1ded0f08a Mon Sep 17 00:00:00 2001 From: wreck Date: Tue, 22 Dec 2020 13:09:37 -0800 Subject: [PATCH] use in-built async http client instead of requests --- maubot.yaml | 2 +- tickerbot.py | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/maubot.yaml b/maubot.yaml index 1b7d585..880274a 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: org.jobmachine.tickerbot -version: 0.0.2 +version: 0.0.3 license: MIT modules: - tickerbot diff --git a/tickerbot.py b/tickerbot.py index 2bbbef6..d54e36b 100644 --- a/tickerbot.py +++ b/tickerbot.py @@ -4,7 +4,9 @@ from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper from maubot import Plugin, MessageEvent from maubot.handlers import command -import requests +# not necessary, as it's imported by maubot already +#import asyncio +#import aiohttp import json class Config(BaseProxyConfig): @@ -29,22 +31,22 @@ class TickerBot(Plugin): await evt.mark_read() tickerUpper = ticker.upper() - url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/v2/get-quotes" - querystring = {"symbols":tickerUpper} + url = f"https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/v2/get-quotes?symbols={tickerUpper}" headers = { 'x-rapidapi-key': self.config["apiKey"], 'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com" } try: - response = requests.request("GET", url, headers=headers, params=querystring) + response = await self.http.get(url, headers=headers) + resp_json = await response.json() except Exception as e: await evt.respond(f"request failed: {e.message}") return None try: - jsonresponse = response.json()['quoteResponse']['result'][0] - openDifference = f"{jsonresponse['regularMarketPrice'] - jsonresponse['regularMarketOpen']:.2f}" - openPercentDiff = f"{(float(openDifference) / jsonresponse['regularMarketOpen'] * 100):.2f}%" + 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}%" except Exception as e: await evt.respond("No results, double check that you've chosen a real ticker symbol") self.log.exception(e) @@ -60,12 +62,12 @@ class TickerBot(Plugin): prettyMessage = "
".join( [ f"Current data for \ - {jsonresponse['longName']} ({tickerUpper}):" , + {pruned_json['longName']} ({tickerUpper}):" , f"", - f"Price: ${str(jsonresponse['regularMarketPrice'])}, \ - {arrow}{openPercentDiff} from market open @ ${str(jsonresponse['regularMarketOpen'])}", - f"52 Week High: ${str(jsonresponse['fiftyTwoWeekHigh'])}", - f"52 Week Low: ${str(jsonresponse['fiftyTwoWeekLow'])}" + f"Price: ${str(pruned_json['regularMarketPrice'])}, \ + {arrow}{openPercentDiff} from market open @ ${str(pruned_json['regularMarketOpen'])}", + f"52 Week High: ${str(pruned_json['fiftyTwoWeekHigh'])}", + f"52 Week Low: ${str(pruned_json['fiftyTwoWeekLow'])}" ] )