/
scraper.py
215 lines (187 loc) · 6.92 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
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
from datetime import date, datetime
from decimal import Decimal
import itertools
import json
import re
import requests
import scraperwiki
SEASON = (2013, 2014)
def percentage(part, whole):
return int(float(part) / float(whole) * 100)
class Clubs(object):
names = (
(1, 'ARS', 'Arsenal'),
(2, 'AVL', 'Aston Villa'),
(3, 'CAR', 'Cardiff'),
(4, 'CHE', 'Chelsea'),
(5, 'CRY', 'Crystal Palace'),
(6, 'EVE', 'Everton'),
(7, 'FUL', 'Fulham'),
(8, 'HUL', 'Hull'),
(9, 'LIV', 'Liverpool'),
(10, 'MCI', 'Man City'),
(11, 'MUN', 'Man Utd', 'Man United'),
(12, 'NEW', 'Newcastle'),
(13, 'NOR', 'Norwich'),
(14, 'SOU', 'Southampton'),
(15, 'STK', 'Stoke City', 'Stoke'),
(16, 'SUN', 'Sunderland'),
(17, 'SWA', 'Swansea'),
(18, 'TOT', 'Tottenham'),
(19, 'WBA', 'West Brom'),
(20, 'WHU', 'West Ham'),
)
def __init__(self):
self._codes = {}
base_url = 'http://www.dectech.org'
url = '%s/cgi-bin/new_site/GetTeamRankingsIntl.pl?divID=0' % base_url
referer = '%s/football_sites/football_dectech/rankings.php' % base_url
html = requests.get(url, headers=dict(referer=referer)).content
self._dectech = {}
data = json.loads(html)
for ranking in data['rankings']:
club = dict(
name=self.find_code(ranking['name']),
defence=percentage(data['bounds']['minDef'], ranking['def']),
attack=percentage(ranking['atk'], data['bounds']['maxAtk']),
overall=percentage(ranking['overall'], data['bounds']['maxOvr']))
self._dectech[club['name']] = club
scraperwiki.sqlite.save(['name'], self._dectech.values(), table_name='club')
def find_code(self, name):
code = self._codes.get(name)
if code:
return code
for club in self.names:
if name in club:
self._codes[name] = club[1]
return club[1]
def find_dectech(self, name):
return self._dectech[self.find_code(name)]
CLUBS = Clubs()
POSITIONS = {
1: 'Goalkeeper', 2: 'Defender',
3: 'Midfielder', 4: 'Forward'}
match_re = re.compile(r'(\w\w\w)\((\w)\) (\d{1,2})-(\d{1,2})')
def fpl_to_decimal(value):
return Decimal((0, [int(c) for c in str(value)], -1))
def dectech_score(position, own, other):
if position in (1, 2):
return own['defence'] - other['attack']
else:
return own['attack'] - other['defence']
def fpl_to_date(value):
dt = datetime.strptime(value, '%d %b %H:%M')
year = SEASON[0] if dt.month > 6 else SEASON[1]
return date(year, dt.month, dt.day)
def elements(start=None, end=None):
url = 'http://fantasy.premierleague.com/web/api/elements/%d/'
start = start or scraperwiki.sqlite.get_var('next_element', default=1)
for i in itertools.count(start):
if end and i > end:
break
response = requests.get(url % i)
if not response.ok:
break
yield json.loads(response.content)
scraperwiki.sqlite.save_var('next_element', i + 1)
scraperwiki.sqlite.save_var('next_element', 1)
def import_fixture_history(element):
own_dt = CLUBS.find_dectech(element['team_id'])
records = []
for event in element['fixture_history']['all']:
match = match_re.match(event[2])
if not match:
continue
match = match.groups()
# other_dt = CLUBS.find_dectech(match[0])
#print event
fixture = dict(
player_id=element['id'],
#played_on=fpl_to_date(event[0]),
played_on=event[0],
round=event[1],
opponent=CLUBS.find_code(match[0]),
venue=match[1],
#team_goals_scored=match[2],
#team_goals_conceded=match[3],
#minutes_played=event[3],
#goals_scored=event[4],
#assists=event[5],
#clean_sheets=event[6],
#goals_conceded=event[7],
#own_goals=event[8],
# penalties_saved=event[9],
#penalties_missed=event[10],
#yellow_cards=event[11],
# red_cards=event[12],
#saves=event[13],
#bonus=event[14],
# ea_sports_ppi=event[15],
#make BPS event 16?
# net_transfers=event[17],
#value=fpl_to_decimal(event[18]),
#points=event[19])
)
#### #dt_diff=dectech_score(element['element_type_id'], own_dt, other_dt))
records.append(fixture)
scraperwiki.sqlite.save(['player_id', 'played_on'], records,table_name='fixture_history')
def import_player(element):
player = dict(
id=element['id'],
code=element['code'],
forename=element['first_name'],
surname=element['second_name'],
web_name=element['web_name'],
club=CLUBS.find_code(element['team_id']),
position=POSITIONS[element['element_type_id']],
#cost=fpl_to_decimal(element['now_cost']))
cost=element['now_cost'])
# )
scraperwiki.sqlite.save(['id'], player, table_name='player')
# scraperwiki.sqlite.save(['id'], player)
#def import_season_history(element):
# records = []
# for season in element['season_history']:
# history = dict(
# player_id=element['id'],
# season=season[0],
# minutes_played=season[1],
# goals_scored=season[2],
# assists=season[3],
# clean_sheets=season[4],
# goals_conceded=season[5],
# own_goals=season[6],
# penalties_saved=season[7],
# penalties_missed=season[8],
# yellow_cards=season[9],
# red_cards=season[10],
# saves=season[11],
# bonus=season[12],
# ea_sports_ppi=season[13],
# value=fpl_to_decimal(season[14]),
# points=season[15])
# records.append(history)
# scraperwiki.sqlite.save(['player_id', 'season'], records,
# table_name='season_history')
#def import_fixtures(element):
# own_dt = CLUBS.find_dectech(element['team_id'])
# records = []
# for event in element['fixtures']['all']:
# other_dt = CLUBS.find_dectech(event[2][:-4])
# fixture = dict(
# player_id=element['id'],
# played_on=fpl_to_date(event[0]),
# round=int(re.findall('\d{1,2}', event[1])[0]),
# opponent=CLUBS.find_code(event[2][:-4]),
# venue=event[2][-2:-1],
# dt_diff=dectech_score(element['element_type_id'], own_dt, other_dt))
# records.append(fixture)
# scraperwiki.sqlite.save(['player_id', 'played_on'], records,
# table_name='fixtures')
def main():#663
for element in elements(1, 5):
import_player(element)
#import_season_history(element)
import_fixture_history(element)
#import_fixtures(element)
main()