-
Notifications
You must be signed in to change notification settings - Fork 14
/
staff.py
178 lines (132 loc) · 4.77 KB
/
staff.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import mechanize
import urllib
import threading
import re
import time
import json
from bs4 import BeautifulSoup as bs4
from collections import OrderedDict
from util import colorize, shorten
# Because where would Batman be without Alfred? Without tea,
# that's fucking where. These are handy helper objects for
# dealing with commands and some more complex shit that
# shouldn't be as complex as it ends up being in brainmeats.
# In particular, the Browser object gets around a lot of
# annoyances in http calls.
# This is how we pseudo-thread all the commands.
class Butler(object):
maxtasks = 8
semaphore = False
def __init__(self, cortex):
self.cx = cortex
self.semaphore = threading.BoundedSemaphore(self.maxtasks)
return
def wrap(self, func, args, semaphore, note, pid):
results = func(*args)
if results:
note = note % results
self.cx.chat(note)
semaphore.release()
return
def do(self, func, args, note=False):
pid = 'task-%s' % time.time()
self.semaphore.acquire()
thread = threading.Thread(target=self.wrap, args=(func, args, self.semaphore, note, pid))
thread.start()
# Better browsing through technology
class Browser(object):
url = False
ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36'
ieua = 'User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
robot = mechanize.Browser()
text = False
error = False
def __init__(self, url, params={}, method='GET', userpass=False, headers=[]):
self.url = url
self.robot.set_handle_equiv(True)
self.robot.set_handle_gzip(True)
self.robot.set_handle_redirect(True)
self.robot.set_handle_referer(True)
self.robot.set_handle_robots(False)
self.robot.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
baseheaders = [
('User-Agent', self.ua),
('Accept', '*/*'),
('Accept-Encoding', 'gzip,deflate,sdch'),
('Accept-Language', 'en-US,en;q=0.8'),
('Cache-Control', 'max-age=0'),
('Connection', 'keep-alive'),
]
self.robot.addheaders = baseheaders + headers
if userpass:
user, password = userpass.split(':')
self.robot.add_password(url, user, password)
# TODO params are broken
try:
if params:
data = urllib.urlencode(params)
if params and method == 'GET':
self.response = self.robot.open(url + '?%s' % data)
elif params and method == 'POST':
self.response = self.robot.open(url, data)
else:
self.response = self.robot.open(url)
except Exception as e:
self.error = str(e)
def soup(self):
return bs4(self.response.read())
def json(self):
return json.loads(self.response.read())
def read(self):
return self.response.read()
def title(self):
try:
result = self.robot.title().decode('utf-8')
except Exception as e:
result = str(e)
return result
def headers(self):
return self.response.info()
# For all your stock needs
class Broker(object):
def __init__(self, symbol):
self.stock = None
self.symbol = symbol
self.price = 0
if not symbol:
return
# Yahoo uses hyphens in the symbols; old portfolios might be saved
# with dots from when we were using the Google API - look up with hyphen.
symbol = symbol.replace('.', '-')
# yahoo specific
url = 'https://api.iextrading.com/1.0/stock/%s/quote' % symbol
try:
fulldata = Browser(url).json()
except Exception as e:
print e
return
data = fulldata
for key, value in data.items():
setattr(self, key, value)
self.stock = data
def __nonzero__(self):
return self.stock is not None
def showquote(self, context):
if not self.stock:
return False
name = "%s (%s)" % (self.companyName, self.symbol.upper())
changestring = str(self.change) + " (" + ("%.2f" % self.changePercent) + "%)"
if self.change < 0:
changestring = colorize(changestring, 'red')
else:
changestring = colorize(changestring, 'lightgreen')
message = [
name,
str(self.iexRealtimePrice),
changestring,
]
link = 'http://finance.yahoo.com/q?s=' + self.symbol
roasted = shorten(link)
message.append(roasted)
output = ', '.join(message)
return output