/
teamparser.py
133 lines (116 loc) · 4.58 KB
/
teamparser.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
import re
import wikiutils
from settings import Globals
import playerparser
import soccer
kitinfo_re = re.compile(r'\| *(body|shorts|socks|pattern_b)([12]) *= *([0-9a-fA-F]*)')
def getColorValue(string):
if string == 'yellow':
return soccer.Color(255, 255, 0)
elif string == 'red':
return soccer.Color(255, 0, 0)
elif string == 'blue':
return soccer.Color(0, 0, 255)
elif string == 'green':
return soccer.Color(0, 255, 0)
elif string == 'black':
return soccer.Color(0, 0, 0)
elif string == 'white':
return soccer.Color(255, 255, 255)
if len(string) != 6:
return soccer.Color()
r = string[0:2]
g = string[2:4]
b = string[4:6]
try:
return soccer.Color(int(r, 16), int(g, 16), int(b, 16))
except ValueError:
print >> Globals.errlog, 'Unknown color "%s"' % string
return soccer.Color()
def fetchTeamData(team):
rvtext = wikiutils.getPage(team)
if not rvtext:
print 'No revision text.'
return None
else:
td = parseTeam(team, rvtext, True)
if not td:
print 'failed - no players found.'
return None
else:
print 'done (kit %s, position %d, %d players)' % (td.kits[0].bodycolor, td.pos, len(td.players))
return td
def parseTeam(team, rvtext, mayGetTemplates):
players = []
teamposition = None
kit = [soccer.Kit(), soccer.Kit()]
finishedReadingPlayers = False
lookForSquadTemplate = False
def teamError(msg):
print >> Globals.errlog, "Team %s: %s" % (team.encode('utf-8'), msg.encode('utf-8'))
for line in rvtext.split('\n'):
lineWithoutSpaces = ''.join(line.split())
if not finishedReadingPlayers:
p = playerparser.fetchPlayer(line)
if p:
players.append(p)
else:
heading = wikiutils.getHeading(line)
if heading:
if mayGetTemplates and 'current squad' in heading.lower() or ('first' in heading.lower() and 'squad' in heading.lower()):
lookForSquadTemplate = True
else:
lookForSquadTemplate = False
elif lookForSquadTemplate:
t = wikiutils.getTemplate(line)
if t:
text = wikiutils.getPage('Template:' + t)
if text:
players = playerparser.fetchPlayers(text)
if len(players) > 15:
finishedReadingPlayers = True
if playerparser.endOfPlayerList(line):
finishedReadingPlayers = True
if lineWithoutSpaces.startswith("|position="):
# this seems to usually be either this or last season's position
if not ('promoted' in lineWithoutSpaces.lower() or 'relegated' in lineWithoutSpaces.lower()):
tp = wikiutils.getNumberKeyValue(line)
if tp:
teamposition = tp
kitresults = kitinfo_re.findall(line)
for kitresult in kitresults:
columns = [x.strip() for x in line.split('|') if 'body' in x or 'shorts' in x or 'socks' in x or 'pattern_b' in x]
# apparently, n may be more than 1 if more than one kit part is on a line
for c in columns:
try:
k, v = wikiutils.getKeyValue(c)
except:
continue
if k.startswith('body'):
k = k[4:]
if not k: continue
n = int(k[0]) - 1
if n == 0 or n == 1:
kit[n].bodycolor = getColorValue(v)
elif k.startswith('shorts'):
k = k[6:]
if not k: continue
n = int(k[0]) - 1
if n == 0 or n == 1:
kit[n].shortscolor = getColorValue(v)
elif k.startswith('socks'):
k = k[5:]
if not k: continue
n = int(k[0]) - 1
if n == 0 or n == 1:
kit[n].sockscolor = getColorValue(v)
elif k.startswith('pattern_b') and k != 'pattern_blue':
k = k[9:]
if not k: continue
n = int(k[0]) - 1
# TODO: body type, second color
if len(players) < 15:
return None
if not teamposition:
teamposition = 0
return soccer.Team(team, kit, teamposition, players)