def post(self): m = self.request.get("month") d = self.request.get("day") y = self.request.get("year") t = self.request.get("time") if m == "" or d == "" or y == "" or t == "": self.response.out.write(RenderAddDatePage(m, d, y, t, "Fill in all the fields")) else: gameDate = GameDate(date=datetime.datetime(month=int(m), day=int(d), year=int(y)), time=t) gameDate.put() memcache.delete("gamedates") values = { "message":"Date added!"} path = os.path.join(os.path.dirname(__file__), 'templates/add_gamedate.html') self.response.out.write(template.render(path, values))
def get_gamedates(years): """ Load the dates and games played on them for the given years supplied :param years: the years to load info for :type years: list[str] """ # For each specified year, look at the dates and games played on them for year in years: season_entry = Season() season_entry.year = year # Get the first day of October as the first possible default date first_date = '{}-10-01'.format(year[:4]) # Iterate until finding first day of regular season while True: print("Looking at {} for first day of season".format(first_date)) gameday = query_nba_api(scoreboardv2.ScoreboardV2, game_date=first_date) game_ids = gameday.available.get_data_frame()['GAME_ID'] # If there were games this day, and it is regular season if len(game_ids) > 0 and game_ids[0][2] == '2': season_entry.first_date = first_date break else: first_date = (datetime.date.fromisoformat(first_date) + timedelta(1)).isoformat() # Begin loading into mongo the game dates date = first_date while True: gamedate_entry = GameDate.objects(date=date) # Game date already exists in database if gamedate_entry: print('{} is already in the database'.format(date)) # Else game date is not already in database else: gameday = query_nba_api(scoreboardv2.ScoreboardV2, game_date=date) game_ids = ( gameday.available.get_data_frame()['GAME_ID'].to_list()) # If all star game, skip if len(game_ids) > 0 and game_ids[0][2] == '3': game_ids = [] # If playoff game, stop and mark previous date as last day if len(game_ids) > 0 and game_ids[0][2] == '4': last_date = (datetime.date.fromisoformat(date) - timedelta(1)).isoformat() season_entry.last_date = last_date if not Season.objects(year=year): season_entry.save() break # Create gameday entry for this day gamedate_entry = GameDate() gamedate_entry.date = date gamedate_entry.year = year if '0021201214' in game_ids: # Remove not played game game_ids.remove('0021201214') gamedate_entry.games = game_ids gamedate_entry.save() print('Adding {} to database with {} games played on ' 'this day'.format(date, len(game_ids))) date = (datetime.date.fromisoformat(date) + timedelta(1)).isoformat()