/
commands.py
231 lines (201 loc) · 9.57 KB
/
commands.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#import urllib2, time, re
#from BeautifulSoup import BeautifulSoup
#from string import whitespace
#from sqlstuff import SqlStuff
from sqlstuff2 import SqlStuff2
import datetime
#import os.path
import webscrape
import time
from common import daterange
def makeATestcardFromResults(date):
""" This function will make a test card from the results. This is required as the actual
test cards on the racingpost website are only acvailable on the day and one day in advance of
a race"""
print date
ResultStuffInsts=[]
HrefStuffInst=webscrape.HrefStuff()
""" get the hrefs that must be appended to http://www.racingpost.com/"""
fullResultHrefs=HrefStuffInst.getFullResultHrefs(date)
horseName=[]
jockeyName=[]
raceLength=[]
weights=[]
goings=[]
draws=[]
todaysRaceTimes=[]
todaysRaceVenues=[]
"""loop through the number of races and make a ResultStuff object for each"""
for fullResultHref in fullResultHrefs:
HrefStuffInst.getFullResults(fullResultHref)
#print "got full results webpage for..."
fullResult=HrefStuffInst.getFullResultsGrid()
fullHeader=HrefStuffInst.getFullResultsHeader()
fullInfo=HrefStuffInst.getFullRaceInfo()
ResultStuffInst=webscrape.ResultStuff(fullResult, fullHeader, fullInfo, date)
ResultStuffInst.getAllResultInfo()
ResultStuffInsts.append(ResultStuffInst)
"""loop through the ResultStuff class objects and add them to the database"""
for ResultStuffInst in ResultStuffInsts:
horseName.append(ResultStuffInst.horseNames)
jockeyName.append(ResultStuffInst.jockeys)
raceLength.append(ResultStuffInst.raceLength)
weights.append(ResultStuffInst.horseWeights)
goings.append(ResultStuffInst.going)
draws.append(ResultStuffInst.draw)
todaysRaceTimes.append(ResultStuffInst.raceTime)
todaysRaceVenues.append(ResultStuffInst.raceName.replace(u'\n', ''))
return (horseName, jockeyName, raceLength, weights, goings, draws, todaysRaceTimes, todaysRaceVenues)
def makeATestcard(date):
""" extract the information from the days cards"""
HrefStuffInst=webscrape.HrefStuff()
horseName=[]
jockeyName=[]
raceLength=[]
weights=[]
goings=[]
draws=[]
trainers=[]
""" get the href for the days test card"""
todaysTestCardHref=HrefStuffInst.getTestCardHref(date)
""" extract all of the links for the races from the days card"""
todaysRaces, todaysRaceTimes, todaysRaceVenues=HrefStuffInst.getTodaysRaces(todaysTestCardHref)
for todaysRace in todaysRaces:
horse, jockey, length, weight, going, draw, trainer=HrefStuffInst.getCardContents(todaysRace)
horseName.append(horse)
jockeyName.append(jockey)
raceLength.append(length)
weights.append(weight)
goings.append(going)
draws.append(draw)
trainers.append(trainer)
return (horseName, jockeyName, raceLength, weights, goings, draws, trainers, todaysRaceTimes, todaysRaceVenues)
def makeAPoliteDatabase(dateStart, dateEnd, test = "false"):
""" the polite database is based on the original database but has the extra fields...
raceName, raceTime
the going variable will be corrected so that goings with several words are stored correctly.
The webpages will be retrieved with several minutes between them so as to be polite to the
target website"""
dateStartSplit=dateStart.split('-')
dateEndSplit=dateEnd.split('-')
SqlStuffInst=SqlStuff2()
SqlStuffInst.createResultTable()
""" user enters the date """
#date=raw_input("enter the required date yyyy-mm-dd")
for single_date in daterange(datetime.date(int(dateStartSplit[0]),int(dateStartSplit[1]),int(dateStartSplit[2])), datetime.date(int(dateEndSplit[0]),int(dateEndSplit[1]),int(dateEndSplit[2]))):
date=time.strftime("%Y-%m-%d", single_date.timetuple())
#date="2014-07-{}".format(day)
print date
ResultStuffInsts=[]
"""
for fullResultHref in fullResultHrefs:
webpage=urllib2.urlopen(fullResultHref.get("href"))"""
HrefStuffInst=webscrape.HrefStuff()
""" get the hrefs that must be appended to http://www.racingpost.com/"""
fullResultHrefs=HrefStuffInst.getFullResultHrefs(date)
"""loop through the number of races and make a ResultStuff object for each"""
for fullResultHref in fullResultHrefs:
HrefStuffInst.getFullResults(fullResultHref)
#print "got full results webpage for..."
fullResult=HrefStuffInst.getFullResultsGrid()
fullHeader=HrefStuffInst.getFullResultsHeader()
fullInfo=HrefStuffInst.getFullRaceInfo()
ResultStuffInst=webscrape.ResultStuff(fullResult, fullHeader, fullInfo, date)
ResultStuffInst.getAllResultInfo()
"""ResultStuffInst.getRaceDate()
ResultStuffInst.getHorseNames()
ResultStuffInst.getNumberOfHorses()
ResultStuffInst.getRaceLength()
ResultStuffInst.getHorseAge()
ResultStuffInst.getHorseWeight()
ResultStuffInst.getJockeyName()
ResultStuffInst.getGoing()"""
ResultStuffInsts.append(ResultStuffInst)
"""loop through the ResultStuff class objects and add them to the database"""
for ResultStuffInst in ResultStuffInsts:
if test == "false":
SqlStuffInst.addResultStuffToTable(ResultStuffInst)
else:
for idx, horseName in enumerate(ResultStuffInst.horseNames):
"""create a string with this horses values"""
val_str="'{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}, '{}', '{}', '{}', '{}', '{}', {}".format(\
ResultStuffInst.horseNames[idx].replace("'", "''"),\
ResultStuffInst.horseAges[idx], ResultStuffInst.horseWeights[idx], idx+1, \
ResultStuffInst.raceLength, ResultStuffInst.numberOfHorses, \
ResultStuffInst.jockeys[idx].replace("'", "''"), \
ResultStuffInst.going, ResultStuffInst.raceDate, ResultStuffInst.raceTime, \
ResultStuffInst.raceName, ResultStuffInst.draw[idx], \
ResultStuffInst.trainers[idx].replace("'", "''"), \
ResultStuffInst.jumps, \
ResultStuffInst.finishingTime)
print val_str
def makeAResult(date):
""" make an array of all the result infos for the day"""
#date=time.strftime("%Y-%m-%d")
#date="2014-07-{}".format(day)
print date
time.sleep(1)
ResultStuffInsts=[]
"""
for fullResultHref in fullResultHrefs:
webpage=urllib2.urlopen(fullResultHref.get("href"))"""
HrefStuffInst=webscrape.HrefStuff()
""" get the hrefs that must be appended to http://www.racingpost.com/"""
fullResultHrefs=HrefStuffInst.getFullResultHrefs(date)
"""loop through the number of races and make a ResultStuff object for each"""
for fullResultHref in fullResultHrefs:
HrefStuffInst.getFullResults(fullResultHref)
#print "got full results webpage for..."
fullResult=HrefStuffInst.getFullResultsGrid()
fullHeader=HrefStuffInst.getFullResultsHeader()
fullInfo=HrefStuffInst.getFullRaceInfo()
ResultStuffInst=webscrape.ResultStuff(fullResult, fullHeader, fullInfo, date)
ResultStuffInst.getAllResultInfo()
"""ResultStuffInst.getRaceDate()
ResultStuffInst.getHorseNames()
ResultStuffInst.getNumberOfHorses()
ResultStuffInst.getRaceLength()
ResultStuffInst.getHorseAge()
ResultStuffInst.getHorseWeight()
ResultStuffInst.getJockeyName()
ResultStuffInst.getGoing()"""
ResultStuffInsts.append(ResultStuffInst)
return ResultStuffInsts
def writeAResult(date, filenameAppend):
"""write the dates result to a file"""
todaysResults=makeAResult(date)
f = open(str(date)+str(filenameAppend),'a')
original = sys.stdout
sys.stdout = Tee(sys.stdout, f)
for idx, ii in enumerate(todaysResults):
print "race number " + str(idx)
for jdx, jj in enumerate(todaysResults[idx].horseNames):
print str(jj)
sys.stdout = original
def viewADatabase():
SqlStuffInst=SqlStuff2()
SqlStuffInst.viewAllTable()
def viewHorse(horseName):
print "ID, HORSENAME, HORSEAGE, HORSEWEIGHT, POSITION, RACELENGTH, NUMBERHORSES, JOCKEYNAME, GOING, RACEDATE"
SqlStuffInst=SqlStuff2()
SqlStuffInst.viewHorse(horseName)
def viewJockey(jockeyName):
print "ID, HORSENAME, HORSEAGE, HORSEWEIGHT, POSITION, RACELENGTH, NUMBERHORSES, JOCKEYNAME, GOING, RACEDATE"
SqlStuffInst=SqlStuff2()
SqlStuffInst.viewJockey(jockeyName)
def viewDate(date):
print "ID, HORSENAME, HORSEAGE, HORSEWEIGHT, POSITION, RACELENGTH, NUMBERHORSES, JOCKEYNAME, GOING, RACEDATE"
SqlStuffInst=SqlStuff2()
SqlStuffInst.viewDate(date)
def viewNewestDate():
SqlStuffInst=SqlStuff2()
SqlStuffInst.viewNewestDate()
def delDate(date):
SqlStuffInst=SqlStuff2()
SqlStuffInst.delDate(date)
def delDateRange(dateStart, dateStop):
dateStartSplit=dateStart.split("-")
dateStopSplit=dateStop.split("-")
for single_date in daterange(datetime.date(int(dateStartSplit[0]),int(dateStartSplit[1]),int(dateStartSplit[2])), datetime.date(int(dateStopSplit[0]),int(dateStopSplit[1]),int(dateStopSplit[2]))):
print single_date
delDate(single_date)