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?

Brian
  • 14,610
  • 7
  • 35
  • 43
user3122306
  • 245
  • 2
  • 4
  • 15
  • can you get it with `GetRecentlyPlayedGames (v0001)`? – meatspace Jan 09 '15 at 14:31
  • No i can't. GetRecentlyPlayedGames (v0001) only shows data from last 2 weeks palyed games. I get 0 but i can not get than on 17 November 2014 the user profile played Skyrim, for example. – user3122306 Jan 09 '15 at 17:02

4 Answers4

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.

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • That's a nice trick. However, depending on the app it might not have access to the file system. – Edgar Jul 15 '16 at 14:11
  • 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. – Cameron Tinker Jul 15 '16 at 14:22
  • 1
    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. – Cameron Tinker Apr 29 '17 at 17:44
  • 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 security – panta82 Jul 04 '17 at 19:00
  • 1
    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? – Cameron Tinker Jul 04 '17 at 21:02
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)
Nick
  • 2,803
  • 1
  • 39
  • 59
1

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

OmegaJak
  • 13
  • 1
  • 3
0

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

Edgar
  • 4,348
  • 4
  • 40
  • 59