<commit_before>#!/usr/bin/env python

import datetime
from google.appengine.api import users
from google.appengine.ext import ndb
import json
import webapp2
from google.appengine.api import urlfetch
import logging
from models import Option, Outcome, Event, Pick, UserGoatIndex
#from google.appengine.api import memcache
#import urllib2
#from xml.etree import ElementTree



class GameHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        throwaway = datetime.datetime.strptime('20110101','%Y%m%d')
        curr_date = datetime.datetime.strptime(self.request.get('date'), "%Y%m%d")

        #if curr_date is None:
        sport = self.request.get('sport')
        #sport = 'nfl'
        logging.info(sport)
        if sport == "nfl":
            qry = Event.query().filter(Event.sport == "nfl", Event.date > curr_date).order(Event.date)
            week = qry.fetch(1)[0].week
            #logging.info(week)
            curr_games_qry = Event.query().filter(Event.week == week)
        else:
            curr_games_qry = Event.query().filter(Event.date == curr_date)
        curr_games_raw = curr_games_qry.fetch()
        #logging.info(curr_games_raw)
        #logging.info(curr_games_raw)
        responseData = []
        for curr_game in curr_games_raw:
            logging.info(curr_game)
            #curr_pick = Pick.query().filter(Pick.event.id() == curr_game.key.id())
            start_time = curr_game.start_time - datetime.timedelta(hours = 5) # to ET
            start_time = start_time.strftime("%H:%M:%S")
            winner = curr_game.outcome.winner
            if winner is not None:
                winner = winner.id()
            game_data = {
                            'time': start_time,
                            'sport': sport,
                            'game_id': curr_game.key.id(), 
                            'home': curr_game.options[0].get().tri_code, 
                            'home_id': curr_game.options[0].id(), 
                            'away': curr_game.options[1].get().tri_code, 
                            'away_id': curr_game.options[1].id(), 
                            'winner': winner}
            if sport == "nfl":
                game_data['week'] = week
                #logging.info("HELLO")
                logging.info(curr_game.date)
                game_data['date'] = curr_game.date.strftime("%Y%m%d")
            if len(curr_game.outcome.scores) > 0:
                game_data['scores'] = curr_game.outcome.scores
            #start_time = curr_game.start_time.strftime("%H:%M:%S") 
            if user:
                curr_pick_qry = Pick.query().filter(Pick.user_id == user.user_id())
                curr_pick_qry = curr_pick_qry.filter(Pick.event == curr_game.key)
                #print curr_pick_qry
                curr_pick = curr_pick_qry.fetch()
                if len(curr_pick) > 0:
                    game_data['current_pick'] = curr_pick[0].pick.get().tri_code
                    responseData.append(game_data)
                    #responseData.append({'time': start_time, 'game_id': curr_game.key.id(), 'home': curr_game.options[0].get().tri_code, 'home_id': curr_game.options[0].id(), 'away': curr_game.options[1].get().tri_code, 'away_id': curr_game.options[1].id(), 'current_pick': curr_pick[0].pick.get().tri_code, 'winner': curr_game.outcome.winner.id()})
                else:
                    responseData.append(game_data)
                    #responseData.append({'time': start_time, 'game_id': curr_game.key.id(), 'home': curr_game.options[0].get().tri_code, 'home_id': curr_game.options[0].id(), 'away': curr_game.options[1].get().tri_code, 'away_id': curr_game.options[1].id(), 'winner': curr_game.outcome.winner.id()})
            else:
                responseData.append(game_data)
        # MAGIC
        self.response.out.write(json.dumps(responseData))

current_live_data = None
last_polled_ts = datetime.datetime.utcnow()

class LiveGameHandler(webapp2.RequestHandler):
    def get(self):
        global current_live_data
        global last_polled_ts
        #logging.info("HELLO")
        curr_ts = datetime.datetime.utcnow()
        if (curr_ts - last_polled_ts).total_seconds() > 10 or current_live_data is None:
            # poll new
            curr_date_str = datetime.datetime.strftime((curr_ts - datetime.timedelta(hours = 5)), "%Y%m%d")
            url =  "http://data.nba.net/data/10s/prod/v1/{}/scoreboard.json".format(curr_date_str)
            #import json
            r = urlfetch.fetch(url)<commit_msg>live game accounts for 6 hour gap<commit_after>#!/usr/bin/env python

import datetime
from google.appengine.api import users
from google.appengine.ext import ndb
import json
import webapp2
from google.appengine.api import urlfetch
import logging
from models import Option, Outcome, Event, Pick, UserGoatIndex
#from google.appengine.api import memcache
#import urllib2
#from xml.etree import ElementTree



class GameHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        throwaway = datetime.datetime.strptime('20110101','%Y%m%d')
        curr_date = datetime.datetime.strptime(self.request.get('date'), "%Y%m%d")

        #if curr_date is None:
        sport = self.request.get('sport')
        #sport = 'nfl'
        logging.info(sport)
        if sport == "nfl":
            qry = Event.query().filter(Event.sport == "nfl", Event.date > curr_date).order(Event.date)
            week = qry.fetch(1)[0].week
            #logging.info(week)
            curr_games_qry = Event.query().filter(Event.week == week)
        else:
            curr_games_qry = Event.query().filter(Event.date == curr_date)
        curr_games_raw = curr_games_qry.fetch()
        #logging.info(curr_games_raw)
        #logging.info(curr_games_raw)
        responseData = []
        for curr_game in curr_games_raw:
            logging.info(curr_game)
            #curr_pick = Pick.query().filter(Pick.event.id() == curr_game.key.id())
            start_time = curr_game.start_time - datetime.timedelta(hours = 5) # to ET
            start_time = start_time.strftime("%H:%M:%S")
            winner = curr_game.outcome.winner
            if winner is not None:
                winner = winner.id()
            game_data = {
                            'time': start_time,
                            'sport': sport,
                            'game_id': curr_game.key.id(), 
                            'home': curr_game.options[0].get().tri_code, 
                            'home_id': curr_game.options[0].id(), 
                            'away': curr_game.options[1].get().tri_code, 
                            'away_id': curr_game.options[1].id(), 
                            'winner': winner}
            if sport == "nfl":
                game_data['week'] = week
                #logging.info("HELLO")
                logging.info(curr_game.date)
                game_data['date'] = curr_game.date.strftime("%Y%m%d")
            if len(curr_game.outcome.scores) > 0:
                game_data['scores'] = curr_game.outcome.scores
            #start_time = curr_game.start_time.strftime("%H:%M:%S") 
            if user:
                curr_pick_qry = Pick.query().filter(Pick.user_id == user.user_id())
                curr_pick_qry = curr_pick_qry.filter(Pick.event == curr_game.key)
                #print curr_pick_qry
                curr_pick = curr_pick_qry.fetch()
                if len(curr_pick) > 0:
                    game_data['current_pick'] = curr_pick[0].pick.get().tri_code
                    responseData.append(game_data)
                    #responseData.append({'time': start_time, 'game_id': curr_game.key.id(), 'home': curr_game.options[0].get().tri_code, 'home_id': curr_game.options[0].id(), 'away': curr_game.options[1].get().tri_code, 'away_id': curr_game.options[1].id(), 'current_pick': curr_pick[0].pick.get().tri_code, 'winner': curr_game.outcome.winner.id()})
                else:
                    responseData.append(game_data)
                    #responseData.append({'time': start_time, 'game_id': curr_game.key.id(), 'home': curr_game.options[0].get().tri_code, 'home_id': curr_game.options[0].id(), 'away': curr_game.options[1].get().tri_code, 'away_id': curr_game.options[1].id(), 'winner': curr_game.outcome.winner.id()})
            else:
                responseData.append(game_data)
        # MAGIC
        self.response.out.write(json.dumps(responseData))

current_live_data = None
last_polled_ts = datetime.datetime.utcnow()

class LiveGameHandler(webapp2.RequestHandler):
    def get(self):
        global current_live_data
        global last_polled_ts
        #logging.info("HELLO")
        curr_ts = datetime.datetime.utcnow()
        if (curr_ts - last_polled_ts).total_seconds() > 10 or current_live_data is None:
            # poll new
            curr_ts_et = curr_ts - datetime.timedelta(hours = 5)
            if curr_ts_et.time() > datetime.time(0) and curr_ts_et.time() < datetime.time(6):
                curr_date_str = datetime.datetime.strftime(curr_ts_et - datetime.timedelta(hours = 6), "%Y%m%d")
            else:
                curr_date_str = datetime.datetime.strftime(curr_ts_et, "%Y%m%d")
            url =  "http://data.nba.net/data/10s/prod/v1/{}/scoreboard.json".format(curr_date_str)
            #import json
            r = urlfetch.fetch(url)
{'fraction_non_alphanumeric': 0.13283154881797943, 'fraction_numerical': 0.008268843422029047, 'mean_word_length': 2.207752465147909, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2918172', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2996, 'n_words': 481}