Skip to content

Instantly share code, notes, and snippets.

@Strikeskids
Created April 23, 2013 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Strikeskids/5448336 to your computer and use it in GitHub Desktop.
Save Strikeskids/5448336 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Paris
*/
public class Hiscores {
private final static Map<String, Hiscores> cache = new ConcurrentHashMap<>();
private final static String ROOT = "http://services.---------.com/";
private final static String REFERER = "m=hiscore/heroes.ws";
private final static String PAGE = ROOT + "m=hiscore/compare.ws?user1=%s";
public class SkillStats {
public int level, xp, rank;
public String name;
public SkillStats(final String name, final int level, final int xp, final int rank) {
this.name = name;
this.level = level;
this.xp = xp;
this.rank = rank;
}
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public int getTotalXp() {
return xp;
}
public int getRank() {
return rank;
}
}
public class ActivityStats {
public int score, rank;
public String name;
public ActivityStats(final String name, final int score, final int rank) {
this.name = name;
this.score = score;
this.rank = rank;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public int getRank() {
return rank;
}
}
private final String username;
private final Map<String, SkillStats> skills;
private final Map<String, ActivityStats> activities;
private Hiscores(final String username) throws IOException {
final String html = download(String.format(PAGE, username));
skills = new HashMap<>();
activities = new HashMap<>();
// actual codes removed for commercial reasons
}
public String getUsername() {
return username;
}
public SkillStats getSkills(final String key) {
return skills.get(key);
}
public ActivityStats getActivities(final String key) {
return activities.get(key);
}
public static Hiscores getProfile(String username) {
username = normaliseUsername(username);
if (cache.containsKey(username)) {
return cache.get(username);
}
Hiscores profile = null;
try {
profile = new Hiscores(username);
} catch (final IOException ignored) {
}
cache.put(username, profile);
return profile;
}
protected void clear() {
cache.clear();
}
@SuppressWarnings("deprecation")
private static String normaliseUsername(String s) {
s = s.toLowerCase().replace(" ", "%A0");
try {
return URLEncoder.encode(s, "UTF-8");
} catch (final UnsupportedEncodingException ignored) {
return URLEncoder.encode(s);
}
}
private synchronized String download(final String page) throws IOException {
// not the real download method
final URL url = new URL(page);
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.addRequestProperty("Host", url.getHost());
con.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
con.addRequestProperty("Accept-Encoding", "gzip, deflate");
con.addRequestProperty("Accept-Charset", "ISO-8859-1,UTF-8;q=0.7,*;q=0.7");
con.addRequestProperty("Accept-Language", "en-us,en;q=0.5");
con.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.addRequestProperty("Connection", "close");
con.addRequestProperty("Referer", REFERER);
con.setConnectTimeout(10000);
final int len = con.getContentLength();
final byte[] buf;
if (len != -1) {
buf = new byte[len];
con.getInputStream().read(buf);
} else {
final byte[] x = new byte[0x1000];
final ByteArrayOutputStream bos = new ByteArrayOutputStream(0x7fff);
final InputStream in = con.getInputStream();
while (in.read(x) != -1) {
bos.write(x);
}
buf = bos.toByteArray();
}
con.disconnect();
return new String(buf, "UTF-8");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment