Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 697
user3122306 Avatar asked Jan 09 '15 14:01

user3122306


People also ask

How can I tell the last time I played a game on Steam?

In your Steam Library, select "List View" from the icons near the top-right of screen, or select "Games List" from the "View" menu. The "Last Played" dates are shown there.

Does Steam keep track of hours for non Steam games?

Due to Steamworks and the Steam AppID it can track and add the playtime of Steam games. Non-Steam games don't have Steamworks, nor a Steam AppID. Through the overlay one can see playtime, but only for that session.

Is Steam API necessary?

The Steamworks API Reference catalogs and documents every interface, function, callback, and type supported in the API. Integration with the Steamworks API is never required to ship your product on Steam, but it is highly recommended as it allows you to accomplish many interactions that Steam users expect.


2 Answers

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.

like image 98
Cameron Tinker Avatar answered Sep 22 '22 14:09

Cameron Tinker


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)
like image 42
Nick Avatar answered Sep 18 '22 14:09

Nick