Stocks plugin for sopel


Good ol’ irc is the best way to keep in contact with my friends, since ‘98 or around. We use sopel for some stuff, for example, to track all the stocks we own.

This is a simple stocks plugin for sopel.

import sopel
import requests

@sopel.module.commands('dji','stocks','stock')
def stocks(bot,trigger):
    if trigger.group(2):
        url = "https://rest.yahoofinanceapi.com/v6/finance/quote"
        querystring = {"symbols": trigger.group(2)}
        headers = {'x-api-key': "FAKE_API_KEY"}
        try:
            response = requests.request("GET", url, headers=headers, params=querystring)
            stock = response.json()['quoteResponse']['result'][0]
        except requests.exceptions.RequestException as e:
            return bot.say("An error ocurred")
    else:
        return bot.say("Please use a valid symbol")

    change = '{0:.2f}'.format(float(stock['regularMarketChange']))
    percent = '{0:.2f}'.format(float(stock['regularMarketChangePercent']))

    return bot.say("{0}: {1} ({2}/{3})".format(stock['longName'], stock['regularMarketPrice'],"\x0304"+change+"\x0F" if float(change) < 0 else "\x0303"+change+"\x0F","\x0304"+percent+"%\x0F" if float(percent) < 0 else "\x0303+"+percent+"%\x0F",str(stock['regularMarketOpen'])))
 

social links