-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgse.py
More file actions
104 lines (72 loc) · 3.17 KB
/
Copy pathgse.py
File metadata and controls
104 lines (72 loc) · 3.17 KB
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
import requests
from bs4 import BeautifulSoup
import pprint
import json
"""
Requires:
BeautifulSoup (sudo pip install beautifulsoup4)
requests (sudo pip install requests) <-- i think
look into Django-Cron --- a django tool for running tasks perioically (every 5,10 minutes or so)
"""
URL = "http://www.gse.com.gh/gseticker/tickerView.php"
HEADERS = {
'connection' : 'keep-alive',
'accept-encoding' : 'gzip,deflate,sdch',
'accept-language' : 'en-US,en;q=0.8',
'accept' : '*/*',
'cache-control' : 'max-age=0',
'host' : 'www.gse.com.gh',
'origin' : 'http://www.gse.com.gh',
'referer' : 'http://www.gse.com.gh/',
'content-length' : '0',
'user-agent' : 'Mozilla/5.0',
}
def get_ticker_data():
def error(string):
return "Something has changed in the reponse HTML!\n%s" % string
r = requests.post(URL, headers=HEADERS)
gse_soup = BeautifulSoup(r.content)
table_rows = gse_soup.find_all('tr')
if not len(table_rows) == 2:
raise ValueError(error("Expected two <tr> elements, got %d" % len(table_rows)))
tickers, volumes = table_rows #unpacking a two element list
ticker_cells = tickers.find_all('td')
volume_cells = volumes.find_all('td')
# lets assert that the first one is different, has no span
if ticker_cells[0].find_all('span'):
raise ValueError(error("Expected first cell of the ticker row to have no span elements!"))
# ok, since we made sure that the first cell is special, we are going to iterate through the rest!
# lets form our datastructure now.
ticker_data = {}
ticker_order = []
for ticker_cell in ticker_cells[1:]:
ticker_cell_spans = ticker_cell.find_all('span')
if not len(ticker_cell_spans) == 3:
raise ValueError(error("Expected 3 spans inside of ticker cell: %s" % str(ticker_cell)))
data = [span.text.strip() for span in ticker_cell_spans]
ticker_symbol = data[0]
try:
price = float(data[1])
except ValueError:
raise ValueError(error("Expected to be able to create a float out of content of second span %s"%data[1]))
try:
change = float(data[2])
except ValueError:
raise ValueError(error("Expected to be able to create a float out of content of second span %s"%data[1]))
ticker_order.append(ticker_symbol)
ticker_data[ticker_symbol] = {
'price' : price,
'change' : change,
'symbol' : ticker_symbol,
}
if not len(ticker_order) == len(volume_cells) - 1:
raise ValueError(error("Expected number of volume cells to be one more than number of ticker symbols!"))
for volume_cell, symbol in zip(volume_cells[1:],ticker_order): #throw away the first td, its garbage
try:
volume = int(volume_cell.text)
except ValueError:
raise ValueError(error("Expected content of volume cell to be integer! got: %s" % volume_cell))
ticker_data[symbol]['volume'] = volume
return [ticker_data[symbol] for symbol in ticker_order]
if __name__ == "__main__":
print json.dumps(get_ticker_data(), indent=4)