-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandings.py
More file actions
145 lines (132 loc) · 5.84 KB
/
Standings.py
File metadata and controls
145 lines (132 loc) · 5.84 KB
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
from bs4 import BeautifulSoup
from collections import OrderedDict
from urllib.request import urlopen
from MarkdownOutput import MarkdownOutput
import NFL
def get_standings(playoffs=True):
if playoffs:
url = "http://espn.go.com/nfl/standings/_/type/playoffs"
column_map = NFL.PLAYOFF_STANDINGS_COLUMNS_MAPPING
else:
url = "http://espn.go.com/nfl/standings"
column_map = NFL.STANDINGS_COLUMNS_MAPPING
page = urlopen(url).read()
soup = BeautifulSoup(page)
table = soup.find("table", {'class': 'tablehead'})
# Parse Standings Table
standings = {}
conference = None
for row in table.findAll("tr"):
if row['class'][0] == 'stathead':
conference = row.td.text
standings[conference] = OrderedDict()
elif row['class'][0] == 'colhead':
stat_cols_raw = []
stat_cols = []
for col in row.findAll('td'):
stat_cols_raw.append(col.text)
stat_cols.append(column_map.get(col.text, col.text))
if playoffs:
team_container = standings[conference]
else:
division = stat_cols_raw[0]
standings[conference][division] = OrderedDict()
team_container = standings[conference][division]
else:
team_row = row.findAll('td')
team_dict = {}
for i in range(len(team_row)):
team_dict[stat_cols[i]] = team_row[i].text.strip()
# Split playoff prefixes to get the city name
team_dict['TEAM'] = team_dict['TEAM'].split('-')[-1].strip()
# Replace the city name for the subreddit link
# team_dict['TEAM'] = NFL.TEAM_SUBREDDIT_MAP[team_dict['TEAM']]
team_dict['TEAM'] = NFL.TEAM_SUBREDDIT_MAP.get(team_dict['TEAM'], team_dict['TEAM'])
# team_container.append(team_dict)
team_container[team_dict['TEAM']] = team_dict
return standings
def get_conference_markdown(conference=None, playoff_standings=None, omit_eliminated=False):
output = ''
if not playoff_standings:
playoff_standings = get_standings()
for conf_name, conf_data in playoff_standings.items():
output_array = []
if conference and conf_name != conference:
continue
# Extract only wanted information
conference_array = []
for name, team in playoff_standings[conf_name].items():
team_array = []
for col in NFL.PLAYOFF_STANDINGS_COLUMNS:
team_array.append(team[col].strip())
conference_array.append(team_array)
# Print the Markdown output
# Only print the conference name if we are printing all conferences
if not conference:
output += '##{}\n'.format(conf_name)
header_array = []
for col in NFL.PLAYOFF_STANDINGS_COLUMNS:
header_array.append(col)
output_array.append(header_array)
output_array.append([' ', '**Division Leaders**'])
for team_array in conference_array[:4]:
team_array[0] = MarkdownOutput.bold(team_array[0])
team_array[1] = MarkdownOutput.bold(team_array[1])
output_array.append(team_array)
output_array.append([' ', '**Wild Cards**'])
for team_array in conference_array[4:6]:
team_array[0] = MarkdownOutput.bold_italicize(team_array[0])
team_array[1] = MarkdownOutput.bold_italicize(team_array[1])
output_array.append(team_array)
output_array.append([' ', '**In the Hunt**'])
for team_array in conference_array[6:]:
if team_array[1] in NFL.PLAYOFF_ELIMINATED_TEAMS:
if omit_eliminated:
continue
team_array[0] = MarkdownOutput.strikethru(team_array[0])
team_array[1] = MarkdownOutput.strikethru(team_array[1])
output_array.append(team_array)
output += MarkdownOutput.array2table(output_array, header_included=True, justification='centered')
output += '\n\n'
return output
def get_standings_markdown(conference=None, division=None, standings=None):
output_array = []
if not standings:
standings = get_standings(playoffs=False)
# Header
header_array = []
for col in NFL.STANDINGS_COLUMNS:
header_array.append(col)
for conf_name, divisions in standings.items():
if not conference and not division:
output_array.append('##' + conf_name)
elif conf_name != conference and not division:
continue
for div_name, div_team_dict in divisions.items():
if division and (div_name != division):
continue
# Only print the division name if we are printing all divisions
if not division:
output_array.append('###' + div_name)
div_array = [header_array]
for name, team in div_team_dict.items():
# Extract only wanted information
team_array = []
for col in NFL.STANDINGS_COLUMNS:
team_array.append(team[col].strip())
div_array.append(team_array)
output_array.append(MarkdownOutput.array2table(div_array,
header_included=True,
justification='centered'))
output_array.append("")
return "\n".join(output_array)
if __name__ == '__main__':
print(get_conference_markdown())
print('\n' + '='*80 + '\n')
print(get_standings_markdown())
print('\n' + '='*100 + '\n')
print('## 2014 NFC West Standings')
print(get_standings_markdown(division='NFC WEST'))
print('## 2014 NFC Playoff Picture')
print(get_conference_markdown(NFL.CONFERENCE_NAMES['nfc']))
print('\n' + '='*100 + '\n')