def _fetch_score(self, week): stale_timestamp = ( datetime.datetime.utcnow() - datetime.timedelta(seconds=_ScoreDatastore.__THRESHOLD)) scores = [] result = [] scores = self.__query_scores(week) for game in scores: # Only check for staleness when dealing with current games if week%100 >= utils.default_week(): # Reject data if any of it is stale if game.timestamp <= stale_timestamp: return [] result.append( { d.AWAY_NAME: game.away_name, d.AWAY_SCORE: game.away_score, d.GAME_CLOCK: game.game_clock, d.GAME_DAY: game.game_day, d.GAME_SEASON: game.year, d.GAME_STATUS: game.game_status, d.GAME_TAG: game.game_tag, d.GAME_TIME: game.game_time, d.GAME_WEEK: game.week, d.HOME_NAME: game.home_name, d.HOME_SCORE: game.home_score, d.NFL_GAME_ID: game.game_id, d.SPREAD_MARGIN: game.spread_margin, d.SPREAD_ODDS: game.spread_odds } ) return result
def get(self): spread = SpreadFactory().get_instance() result = {} week_req = self.request.get( d.GAME_WEEK, default_value=utils.default_week()) week = (self._validate_params(d.GAME_WEEK, week_req) or utils.default_week()) result = spread.fetch(week) self.response.headers['Content-Type'] = 'application/json' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.set_status(http_code.OK) self.response.out.write(json.dumps(result, indent = 4))
def post(self): data = {} result = { "data": 0, "Success": "Success", "status_code": 201 } score = ScoreFactory().get_instance() week = utils.default_week() parameters = self.request.POST.items() for item in parameters: value = self._validate_params(item[0], item[1]) if value != None: data[item[0]] = value if d.NFL_GAME_ID in data: if d.GAME_WEEK in data: week = data[d.GAME_WEEK] result['data'] = score.save(week, [data]) self.response.headers['Content-Type'] = 'application/json' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.set_status(result['status_code']) self.response.out.write(json.dumps(result, indent = 4))
def __week_offset(self): current_week = utils.default_week() if current_week <= 0: # Preseason return nfl.WEEK_PREFIX['PRE'] elif current_week > 17: return nfl.WEEK_PREFIX['POS'] else: return nfl.WEEK_PREFIX['REG']
def _convert_to_spread_object(self, owner_name, spread_data): result = { 'year': nfl.YEAR, 'week': utils.default_week(), 'owner': owner_name } for game_id, picks in spread_data.iteritems(): result[game_id] = picks return result
def _get_game_ids_map(self): score_factory = ScoreFactory().get_instance(depth=4) scores = score_factory.fetch(utils.default_week()) result = {} for game in scores: result[game['home_name']] = game['game_id'] result[game['away_name']] = game['game_id'] # Workaround for arizona # TODO: in wrong place; should trust source of truth if 'ARI' in result: result['AZ'] = result['ARI'] return result
def post(self): spread = SpreadFactory().get_instance() data = {} result = { "data": 0, "Success": "Success", "status_code": http_code.CREATED } week = utils.default_week() values = [] key = '' for item in self.request.POST.items(): values = self.request.POST.getall(item[0]) key = item[0] # Keys should only be strings if not isinstance(item[0], basestring): try: key = unicode(item[0]) except ValueError: logging.error("Cannot convert key. Skipping") continue # Trim endings off keys for array objects if key[-2:] == '[]': key = self._trim_keys(key) value = values else: # Can only validate non-list items value = self._validate_params(key, values.pop()) data[key] = value if d.GAME_WEEK in data: week = data[d.GAME_WEEK] result["data"] = spread.save(week, [data]) self.response.headers['Content-Type'] = 'application/json' self.response.headers['Access-Control-Allow-Origin'] = '*' self.response.set_status(result["status_code"]) self.response.out.write(json.dumps(result["data"], indent = 4))
def test_default_week(self): time_delta = datetime.datetime.now() - nfl.WEEK_ONE[nfl.YEAR] current_week = (time_delta.days/7)+1 week = utils.default_week() self.assertEqual(current_week, week)