-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.py
51 lines (38 loc) · 985 Bytes
/
ticker.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
#! /usr/bin/python
import urllib
import re
import threading
import time
import io
import json
from BeautifulSoup import BeautifulSoup
def Pricer(ticker, cond):
prices = []
while not cond.is_set():
htmlfile = urllib.urlopen("http://finance.yahoo.com/q?s="+ticker+"&ql=1")
htmltext = htmlfile.read()
soup = BeautifulSoup(htmltext)
holder = soup.find(id="yfs_l84_"+ticker)
price = holder.contents[0]
prices.append((price, time.asctime(time.localtime(time.time()))))
if len(prices) >= 100:
f = open(ticker + ".json", 'a')
json.dump(prices, f)
f.close()
prices = []
stockname = ""
threads = []
while stockname != 'exit':
stockname = raw_input("Enter a stock ticker to collect it's price: ")
if stockname == 'exit':
for thread, c in threads:
c.set()
print "Exiting..."
break;
try:
cond = threading.Event()
t = threading.Thread(target=Pricer, args=(stockname,cond,))
threads.append((t, cond))
t.start()
except:
continue