-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
181 lines (169 loc) · 8.89 KB
/
scraper.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
# scrapes odds from betfair.org and updates github datapackages
import csv
import datapackage # v0.8.3
import datetime
import json
import git
import os
import requests
import betfair_com_scraper_utils as utils
import settings
data_path = "data/" # from this script to data
# repo settings
repo = git.Repo(settings.git_dir)
git_ssh_identity_file = settings.ssh_file
o = repo.remotes.origin
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file
total_groups = 0
for fdir in settings.betfair_dirsB:
da = utils.scrape_races(fdir)
for roww in da:
dat = utils.scrape_subraces(roww['identifier'])
for row in dat:
date = datetime.datetime.utcnow().isoformat()
data = utils.scrape_race(row['identifier'])
# load or create datapackage
try:
# load datapackage
datapackage_url = settings.project_url + data_path + row['identifier'] + "/datapackage.json"
dp = datapackage.DataPackage(datapackage_url)
except:
# create datapackage
dp = datapackage.DataPackage()
urldp = settings.project_url + "datapackage_prepared.json"
rdp = requests.get(urldp)
prepared = rdp.json()
dp.descriptor['name'] = "betfair_com_" + row['identifier']
dp.descriptor['title'] = roww['title'] + " - " + row['title'] + " - odds from betfair.com"
dp.descriptor['description'] = "Scraped odds from betfair.com for: " + roww['title'] + " - " + row['title']
for k in prepared:
dp.descriptor[k] = prepared[k]
if not os.path.exists(settings.git_dir + data_path + row['identifier']):
os.makedirs(settings.git_dir + data_path + row['identifier'])
with open(settings.git_dir + data_path + row['identifier'] + '/datapackage.json', 'w') as fout:
fout.write(dp.to_json())
repo.git.add(settings.git_dir + data_path + row['identifier'] + '/datapackage.json')
with open(settings.git_dir + data_path + row['identifier'] + '/odds.csv', "w") as fout:
header = []
for resource in dp.resources:
if resource.descriptor['name'] == 'odds':
for field in resource.descriptor['schema']['fields']:
header.append(field['name'])
dw = csv.DictWriter(fout, header)
dw.writeheader()
repo.git.add(settings.git_dir + data_path + row['identifier'] + '/odds.csv')
with open(settings.git_dir + data_path + row['identifier'] + '/market.csv', "w") as fout:
header = []
for resource in dp.resources:
if resource.descriptor['name'] == 'market':
for field in resource.descriptor['schema']['fields']:
header.append(field['name'])
dw = csv.DictWriter(fout, header)
dw.writeheader()
repo.git.add(settings.git_dir + data_path + row['identifier'] + '/market.csv')
# add data
with open(settings.git_dir + data_path + row['identifier'] + '/odds.csv', "a") as fout:
header = []
for resource in dp.resources:
if resource.descriptor['name'] == 'odds':
for field in resource.descriptor['schema']['fields']:
header.append(field['name'])
dw = csv.DictWriter(fout, header)
for bet in data['runners']:
if bet['state']['status'] == 'ACTIVE':
item = {
'date': date,
'title': bet['description']['runnerName'],
'identifier': bet['selectionId']
}
try:
item['odds'] = bet['state']['lastPriceTraded']
except:
item['odds'] = ''
try:
item['available_to_back_0'] = bet['exchange']['availableToBack'][0]['price']
except:
item['available_to_back_0'] = ''
try:
item['available_to_back_1'] = bet['exchange']['availableToBack'][1]['price']
except:
item['available_to_back_1'] = ''
try:
item['available_to_back_2'] = bet['exchange']['availableToBack'][2]['price']
except:
item['available_to_back_2'] = ''
try:
item['available_to_lay_0'] = bet['exchange']['availableToLay'][0]['price']
except:
item['available_to_lay_0'] = ''
try:
item['available_to_lay_1'] = bet['exchange']['availableToLay'][1]['price']
except:
item['available_to_lay_1'] = ''
try:
item['available_to_lay_2'] = bet['exchange']['availableToLay'][2]['price']
except:
item['available_to_lay_2'] = ''
try:
item['available_to_back_0_size'] = bet['exchange']['availableToBack'][0]['size']
except:
item['available_to_back_0_size'] = ''
try:
item['available_to_back_1_size'] = bet['exchange']['availableToBack'][1]['size']
except:
item['available_to_back_1_size'] = ''
try:
item['available_to_back_2_size'] = bet['exchange']['availableToBack'][2]['size']
except:
item['available_to_back_2_size'] = ''
try:
item['available_to_lay_0_size'] = bet['exchange']['availableToLay'][0]['size']
except:
item['available_to_lay_0_size'] = ''
try:
item['available_to_lay_1_size'] = bet['exchange']['availableToLay'][1]['size']
except:
item['available_to_lay_1_size'] = ''
try:
item['available_to_lay_2_size'] = bet['exchange']['availableToLay'][2]['size']
except:
item['available_to_lay_2_size'] = ''
dw.writerow(item)
repo.git.add(settings.git_dir + data_path + row['identifier'] + '/odds.csv')
with open(settings.git_dir + data_path + row['identifier'] + '/market.csv', "a") as fout:
header = []
for resource in dp.resources:
if resource.descriptor['name'] == 'market':
for field in resource.descriptor['schema']['fields']:
header.append(field['name'])
dw = csv.DictWriter(fout, header)
ro = data['state']
item = {
'date': date,
'date_bet': row['date'],
'bet_delay': ro['betDelay'],
'bsp_reconciled': ro['bspReconciled'],
'complete': ro['complete'],
'inplay': ro['inplay'],
'number_of_winners': ro['numberOfWinners'],
'number_of_runners': ro['numberOfRunners'],
'number_of_active_runners': ro['numberOfActiveRunners'],
'total_matched': ro['totalMatched'],
'total_available': ro['totalAvailable'],
'cross_matching': ro['crossMatching'],
'runners_voidable': ro['runnersVoidable'],
'version': ro['version'],
'status': ro['status']
}
try:
item['last_match_time'] = ro['lastMatchTime']
except:
item['last_match_time'] = ''
dw.writerow(item)
repo.git.add(settings.git_dir + data_path + row['identifier'] + '/market.csv')
total_groups += 1
# commit to github
with repo.git.custom_environment(GIT_COMMITTER_NAME=settings.bot_name, GIT_COMMITTER_EMAIL=settings.bot_email):
repo.git.commit(message="happily updating data %s groups of bets" % (str(total_groups)), author="%s <%s>" % (settings.bot_name, settings.bot_email))
with repo.git.custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):
o.push()