Merge remote-tracking branch 'Owain/scrapy'

This commit is contained in:
Owain van Brakel
2022-05-18 23:45:18 +02:00
4 changed files with 53 additions and 7 deletions

View File

@@ -68,7 +68,7 @@ public class App
private static Store cacheStore() throws IOException private static Store cacheStore() throws IOException
{ {
Path path = Paths.get(System.getProperty("user.home"), "jagexcache" + File.separator + "oldschool" + File.separator + "LIVE"); Path path = Paths.get(System.getProperty("user.home"), ".openosrs" + File.separator + "jagexcache" + File.separator + "oldschool" + File.separator + "LIVE");
final File jagexcache = new File(String.valueOf(path)); final File jagexcache = new File(String.valueOf(path));
if (!Files.exists(path)) if (!Files.exists(path))
@@ -80,9 +80,6 @@ public class App
cacheStore.load(); cacheStore.load();
// Try to make this go faster (probably not very smart)
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "100");
return cacheStore; return cacheStore;
} }

View File

@@ -27,6 +27,9 @@ import java.io.UnsupportedEncodingException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.data.App; import net.runelite.data.App;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@@ -35,6 +38,7 @@ import okhttp3.Response;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@Slf4j
public class MediaWiki public class MediaWiki
{ {
private static final class WikiInnerResponse private static final class WikiInnerResponse
@@ -53,6 +57,7 @@ public class MediaWiki
.followSslRedirects(false) .followSslRedirects(false)
.build(); .build();
@Getter
private final HttpUrl base; private final HttpUrl base;
public MediaWiki(final String base) public MediaWiki(final String base)
@@ -75,15 +80,28 @@ public class MediaWiki
try (final Response response = clientNoRedirect.newCall(request).execute()) try (final Response response = clientNoRedirect.newCall(request).execute())
{ {
log.info("original url: {}", url);
if (response.isRedirect()) if (response.isRedirect())
{ {
final String page = response.header("Location") final String page = response.header("Location")
.replace(base.newBuilder().addPathSegment("w").build().toString() + "/", ""); .replace(base.newBuilder().addPathSegment("w").build().toString() + "/", "");
log.info("redirect url: {}", page);
return getPageData(page, section); return getPageData(page, section);
} }
else
{
log.info("unsuccessful: {}", response.code());
if (response.code() == 429)
{
Thread.sleep(2500);
return getSpecialLookupData(type, id, section);
}
}
} }
catch (Exception e) catch (Exception e)
{ {
log.info("exception: {}", e.getMessage());
return ""; return "";
} }
@@ -109,7 +127,7 @@ public class MediaWiki
.addQueryParameter("format", "json") .addQueryParameter("format", "json")
.addQueryParameter("prop", "wikitext") .addQueryParameter("prop", "wikitext")
.addQueryParameter("redirects", "true") .addQueryParameter("redirects", "true")
.addQueryParameter("page", page.replaceAll(" ", "_")); .addQueryParameter("page", page.replaceAll(" ", "_"))
if (section != -1) if (section != -1)
{ {
@@ -129,9 +147,20 @@ public class MediaWiki
final InputStream in = response.body().byteStream(); final InputStream in = response.body().byteStream();
return App.GSON.fromJson(new InputStreamReader(in), WikiResponse.class).parse.wikitext.get("*"); return App.GSON.fromJson(new InputStreamReader(in), WikiResponse.class).parse.wikitext.get("*");
} }
else
{
log.info("page data unsuccessful: {}", response.code());
if (response.code() == 429)
{
Thread.sleep(2500);
return getPageData(page, section);
}
}
} }
catch (Exception e) catch (Exception e)
{ {
log.info("exception page data: {}", e.getMessage());
return ""; return "";
} }

View File

@@ -57,7 +57,10 @@ public class ItemStatsDumper
final Map<Integer, ItemStats> itemStats = new TreeMap<>(); final Map<Integer, ItemStats> itemStats = new TreeMap<>();
final Collection<ItemDefinition> items = itemManager.getItems(); final Collection<ItemDefinition> items = itemManager.getItems();
final Stream<ItemDefinition> itemDefinitionStream = items.parallelStream();
log.info("{}", items.size());
final Stream<ItemDefinition> itemDefinitionStream = items.stream();
itemDefinitionStream.forEach(item -> itemDefinitionStream.forEach(item ->
{ {
@@ -109,6 +112,12 @@ public class ItemStatsDumper
continue; continue;
} }
itemStat.wiki(wiki.getBase().newBuilder()
.addPathSegment("w")
.addPathSegment("Special:Lookup")
.addQueryParameter("type", "item")
.addQueryParameter("id", String.valueOf(item.id))
.build().toString());
itemStat.name(getVarString(base, "name", offset) == null ? getVarString(base, "name1", offset) : getVarString(base, "name", offset)); itemStat.name(getVarString(base, "name", offset) == null ? getVarString(base, "name1", offset) : getVarString(base, "name", offset));
itemStat.quest(getVarBoolean(base, "quest", offset)); itemStat.quest(getVarBoolean(base, "quest", offset));
itemStat.equipable(getVarBoolean(base, "equipable", offset) == null itemStat.equipable(getVarBoolean(base, "equipable", offset) == null
@@ -347,6 +356,7 @@ public class ItemStatsDumper
{ {
static final ItemStats DEFAULT = ItemStats.builder().build(); static final ItemStats DEFAULT = ItemStats.builder().build();
private final String wiki;
private final String name; private final String name;
private final Boolean quest; private final Boolean quest;
private final Boolean equipable; private final Boolean equipable;

View File

@@ -56,6 +56,7 @@ public class NpcStatsDumper
private static final class NpcStats private static final class NpcStats
{ {
private String name; private String name;
private String wiki;
private final Integer hitpoints; private final Integer hitpoints;
private final Integer hitpoints1; private final Integer hitpoints1;
private final Integer combatLevel; private final Integer combatLevel;
@@ -133,7 +134,10 @@ public class NpcStatsDumper
final Map<Integer, NpcStats> npcStats = new HashMap<>(); final Map<Integer, NpcStats> npcStats = new HashMap<>();
final Collection<NpcDefinition> definitions = npcManager.getNpcs(); final Collection<NpcDefinition> definitions = npcManager.getNpcs();
final Stream<NpcDefinition> npcDefinitionStream = definitions.parallelStream();
log.info("{}", definitions.size());
final Stream<NpcDefinition> npcDefinitionStream = definitions.stream();
// Ensure variant names match cache as wiki isn't always correct // Ensure variant names match cache as wiki isn't always correct
final Map<Integer, String> nameMap = new HashMap<>(); final Map<Integer, String> nameMap = new HashMap<>();
@@ -232,6 +236,12 @@ public class NpcStatsDumper
// Update variant name or fall back to current name // Update variant name or fall back to current name
final String curName = nameMap.get(curID); final String curName = nameMap.get(curID);
stats.setName(curName == null ? stats.getName() : curName); stats.setName(curName == null ? stats.getName() : curName);
stats.setWiki(wiki.getBase().newBuilder()
.addPathSegment("w")
.addPathSegment("Special:Lookup")
.addQueryParameter("type", "npc")
.addQueryParameter("id", String.valueOf(n.getId()))
.build().toString());
npcStats.put(curID, stats); npcStats.put(curID, stats);
log.debug("Dumped npc stats for npc id: {}", curID); log.debug("Dumped npc stats for npc id: {}", curID);