-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocks.py
57 lines (48 loc) · 1.72 KB
/
stocks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
import logging
import json
from Legobot.Lego import Lego
from .cryptocurrency import Cryptocurrency # noqa: F401
logger = logging.getLogger(__name__)
class Stocks(Lego):
def listening_for(self, message):
if message['text'] is not None:
try:
return message['text'].split()[0] == '!stocks'
except Exception as e:
logger.error('Stocks lego failed to check message text: %s'
% e)
return False
def handle(self, message):
try:
target = message['metadata']['source_channel']
opts = {'target': target}
except IndexError:
logger.error('Could not identify message source in message: %s'
% str(message))
try:
query = message['text'].split()[1]
except:
self.reply(message, "Invalid query", opts)
self.reply(message, self._lookup_symbol(query), opts)
def get_name(self):
return 'stocks'
def get_help(self):
return "Lookup a stock symbol's value. Usage: !stocks <symbol>"
def _lookup_symbol(self, symbol):
base_url = 'https://www.google.com/finance/info?q='
url = base_url + symbol
r = requests.get(url)
logger.debug(r.status_code)
if r.status_code == requests.codes.ok:
r = r.text
# take the leading // off the string
r = r[4:]
r = json.loads(r)
r = r[0]
ticker = r['t']
exchange = r['e']
value = r['l']
return '%s trading on %s at %s' % (ticker, exchange, value)
else:
return "Could not find that ticker"