forked from mattabullock/Baseball-GDT-Bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lookup_team_code.py
41 lines (36 loc) · 1.73 KB
/
lookup_team_code.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
import urllib2
import simplejson as json
teaminfo = {}
def lookup_team_info(field="name_abbrev", lookupfield="team_code", lookupval=None):
global teaminfo
if len(teaminfo) == 0:
try:
response = urllib2.urlopen("http://mlb.com/lookup/json/named.team_all.bam?sport_code=%27mlb%27&active_sw=%27Y%27&all_star_sw=%27N%27")
teaminfo = json.load(response)
except Exception as e:
print "Error downloading team info:",e
return None
teamlist = teaminfo.get('team_all').get('queryResults').get('row')
teams = []
for team in teamlist:
if team.get(lookupfield,"").lower() == lookupval.lower(): teams.append(team)
return teams
print "########"
print "This script will look up the TEAM_CODE value you should use in settings.json."
print "You can enter the team name (Phillies, Athletics, Cubs),"
print "team name abbreviation (PHI, OAK, CHC),"
print "or city (Philadelphia, Oakland, Chicago)"
print ""
while True:
inputval = raw_input("Enter your team's name, abbreviation, or city: ")
teams = []
teams = lookup_team_info(field='team_code', lookupfield='name', lookupval=inputval)
if not teams: teams = lookup_team_info(field='team_code', lookupfield='name_abbrev', lookupval=inputval)
if not teams: teams = lookup_team_info(field='team_code', lookupfield='city', lookupval=inputval)
if not teams: teams = lookup_team_info(field='team_code', lookupfield='name_display_short', lookupval=inputval)
if len(teams):
for team in teams:
print "The TEAM_CODE for",team.get('name_display_long'),"is:",team.get('team_code')
else:
print "TEAM_CODE not found. Are you sure you entered the team name/abbreviation/city correctly?"
print ""