/
parser.py
314 lines (281 loc) · 11.8 KB
/
parser.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# coding: utf-8
from lxml import etree
import re
import wikiutils
import soccer
import teamparser
from settings import Globals
table_re = re.compile(r' *\{\| *class *= *"?wikitable"?.*')
br_re = re.compile(r'<br */?>')
def getTopLeagues():
templates = ['UEFA_leagues', 'CONMEBOL_leagues', 'CONCACAF_leagues',
'CAF_leagues', 'AFC_leagues', 'OFC_leagues']
leagues = dict()
for t in templates:
confederationname = t.split('_')[0]
text = wikiutils.getPage('Template:' + t)
if text:
print 'done.'
state = 0
for line in text.split('\n'):
lineWithoutSpaces = ''.join(line.split())
if state == 0 and re.match('\|list[123456789]=', lineWithoutSpaces):
state = 1
elif state == 1:
if lineWithoutSpaces:
if (lineWithoutSpaces[0] == '|' or lineWithoutSpaces[0] == '}'):
state == 0
if lineWithoutSpaces[0] == '*':
v = line.strip('*').strip()
name, link = wikiutils.unlinkify(v)
if link:
leagues[link] = (name, name, link, confederationname)
print 'Found', name
return leagues
def getLeagueData(rvtext, leaguedata):
season = ''
relegationleagues = dict()
numteams = 0
levelnum = 0
divisions = 0
class InfoboxState:
Outside = 0
Entered = 1
RelegationLeagues = 2
NumTeams = 3
NumLevel = 4
Season = 5
ibs = InfoboxState.Outside
for line in rvtext.split('\n'):
lineWithoutSpaces = ''.join(line.split())
if not season and lineWithoutSpaces.startswith("|current="):
k, v = wikiutils.getKeyValue(line)
competition, competitionlink = wikiutils.unlinkify(v)
if competitionlink:
season = competitionlink
if not divisions and (lineWithoutSpaces.startswith("|divisions=") or lineWithoutSpaces.startswith("|division=")):
tp = wikiutils.getNumberKeyValue(line)
if tp:
divisions = tp
if not levelnum and (lineWithoutSpaces.startswith("|levels=") or lineWithoutSpaces.startswith("|level=")):
tp = wikiutils.getNumberKeyValue(line)
if tp:
levelnum = tp
if len(relegationleagues) == 0 and lineWithoutSpaces.startswith("|relegation="):
k, v = wikiutils.getKeyValue(line)
candidates = [wikiutils.unlinkify(x.strip()) for x in br_re.split(v)]
for cn, cl in candidates:
if cl:
relegationleagues[cl] = cl
if not numteams and lineWithoutSpaces.startswith('|teams='):
numteams = wikiutils.getNumberKeyValue(line)
if ibs == InfoboxState.Outside and lineWithoutSpaces.startswith('{|class="infoboxfootball"'):
# e.g. Regionalliga_Nord
ibs = InfoboxState.Entered
elif ibs != InfoboxState.Outside:
if lineWithoutSpaces and lineWithoutSpaces[0] == '|':
text = '|'.join(line.split('|')[2:])
if not text and lineWithoutSpaces[0:2] == '|}':
ibs = InfoboxState.Outside
break
elif text:
t, link = wikiutils.unlinkify(text)
tl = t.lower()
if 'background' in line:
if 'relegation' in tl:
ibs = InfoboxState.RelegationLeagues
elif 'number of clubs' in tl:
ibs = InfoboxState.NumTeams
elif 'level' in tl:
ibs = InfoboxState.NumLevel
elif 'current season' in tl:
ibs = InfoboxState.Season
else:
ibs = InfoboxState.Entered
else:
if ibs == InfoboxState.RelegationLeagues:
if not link:
ibs = InfoboxState.Entered
else:
relegationleagues[link] = link
elif ibs == InfoboxState.NumTeams:
pos = re.findall(r'\d+', t)
if len(pos) >= 1:
numteams = int(pos[0])
elif ibs == InfoboxState.NumLevel:
pos = re.findall(r'\d+', t)
if len(pos) >= 1:
levelnum = int(pos[0])
elif ibs == InfoboxState.Season:
if not link:
ibs = InfoboxState.Entered
else:
season = link
if not leaguedata.season:
leaguedata.season = season
if not leaguedata.relegationleagues:
leaguedata.relegationleagues = relegationleagues
if not leaguedata.numteams:
leaguedata.numteams = numteams
if not leaguedata.divisions:
leaguedata.divisions = divisions
if not leaguedata.levelnum:
leaguedata.levelnum = levelnum
def addOrUpdateTeamList(l, heading, teams):
def cleaned(namelist):
ret = []
for t in sorted(namelist):
ret.append(t.replace(' ', ' '))
return ret
"""Check whether a list with the same team names already exists in the list.
If this is the case, have the team list with more links in the list."""
tlist = sorted([wikiutils.unlinkify(t) for t in teams])
toinsert = cleaned([t[0] for t in tlist])
previous = None
teamPairList = [p[1] for p in l]
prev = None
for t in l:
thistl = cleaned([x[0] for x in t[1]])
if thistl == toinsert:
prev = t
break
if prev:
numLinksInPrev = len([x for x in prev[1] if x[1]])
numLinksInThis = len([x for x in tlist if x[1]])
if numLinksInThis > numLinksInPrev:
l.remove(prev)
l.append((heading, tlist))
else:
l.append((heading, tlist))
def getSeasonTeams(rvtext, leaguedata):
"""Collect all lists that seem to be club lists along with their corresponding headings.
If one team list with the number of teams as length, we're done.
If multiple team lists, all with the correct number of teams as length, are found,
pick the one with the correct heading.
If multiple team lists are found, all shorter than the correct number of teams,
and the sum of the lists is the total number of teams, create one group for each list."""
correctLengthTeamLists = []
shorterTeamLists = []
tableStatus = 0
teamColumn = -1
thisColumn = -1
haveTeams = False
thisTeamHeading = None
"""First collect all team lists."""
for line in rvtext.split('\n'):
lineWithoutSpaces = ''.join(line.split())
ls = line.strip()
# print "Table status", tableStatus, "line", ls
hd = wikiutils.getHeading(ls)
if hd:
thisTeamHeading = hd
if table_re.match(ls):
tableStatus = 1
teamColumn = -1
thisteams = []
elif tableStatus == 1:
if ls and ls[0] == '!':
teamColumn += 1
if 'Team' in ls or 'Club' in ls:
tableStatus = 2
elif tableStatus == 2:
if ls[0:2] == '|-':
tableStatus = 3
thisColumn = -1
elif tableStatus == 3:
if ls[0:2] == '|-':
tableStatus = 2
thisColumn = -1
elif len(ls) >= 2 and ls[0] == '|' and ls[1] != '}':
columns = ls.split('||')
if len(columns) == 1:
''' Columns divided by line. It looks like this (e.g. Premier League):
{| class="wikitable sortable"
! Team
! Location
! Stadium
|-
| [[Arsenal]]
| [[London]]
| [[Emirates Stadium]]
|-
...
|}
'''
thisColumn += 1
if thisColumn == teamColumn:
teamName = ls.strip('|')
thisteams.append(teamName)
tableStatus = 2
thisColumn = -1
else:
''' Columns on one line. It looks like this (e.g. Football_League_Championship):
{| class="wikitable sortable"
! Team
! Location
! Stadium
|-
| {{ fb team Barnsley }} || [[Barnsley]] || [[Oakwell]]
|-
...
|}
'''
columns = [x.strip() for x in ls[1:].split('||')]
if len(columns) > teamColumn:
thisteams.append(columns[teamColumn])
tableStatus = 2
if (tableStatus == 2 or tableStatus == 3) and ls[0:2] == '|}':
# make sure there are no duplicates in the list - may happen
# e.g. with historical winners tables (Regionalliga_Süd)
if len(set(thisteams)) == len(thisteams):
if len(thisteams) == leaguedata.numteams:
addOrUpdateTeamList(correctLengthTeamLists, thisTeamHeading, thisteams)
elif thisteams and len(thisteams) < leaguedata.numteams:
# Only add list if the same team list not already added.
addOrUpdateTeamList(shorterTeamLists, thisTeamHeading, thisteams)
tableStatus = 0
thisteams = []
thisTeamHeading = None
groups = []
if correctLengthTeamLists and leaguedata.divisions <= 1:
if len(correctLengthTeamLists) == 1:
groups = [('', correctLengthTeamLists[0][1])]
else:
if leaguedata.title:
for theading, tlist in correctLengthTeamLists:
if theading and leaguedata.title in theading:
groups = [(theading, tlist)]
break
if not groups:
# if not found, default to the first one (correct in e.g. 2012_Ykkönen)
groups = [('', correctLengthTeamLists[0][1])]
elif shorterTeamLists and (leaguedata.divisions == 0 or len(shorterTeamLists) == leaguedata.divisions):
totalNumTeams = sum([len(l[1]) for l in shorterTeamLists])
if totalNumTeams == leaguedata.numteams:
groups = shorterTeamLists
return groups
def getTeamData(rvtext, leaguedata):
if leaguedata.hasTeams():
return
groups = getSeasonTeams(rvtext, leaguedata)
if groups:
print len(groups), 'groups found.'
for name, teams in groups:
numCompleteTeams = 0
group = soccer.LeagueGroup(name, leaguedata.title)
print '%d teams in group %s.' % (len(teams), name)
for name, link in teams:
if link:
td = teamparser.fetchTeamData(link)
if td:
if len(td.players) > 0:
numCompleteTeams += 1
group.teams.append(td)
else:
group.teams.append(soccer.Team(name, [], 0, []))
numPartialTeams = len(teams) - numCompleteTeams
group.numPartialTeams = numPartialTeams
group.numCompleteTeams = numCompleteTeams
leaguedata.groups.append(group)
else:
print "Failed finding teams."