1package com.osroyale.util.tools.wiki.parser;
3import com.google.common.util.concurrent.ListeningExecutorService;
4import com.google.common.util.concurrent.MoreExecutors;
5import com.google.common.util.concurrent.ThreadFactoryBuilder;
6import com.google.gson.Gson;
7import com.google.gson.GsonBuilder;
8import com.google.gson.JsonElement;
9import org.apache.logging.log4j.LogManager;
10import org.jsoup.Jsoup;
11import org.jsoup.nodes.Document;
13import java.io.IOException;
14import java.nio.file.Files;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17import java.util.LinkedList;
18import java.util.concurrent.ExecutorService;
19import java.util.concurrent.Executors;
20import java.util.concurrent.TimeUnit;
62 private static final Path WRITE_PATH = Paths.get(
"./data/wiki");
65 protected static final String
WIKI_LINK =
"http://oldschoolrunescape.wikia.com/wiki/";
67 private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(
WikiTableParser.class);
70 protected static final Gson
GSON =
new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
73 private final ListeningExecutorService executorService;
76 protected final LinkedList<WikiTable>
tables;
81 ExecutorService delegateService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
82 new ThreadFactoryBuilder().setNameFormat(
"WikiTableParserThread").build());
83 executorService = MoreExecutors.listeningDecorator(delegateService);
87 public void begin() throws InterruptedException {
89 logger.info(
"Parsing " + size +
" names from " +
WIKI_LINK);
91 tables.forEach(table -> execute(
new ParserTask(table)));
93 executorService.shutdown();
94 executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
99 protected void execute(Runnable runnable) {
100 executorService.execute(runnable);
103 protected abstract void finish();
111 protected static void writeToJson(String name, JsonElement array) {
113 String json =
GSON.toJson(array);
115 if (!Files.exists(WRITE_PATH)) {
116 Files.createDirectory(WRITE_PATH);
119 Path path = WRITE_PATH.resolve(name +=
".json");
120 Files.write(path, json.getBytes());
122 logger.info(
"Successfully wrote " + path);
123 }
catch (IOException e) {
124 logger.warn(
"Could not save '" + name +
"' to '" + WRITE_PATH +
"'", e);
133 private final class ParserTask
implements Runnable {
151 String link = table.getLink();
152 Document document = Jsoup.connect(link).get();
153 table.parseDocument(document);
154 }
catch (Exception e) {
155 System.out.println(
"Could not parse table from wiki for '" + table.getLink() +
"'");