import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.*;
public class HiScores {
public static HashMap<String, Stats> find(String searchPhrase){
HashMap<String, Stats> statsHashMap = new HashMap<>();
try {
//Jsoup closes the connection by its own, after the request is done
Document document = Jsoup
.connect("https://secure.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws?user1=" + searchPhrase.replaceAll(" ", "%20"))
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
.timeout(3000)
.get();
//Select the first table.
Element table = document.select("table").get(0);
//Grab our rows of td from Element table
Elements rows = table.select("td");
//Iterator to remove unnecessary data
Iterator<Element> elementIterator = rows.iterator();
//List holds our skill type names
List<String> typesAvailable = new ArrayList<>();
while (elementIterator.hasNext()){
Element node = elementIterator.next();
String attributesString = node.attributes().toString();
String text = node.text();
//Remove unneeded elements
if (attributesString.contains(" valign=\"top\"") || attributesString.contains(" align=\"left\"")
|| attributesString.contains(" width=") || attributesString.contains(" colspan=")
|| text.getBytes().length == 0 || Character.isLetter(text.charAt(0))){
//Add our skill names to the map
if (attributesString.contains(" align=\"left\"") && !attributesString.contains(" valign=\"top\"")){
System.out.println("Text available: " + text);
System.out.println("Type added: " + text);
typesAvailable.add(text);
statsHashMap.put(text, new Stats(0, 0, 0));
}
System.out.println("Removed: " + node.attributes());
elementIterator.remove();
}
}
System.out.println("\n" + "Personal scores for: " + searchPhrase);
String type, rank, level, xp;
//Loop through our elements and submit innertext to statsHashMap
for (int i = 0, j = 0; i < rows.size() && j < typesAvailable.size(); i += 3, j++) {
//Get type from typesAvailable
type = typesAvailable.get(j);
//Remove commas from our innertext data
rank = rows.get(i).text().replaceAll(",", "");
level = rows.get(i + 1).text().replaceAll(",", "");
xp = rows.get(i + 2).text().replaceAll(",", "");
//Debug
System.out.println("Type: " + type);
System.out.println("Rank: " + rank);
System.out.println("Level: " + level);
System.out.println("Xp: " + xp + "\n");
//Add to our stats hashmap
statsHashMap.put(type, new Stats(Integer.parseInt(rank), Integer.parseInt(level), Integer.parseInt(xp)));
}
} catch (IOException e) {
e.printStackTrace();
}
return statsHashMap;
}
public static class Stats {
private int rank;
private int level;
private int exp;
public Stats(int rank, int level, int exp) {
this.rank = rank;
this.level = level;
this.exp = exp;
}
public int getRank() {
return this.rank;
}
public int getLevel() {
return this.level;
}
public int getExp() {
return this.exp;
}
}
//This uses Jaunt API but does not work inside OSbot scripts! Still was fun to learn because the API can fill forms and such
/*public static HashMap<String, Stats> find(String searchPhrase){
HashMap<String, Stats> statsHashMap = new HashMap<>();
try {
/* API: http://jaunt-api.com/javadocs/index.html
Source: http://jaunt-api.com/
Usage: Import Source as JAR
Warning: Ignores Overall statistics
//Jaunt useragent connection and user submit
UserAgent userAgent = new UserAgent();
userAgent.setCacheEnabled(false);
userAgent.visit("https://secure.runescape.com/m=hiscore_oldschool/hiscorepersonal.ws");
userAgent.doc.apply(searchPhrase);
userAgent.doc.submit("Search");
//Find table data elements and populate list
Elements table = userAgent.doc.findEach("<td align=\"right\">");
List<Element> elementsList = table.toList();
List<String> typesAvailable = new ArrayList<>();
Iterator<Element> elementIterator = elementsList.iterator();
userAgent.close();
//Remove unneeded elements from our List<Element> elementsList
while (elementIterator.hasNext()){
Element element = elementIterator.next();
String innerText = element.innerText();
String innerHTML = element.innerHTML();
if (innerText.getBytes().length == 0 || Character.isLetter(innerText.charAt(0))){
//Grab available types if any (e.g Attack)
if (innerHTML.contains("src=")){
//Format: <img class="miniimg" src="https://www.runescape.com/img/rsp777/hiscores/skill_icon_attack1.gif">
String tableName = innerHTML.substring(innerHTML.indexOf("icon_") + 5, innerHTML.indexOf("1"));
System.out.println("Added type: " + tableName);
typesAvailable.add((tableName.substring(0, 1).toUpperCase() + tableName.substring(1)));
}
System.out.println("Removed: " + innerText);
elementIterator.remove();
}
}
System.out.println("\n" + "Personal scores for: " + searchPhrase);
String type, rank, level, xp;
//Loop through our elements and submit innertext to statsHashMap
for (int i = 0, j = 0; i < elementsList.size() && j < typesAvailable.size(); i += 3, j++) {
//Get type from typesAvailable
type = typesAvailable.get(j);
//Remove commas from our innertext data
rank = elementsList.get(i).innerText().replaceAll(",", "");
level = elementsList.get(i + 1).innerText().replaceAll(",", "");
xp = elementsList.get(i + 2).innerText().replaceAll(",", "");
//Debug
System.out.println("Type: " + type);
System.out.println("Rank: " + rank);
System.out.println("Level: " + level);
System.out.println("Xp: " + xp + "\n");
//Add to our stats hashmap
statsHashMap.put(type, new Stats(Integer.parseInt(rank), Integer.parseInt(level), Integer.parseInt(xp)));
}
System.out.println("Skills map size: " + statsHashMap.size());
} catch (JauntException | IOException e) {
e.printStackTrace();
}
return statsHashMap;
} */
}