Scrapelist The art of content scraping

26Feb/110

Display Steam games list on your website

Here is a simple PHP function to scrape your games list off your Steam profile. You can select games from the recent, all, and wishlist tab lists.

By looking at the source code of your profile's games list, you'll notice that every game title is wrapped in the h4 element. No other content on the page uses the h4 element so it is easy to extract the titles. Using the preg_match_all() function you can grab all text between the h4 tags and store it in an array.

There are also a few XML sources for Steam community data that include Player Profile, Player Game Stats, Leaderboards, and Groups. Also available is a Team Fortress 2 API

Usage:
$steamname is your Steam nickname id that is in the URL of your public profile. http://steamcommunity.com/id/nickname
$list argument can accept all, recent, or wishlist

Display Array data for test:

print_r( steam_games_list('nickname','recent') );

The function:

function steam_games_list($steamname,$list="all"){

     //only allow input of exactly recent|all|wishlist
     $list = (preg_match('/^recent$|^all$|^wishlist$/',$list))?$list:"all";

     //wishlist url is different
     if($list == "wishlist"){
     $url = "http://steamcommunity.com/id/".$steamname."/wishlist/";
     }else{
     $url = "http://steamcommunity.com/id/".$steamname."/games/?tab=".$list;
     }

     $games = file_get_contents($url);		

     //all game titles are in H4, no other H4 on page
     preg_match_all("|<h4>(.*?)</h4>|",$games,$match);
     $games = $match[0];

     //remove h4 and other possible tags from name, just in case
     foreach($games as $key=>$val){
	$games[$key] = trim(strip_tags($val));
     }

     return $games;
}
Filed under: Functions, PHP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.