How to get "last played on" for Steam game using Steam API
Asked Answered
T

4

6

I'm developing an App that uses public Steam API for collect some information. Currently I retrieve the achievements by calling GetPlayerAchievements (v0001) and total hours played calling GetOwnedGames (v0001). This works fine.

But now I need to known also what was the last played data from a game, for example if you enter on a profile page you can see this info in html page (http://steamcommunity.com/profiles/your_steam_id_here/)

Reviewing the Steam API documentation I cannot find any api call to retrieve this information. So, can this only be obtained scraping the user profile web?

Thermopile answered 9/1, 2015 at 14:26 Comment(2)
can you get it with GetRecentlyPlayedGames (v0001)?Protection
No i can't. GetRecentlyPlayedGames (v0001) only shows data from last 2 weeks palyed games. I get <response> <total_count>0</total_count> </response> but i can not get than on 17 November 2014 the user profile played Skyrim, for example.Thermopile
T
9

Surely, this data must exist within Valve's servers, but they just don't expose it for whatever reason. For now, it appears that this data is only available from the Steam client on a user's PC. After some digging, I was able to find that lastplayed timestamps are available from the following file:

\steam\userdata\{steam_id}\config\localconfig.vdf

Note, the .vdf file is a text file that can be opened in a text editor. Here's an example of one of the timestamps (Just Cause 3):

"225540"
{
    "LastPlayed"        "1466894369"
}

The timestamp is in epoch format and translates to 6/25/2016, 6:39:29 PM GMT-4:00 DST

You could have users upload this file from their computer and then you could parse it on your server to get the last played timestamps. It's not ideal, but it is a start.

I'm not sure what your programming language of choice is for your project, but here are some VDF parsers that can get you started:
C#:
https://github.com/sanmadjack/VDF
NodeJS:
https://www.npmjs.com/package/vdf
Python:
https://gist.github.com/strycore/5735482

EDIT April 29, 2017:
I have now discovered that it's possible to scrape this information from a user's steam games page: http://steamcommunity.com/id/pcmantinker/games/?tab=all

By inspecting the source code of the page, I noticed that there is a JavaScript object for rendering the games. Within this object, each game has a "last_played" field available. One entry looks like this:

{
   "appid":346110,
   "name":"ARK: Survival Evolved",
   "logo":"http:\/\/cdn.edgecast.steamstatic.com\/steamcommunity\/public\/images\/apps\/346110\/58a660ddb7ed1864656ec65e4c18d6edd3bbf512.jpg",
   "friendlyURL":346110,
   "availStatLinks":{
      "achievements":true,
      "global_achievements":true,
      "stats":false,
      "leaderboards":false,
      "global_leaderboards":false
   },
   "hours":"0.5",
   "hours_forever":"84",
   "last_played":1492447993
}

In order to parse this information, you'll need to find the beginning and the end of the string and then parse the JSON to an object you can manipulate. At this time, the beginning of the string is "var rgGames = [" and the end of the string is "];". I know this not ideal, but it does allow you to obtain this information without the need for the Steam client to be installed.

Tarver answered 26/6, 2016 at 20:43 Comment(7)
That's a nice trick. However, depending on the app it might not have access to the file system.Dropout
I imagine that the appropriate file permissions could be granted through some sort of elevation during installation. Most application installers require elevation in order to change the file system, add registry keys, etc.Tarver
I've updated my answer to include a nicer approach for getting last played timestamps from a webpage. This should be the preferred method as it doesn't require Steam to be installed on the user's machine.Tarver
This only works if you are logged into Steam. I wouldn't like to have scraper try to do that, Steam is pretty fiddly regarding securityNuli
I just tested my JavaScript method and verified that my account doesn't need to be logged in for my profile. Could it be a privacy setting per user?Tarver
is there a way to get more granular session level data? Like Played game X, at time Y for duration Z?Landwehr
It looks like the official Steam API now includes rtime_last_played in the GetOwnedGames endpoint. As others have said, I don't believe you can get this information from the HTML in this way anymore.Tarver
P
2

As of 3/9/2023, the answers using rgGames no longer work. Valve has updated that page and it appears to no longer expose that javascript variable.

However, the Steam API response for GetOwnedGames now contains the field rtime_last_played. This appears to be the last time the game was played, in UTC unix time in seconds.

For example, the query

https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?access_token={YOUR_ACCESS_TOKEN}&steamid={STEAMID}

now returns a response like:

{
  "response": {
    "game_count": 1,
    "games": [
      {
        "appid": 286160,
        "playtime_2weeks": 89,
        "playtime_forever": 7167,
        "playtime_windows_forever": 7108,
        "playtime_mac_forever": 0,
        "playtime_linux_forever": 0,
        "rtime_last_played": 1677310875
      }
    ]
  }
}

Thanks, Valve! Glad to have this as part of the official API now -- no need to scrape.

steamapi.xpaw.me is a great way to explore these APIs. It appears to include parameters that are not on the official documentation on the Valve Developer Community Wiki and the SteamWorks Web Api documentation

Perfect answered 10/3, 2023 at 6:53 Comment(0)
H
1

Based on @cameron-tinker's post (as of June 2022), this JS in the browser console for a URL like this http://steamcommunity.com/id/pcmantinker/games/?tab=all pulls out the most recent item:

rgGames.filter(g => g.last_played > 0).sort((a,b) => b.last_played - a.last_played)[0]

Then you can wrap it in a date parser to print the human-friendly date.

new Date(rgGames.filter(g => g.last_played > 0).sort((a,b) => b.last_played - a.last_played)[0].last_played * 1000)
Herriot answered 13/6, 2022 at 11:1 Comment(0)
D
0

This information currently isn't available on the Steam API. I was looking for it myself to sort the list of owned games.

Dropout answered 19/3, 2016 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.