-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiThread.py
More file actions
72 lines (52 loc) · 1.66 KB
/
multiThread.py
File metadata and controls
72 lines (52 loc) · 1.66 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
from threading import Thread
import urllib2
import re
import os
import MySQLdb
# Write to MySQLdb
# Input stock symbol from file, pull value from web,
# Write symbol + value to DB
# Chage working directory
direct = "C:\PythonPrograms\\8_MultiThreadDB\\"
os.chdir(direct)
gmap = {} #hashmap
def thread(shareCode):
pageUrl = "https://au.finance.yahoo.com/q?s=" + shareCode + ".AX"
htmlfile = urllib2.urlopen(pageUrl)
htmltext = htmlfile.read()
regex = '<span id="yfs_l84_'+shareCode.lower()+'.ax">(.+?)</span>'
pattern = re.compile(regex)
results = re.findall(pattern,htmltext)
try:
gmap[shareCode] = results[0]
except:
print "gmap error"
threadList = []
# Stringify words
symbolsList = open("ASX_Top100.txt").read()
symbolsList = symbolsList.split("\n")
#Add symbols onto the list of thread symbols.
for symbol in symbolsList:
print symbol
target = Thread(target=thread,args=(symbol,))
target.start()
threadList.append(target)
#Join threads back
for b in threadList:
b.join()
# Read DB info from file
login_info = open("C:\PythonPrograms\\8_MultiThreadDB\\dbLogin.txt").read()
login_info = login_info.split()
host = login_info[0]
user = login_info[1]
password = login_info[2]
db = login_info[3]
conn = MySQLdb.connect(host=login_info[0], user=login_info[1], passwd=login_info[2], db=login_info[3])
for key in gmap.keys():
print key, gmap[key] #symbol and price
query = "INSERT INTO stock_table (symbol,last) values (" #can refine to single query
query = query + "'" + key + "',"+ gmap[key] +")"
x = conn.cursor()
x.execute(query)
conn.commit()
row = x.fetchall()