RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
WikiTableParser.java
1package com.osroyale.util.tools.wiki.parser;
2
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;
12
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;
21
58
59public abstract class WikiTableParser {
60
62 private static final Path WRITE_PATH = Paths.get("./data/wiki");
63
65 protected static final String WIKI_LINK = "http://oldschoolrunescape.wikia.com/wiki/";
66
67 private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(WikiTableParser.class);
68
70 protected static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
71
73 private final ListeningExecutorService executorService;
74
76 protected final LinkedList<WikiTable> tables;
77
79 public WikiTableParser(LinkedList<WikiTable> tables) {
80 this.tables = tables;
81 ExecutorService delegateService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
82 new ThreadFactoryBuilder().setNameFormat("WikiTableParserThread").build());
83 executorService = MoreExecutors.listeningDecorator(delegateService);
84 }
85
87 public void begin() throws InterruptedException {
88 int size = tables.size();
89 logger.info("Parsing " + size + " names from " + WIKI_LINK);
90
91 tables.forEach(table -> execute(new ParserTask(table)));
92
93 executorService.shutdown();
94 executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
95
96 finish();
97 }
98
99 protected void execute(Runnable runnable) {
100 executorService.execute(runnable);
101 }
102
103 protected abstract void finish();
104
111 protected static void writeToJson(String name, JsonElement array) {
112 try {
113 String json = GSON.toJson(array);
114
115 if (!Files.exists(WRITE_PATH)) {
116 Files.createDirectory(WRITE_PATH);
117 }
118
119 Path path = WRITE_PATH.resolve(name += ".json");
120 Files.write(path, json.getBytes());
121
122 logger.info("Successfully wrote " + path);
123 } catch (IOException e) {
124 logger.warn("Could not save '" + name + "' to '" + WRITE_PATH + "'", e);
125 }
126 }
127
133 private final class ParserTask implements Runnable {
134
136 private final WikiTable table;
137
144 private ParserTask(WikiTable table) {
145 this.table = table;
146 }
147
148 @Override
149 public void run() {
150 try {
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() + "'");
156// logger.error("Could not parse table from wiki for '" + table.getLink() + "'", e);
157 }
158 }
159 }
160
161}
static void writeToJson(String name, JsonElement array)