codastic
codastic

Reputation: 333

Steam API get historical player count of specific game

I am using steam api with python in order to get the number of players playing a game such as Dota 2.

import requests
import numpy as np
import pandas as pd

def main():

    header = {"Client-ID": "F07D7ED5C43A695B3EBB01C28B6A18E5"}

    appId = 570
    game_players_url = 'https://api.steampowered.com/ISteamUserStats/GetNumberOfCurrentPlayers/v1/?format=json&appid=' + appId
    game_players = requests.get(game_players_url, headers=header)

    print("Game name: Dota 2" + ", Player count: " + str(game_players.json()['response']['player_count']))


if __name__ == '__main__':
    main()

This gets me the correct current number of players for a specific game (in this case dota 2), however what i need is historical data concerning the player count of this specific game. This should be possible, since this site has the information that i desire and they are probably getting their data from the Steam API.

Any help would be greatly appreciated!

Thank you

Upvotes: 7

Views: 6090

Answers (2)

Serpensin
Serpensin

Reputation: 1

I know, that it's been a few years, but I wanted to share my solution, that I've been using for a while. I just came about this post, because I was searching for a better way.

import aiohttp
import http
from bs4 import BeautifulSoup



async def playercount(gameid):
    url = f'https://steamcharts.com/app/{gameid}'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}

    async with aiohttp.ClientSession(headers=headers) as session:
        async with session.get(url) as response:
            if response.status != 200:
                return{"error": {"code": response.status, "message": http.HTTPStatus(response.status).phrase}}
            html = await response.text()

    soup = BeautifulSoup(html, 'html.parser')
    data = {}
    count = 0
    for stats in soup.find_all('div', class_='app-stat'):
        soup2 = BeautifulSoup(str(stats), 'html.parser')
        for stat in soup2.find_all('span', class_='num'):
            stat = str(stat).replace('<span class="num">', '').replace('</span>', '')
            if count == 0:
                data['Current Players'] = stat
            elif count == 1:
                data['Peak Players 24h'] = stat
            elif count == 2:
                data['Peak Players All Time'] = stat
            count += 1
    return data

Upvotes: 0

EliteRaceElephant
EliteRaceElephant

Reputation: 8162

ilhicas pointed it out correctly in the comments: SteamDB has this historical data because they have been collecting and saving it for years, every single day. Official release for SteamDB was around 2010 so that's why they have so much data.

I have had a similar problem, looked around extensiveley and came to this conclusion:

There is no Steam Web API method for historical player count of a specific game.

In case you do not believe me:

Upvotes: 6

Related Questions