nightr/server/nightr/strategies/steam.py

41 lines
1.6 KiB
Python

from datetime import datetime
from pathlib import Path
import requests
from ..util import Context, Prediction
def dota2_players(context: Context) -> Prediction:
"""
How many players are playing Dota 2?
"""
p = Prediction()
# Find the historic data closest matching the current number of players. Lol yolo
current_players = get_dota2_players()
with Path(__file__).parent.joinpath("dotaplayers 2019-04-06 13:43:33.074496.csv").open() as csv:
best_players, best_dt = min(csv, key=lambda l: abs(int(l.rstrip().split(", ")[0]) - current_players)).rstrip().split(", ")
best_match_players = int(best_players)
best_match_datetime = datetime.strptime(best_dt, "%Y-%m-%d %H:%M:%S.%f")
night = best_match_datetime.hour < 6 or best_match_datetime.hour >= 22
night_description = "night" if night else "day"
p.reasons.append(f"{current_players} people are currently playing Dota 2.")
p.reasons.append(f"On the {night_description} of {best_match_datetime.strftime('%B %d')}, {best_match_players} were playing Dota 2.")
p.reasons.append(f"Therefore, it must currently be {night_description}time.")
p.probability = float(night) # TODO: actual float lol gg
return p
def get_dota2_players():
header = {"Client-ID": "F07D7ED5C43A695B3EBB01C28B6A18E5"}
appId = 570
game_players_url = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?format=json&appid=' + str(appId)
game_players = requests.get(game_players_url, headers=header)
players_str = str(game_players.json()['response']['player_count'])
return int(players_str)