Esempio n. 1
0
def getPlayer(mlb_id):
    db.execute("SELECT * FROM players WHERE mlb_id = ?", (mlb_id,))

    row = db.fetchone()

    if row is None:
        # Let's go find this player! First we need to find out what team they're on, and what that team's last game_id was
        try:
            file = urllib2.urlopen("http://gd2.mlb.com/components/game/mlb/year_2012/batters/%s.xml" % mlb_id).read()
        except urllib2.HTTPError:
            return False

        dom = minidom.parseString(file)
        game_id = dom.getElementsByTagName("batting")[0].getAttribute("game_id")

        year, month, day, meh = game_id.split("/")
        game_id = game_id.replace("-", "_").replace("/", "_")

        try:
            file = urllib2.urlopen(
                "http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/gid_%s/batters/%s.xml"
                % (year, month, day, game_id, mlb_id)
            ).read()
        except urllib2.HTTPError:
            return False

        player = minidom.parseString(file).getElementsByTagName("Player")[0]

        db.execute(
            "INSERT OR IGNORE INTO players (mlb_id, first, last, num, team) VALUES (?, ?, ?, ?, ?)",
            (
                mlb_id,
                player.getAttribute("first_name"),
                player.getAttribute("last_name"),
                player.getAttribute("jersey_number"),
                getTeam(player.getAttribute("team")).code,
            ),
        )

        connection.commit()

        return getPlayer(mlb_id)

    return Player(*row)
Esempio n. 2
0
def getTeam(name):
	if isinstance(name, Team):
		_localCache[name.code] = name
		return name

	if name in _localCache:
		return _localCache[name]

	db.execute("SELECT * FROM teams WHERE code = ?", (name,))
	row = db.fetchone()

	if row is not None:
		_localCache[row["code"]] = Team(*row)
		return _localCache[row["code"]]

	name = name.lower()

	for code in team_matching:
		if name in team_matching[code]:
			return getTeam(code)

	return None