Created
July 5, 2011 23:41
-
-
Save neilkod/1066224 to your computer and use it in GitHub Desktop.
source code for aretheyankeeswinning.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Copyright 2007 Google Inc. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
from datetime import date, datetime, time, timedelta | |
import os | |
import logging | |
from google.appengine.ext.webapp import template | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp import util | |
from google.appengine.api.urlfetch import fetch | |
import simplejson as json | |
class MainHandler(webapp.RequestHandler): | |
def get(self): | |
# warning - bad code. I'm not properly handling the fact that | |
# mlb.com is based on GMT. For now, we'll naively subtract 5 hours | |
# from the current date/time. Keyword: naive. | |
the_date = datetime.today() - timedelta(hours=5) | |
TEAM = 'Yankees' # replace the team with any other. was useful for testing. | |
YEAR, MONTH, DAY = the_date.strftime('%Y-%m-%d').split('-') | |
url = "http://gd2.mlb.com/components/game/mlb/year_%s/month_%s/day_%s/master_scoreboard.json" % \ | |
(YEAR, MONTH, DAY) | |
data = fetch(url).content | |
score_data = json.loads(data) | |
games = score_data['data']['games']['game'] | |
status = "%s aren't playing today" % TEAM | |
for game in games: | |
home_team = game['home_team_name'] | |
away_team = game['away_team_name'] | |
if home_team == TEAM or away_team == TEAM: | |
yankees_game = game | |
# found the game we're looking for, exit the loop of games | |
break | |
# the data source is not documented so I'm learning as I go along. | |
# i noticed that the 'linescore' key is not present until the game starts. | |
# i should also check for game['status']['status'] values | |
# to indicate that the game hasn't started yet. | |
if 'linescore' not in game.keys(): | |
status = "game hasn't started yet!" | |
else: | |
# game has started | |
if game['home_team_name'] == TEAM: | |
our_team = 'home' | |
not_our_team = 'away' | |
else: | |
our_team = 'away' | |
not_our_team = 'home' | |
our_score = int(game['linescore']['r'][our_team]) | |
their_score = int(game['linescore']['r'][not_our_team]) | |
we_won = our_score > their_score | |
they_won = our_score < their_score | |
its_tied = our_score == their_score | |
# refine this. I have to monitor game['status']['status'] to see | |
# what the possible values are | |
if game['status']['status'] == 'Game Over' or game['status']['status'] == 'Final': | |
if we_won: | |
status = 'The %s won!' % TEAM | |
else: | |
status = 'The %s lost' % TEAM | |
else: | |
if game['linescore']['r'][our_team] == game['linescore']['r'][not_our_team]: | |
status = 'its tied' | |
elif game['linescore']['r'][our_team] > game['linescore']['r'][not_our_team]: | |
status = 'Yes' | |
else: | |
status = 'No' | |
output_values = {'status': status} | |
path = os.path.join(os.path.dirname(__file__), 'templates/atyw.html') | |
self.response.out.write(template.render(path, output_values)) | |
def main(): | |
application = webapp.WSGIApplication([('/', MainHandler)], debug=True) | |
util.run_wsgi_app(application) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment