def _update_monthly(self): """Download and store monthly leaderboard table. Writes full data in data/monthly/ and calls curator.""" data = self.get_board(2) written_date = helpers.utc_to_uae(datetime.datetime.utcnow()) file_date = written_date # convert first day of each month to last day of previous month if written_date.day == 1: file_date = file_date + datetime.timedelta(days=-1) # create filename filename = "data/monthly/" + file_date.strftime("%y-%m") + ".csv" needs_header = False if not os.path.isfile(filename): # file doesn't exist - needs header # it will be written after the file is actually created needs_header = True with open(filename, "a") as data_file: if needs_header: data_file.write("date,player,score") for row in data['board']: data_file.write("\n" + written_date.strftime("%m-%d-%H%M%S") +\ "," + row['name'] +\ "," + row['points']) self.curate_file("monthly", file_date, data)
def _update_weekly(self): """Download and store weekly leaderboard table. Writes full data in data/weekly/ and calls curator.""" data = self.get_board(1) # get current time in the proper timezone cur_date = helpers.utc_to_uae(datetime.datetime.utcnow()) # get current date values weeknum = str(cur_date.isocalendar()[1]) # create filename filename = "data/weekly/" + cur_date.strftime("%y") + "-" + weeknum + ".csv" needs_header = False if not os.path.isfile(filename): # file doesn't exist - needs header # it will be written after the file is actually created needs_header = True with open(filename, "a") as data_file: if needs_header: data_file.write("date,player,score") for row in data['board']: data_file.write("\n" + cur_date.strftime("%m-%d-%H%M%S") +\ "," + row['name'] +\ "," + row['points']) self.curate_file("weekly", cur_date, data)
def _download_board(self, filename, board_type): """ Downloads full leaderboard including storing player profile stats, writes it into a new csv, then calls the submitter. Args: filename: string with path of the output date file board_type: string representing the board timeframe """ if board_type == "Daily": board_num = 0 elif board_type == "Weekly": board_num = 1 elif board_type == "Monthly": board_num = 2 try: data = self.get_board(board_num) except IndexError: print("Error retrieving board") cur_date = helpers.utc_to_uae(datetime.datetime.utcnow()) data_file = open(filename, "w") i = 0 for row in data['board']: i += 1 profile = self.get_profile(row['id'], board_num) data_file.write(str(i)) data_file.write("," + row['name']) data_file.write("," + row['points']) data_file.write("," + profile['win-percent']) data_file.write("," + profile['games']) data_file.write("," + profile['wins']) data_file.write("," + profile['losses']) data_file.write("," + profile['save-percent']) data_file.write("," + profile['tags']) data_file.write("," + profile['popped']) data_file.write("," + profile['grabs']) data_file.write("," + profile['captures']) data_file.write("," + profile['hold']) data_file.write("," + profile['prevent']) data_file.write("," + profile['returns']) data_file.write("," + profile['support']) data_file.write("," + profile['powerup-percent']) data_file.write("," + profile['time']) data_file.write("," + profile['disconnects']) data_file.write("\n") data_file.close() self._submit_board(board_type, cur_date, filename)
def _update_daily(self): """Download and store daily leaderboard table. Writes full data in data/daily/ and calls curator.""" data = self.get_board(0) cur_date = helpers.utc_to_uae(datetime.datetime.utcnow()) needs_header = False if not os.path.isfile("data/daily/" + cur_date.strftime("%y-%m-%d") + ".csv"): # file doesn't exist - needs header # it will be written after the file is actually created needs_header = True with open("data/daily/" + cur_date.strftime("%y-%m-%d") + ".csv", "a") as data_file: if needs_header: data_file.write("date,player,score") for row in data['board']: data_file.write("\n" + cur_date.strftime("%H%M%S") +\ "," + row['name'] +\ "," + row['points']) self.curate_file("daily", cur_date, data)
def scheduler(self): """Main loop - runs threaded leaderboard operations.""" while True: # get current time in the proper timezone cur_date = helpers.utc_to_uae(datetime.datetime.utcnow()) # get current date values minute = cur_date.minute hour = cur_date.hour weekday = cur_date.weekday() weeknum = str(cur_date.isocalendar()[1]) day = cur_date.day # update all the data threading.Thread(target=self._update_monthly).start() threading.Thread(target=self._update_weekly).start() threading.Thread(target=self._update_daily).start() # monthly: called during the first of the month from 23:40-23:49 if day == 1 and hour == 23 and minute >= 40 and minute <= 49: # get object of yesterday's date to have the previous month yesterday = cur_date + datetime.timedelta(days=-1) filename = "static/history/monthly/" + yesterday.strftime("%y-%m") + ".csv" threading.Thread(target=self._download_board, args=(filename, "Monthly")).start() # weekly: called every sunday from 23:40-23:49 if weekday == 6 and hour == 23 and minute >= 40 and minute <= 49: filename = "static/history/weekly/" + cur_date.strftime("%y") + "-" + weeknum + ".csv" threading.Thread(target=self._download_board, args=(filename, "Weekly")).start() # daily: called everyday from 23:40-23:49 if hour == 23 and minute >= 40 and minute <= 49: filename = "static/history/daily/" + cur_date.strftime("%y-%m-%d") + ".csv" threading.Thread(target=self._download_board, args=(filename, "Daily")).start() time.sleep(600) # 600 seconds = 10 minutes
def history_monthly(): return render_template( "history/monthly.html", time=helpers.utc_to_uae(datetime.datetime.utcnow()).strftime("%H:%M:%S"), date=request.args.get("date", ""), )
def graphs_weekly(): return render_template( "graphs/weekly.html", time=helpers.utc_to_uae(datetime.datetime.utcnow()).strftime("%H:%M:%S"), date=request.args.get("date", ""), )
def about(): return render_template("about.html", time=helpers.utc_to_uae(datetime.datetime.utcnow()).strftime("%H:%M:%S"))
def home(): # uptime = time.time() - start_time return render_template("home.html", time=helpers.utc_to_uae(datetime.datetime.utcnow()).strftime("%H:%M:%S"))