Skip to content

Instantly share code, notes, and snippets.

@justinwoo
Created January 15, 2013 22:18
Show Gist options
  • Save justinwoo/4542655 to your computer and use it in GitHub Desktop.
Save justinwoo/4542655 to your computer and use it in GitHub Desktop.
Looks up specified games on steam, steamappidlookup package used by using ApplistEngine methods .addEntry(Integer) and getting an ArrayList<string> returned from .getEntries() to get strings formatted ###: "appname". THAT'S RIGHT I THROW UP EXCEPTIONS IN YOUR FACE, DEAL WITH IT.
package steamappidlookup;
// doesn't actually help us get any better compiled code
// only for testing's sake
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.net.URL;
import java.net.URLConnection;
public class ApplistEngine {
private String getSTEAMApplistHTMLContents()
{
// find index of this: "app": [
// find index of this: ] (STARTING BACKWARDS)
// those are the bounds we need
try
{
URLConnection connection = new URL("http://api.steampowered.com/ISteamApps/GetAppList/v0001/").openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
scanner.useDelimiter("\\Z");
String content = scanner.next();
int first = content.indexOf('[');
int second = content.lastIndexOf(']');
return content.substring(first + 2, second - 4);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
private HashMap<Integer, ApplistEntry> entries;
public ApplistEngine()
{
entries = new HashMap<Integer, ApplistEntry>();
}
public void addEntry(Integer appid)
{
entries.put(appid, new ApplistEntry(appid));
}
public void addEntries(ArrayList<Integer> appidlist)
{
for(Integer appid : appidlist)
{
entries.put(appid, new ApplistEntry(appid));
}
}
private void getNamesandAddtoEntries()
{
// get names from JSON: api.steampowered.com/ISteamApps/GetAppList/v0001/
String masterlist = getSTEAMApplistHTMLContents();
for(Map.Entry<Integer, ApplistEntry> entry : entries.entrySet())
{
String appidprefix = "\"appid\": ";
String nameprefix = "\"name\": ";
int beginindex = masterlist.indexOf(appidprefix + entry.getKey().toString());
if(beginindex != -1)
{
int namepos = beginindex + masterlist.substring(beginindex).indexOf(nameprefix);
int endpos = namepos + masterlist.substring(namepos).indexOf('\n');
String name = masterlist.substring(namepos + nameprefix.length(), endpos);
entry.getValue().setName(name);
}
else
{
System.out.println(entry.getKey().toString() + "failed searching?");
System.out.println(appidprefix + entry.getKey().toString());
}
}
}
public ArrayList<String> getEntries()
{
getNamesandAddtoEntries();
ArrayList<String> returnlist = new ArrayList<String>();
for(Map.Entry<Integer, ApplistEntry> entry : entries.entrySet())
{
// the spaghetti
returnlist.add(entry.getKey().toString() + ": " + entry.getValue().getName());
}
return returnlist;
}
}
package steamappidlookup;
public class ApplistEntry
{
private int appid;
private String name;
public ApplistEntry(int appid)
{
this.appid = appid;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAppid()
{
return appid;
}
}
import java.util.ArrayList;
import steamappidlookup.*;
public class ExampleUsage {
public static void main(String[] args)
{
ApplistEngine engine = new ApplistEngine();
engine.addEntry(730); // CS:GO
engine.addEntry(4830); // S.T.A.L.K.E.R.
engine.addEntry(5070); // Dawn of War II Trailer
engine.addEntry(34440); // Civilization 4
engine.addEntry(81181); // F1 2011 Trailer
ArrayList<String> entries = engine.getEntries();
for(String entry : entries)
{
System.out.println(entry);
}
}
}
/* output:
730: "Counter-Strike: Global Offensive"
81181: "F1 2011 Launch Trailer PEGI"
5070: "Dawn of War II: Trailer"
34440: "Sid Meier's Civilization IV"
4830: "S.T.A.L.K.E.R."
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment