-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand the !wiki command to enable searching the wiki.
- Loading branch information
Showing
3 changed files
with
236 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/javacord/bot/util/wiki/parser/WikiPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.javacord.bot.util.wiki.parser; | ||
|
||
public class WikiPage implements Comparable<WikiPage> { | ||
|
||
private final String title; | ||
private final String[] keywords; | ||
private final String url; | ||
private final String content; | ||
|
||
public WikiPage(String title, String[] keywords, String url, String content) { | ||
this.title = title; | ||
this.keywords = keywords; | ||
this.url = url; | ||
this.content = content; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String[] getKeywords() { | ||
return keywords; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
@Override | ||
public int compareTo(WikiPage that) { | ||
return this.title.compareTo(that.title); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/org/javacord/bot/util/wiki/parser/WikiParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.javacord.bot.util.wiki.parser; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import org.javacord.api.DiscordApi; | ||
|
||
import java.io.IOException; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
|
||
public class WikiParser { | ||
|
||
public static final String API_URL = "https://javacord.org/api/wiki.json"; | ||
public static final String BASE_URL = "https://javacord.org"; // the /wiki/ part of the url will be returned by the API | ||
|
||
private static final OkHttpClient client = new OkHttpClient(); | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private final DiscordApi discordAPi; | ||
private final String apiUrl; | ||
private final String baseUrl; | ||
|
||
public WikiParser(DiscordApi api) { | ||
this(api, API_URL, BASE_URL); | ||
} | ||
|
||
public WikiParser(DiscordApi api, String apiUrl, String baseUrl) { | ||
this.discordAPi = api; | ||
this.apiUrl = apiUrl; | ||
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length()-1) : baseUrl; | ||
} | ||
|
||
public CompletableFuture<Set<WikiPage>> getPages() { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return getPagesBlocking(); | ||
} catch (Throwable t) { | ||
throw new CompletionException(t); | ||
} | ||
}, discordAPi.getThreadPool().getExecutorService()); | ||
} | ||
|
||
private Set<WikiPage> getPagesBlocking() throws IOException { | ||
Request request = new Request.Builder() | ||
.url(apiUrl) | ||
.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
ResponseBody body = response.body(); | ||
Set<WikiPage> pages = new HashSet<>(); | ||
if (body == null) { | ||
return pages; | ||
} | ||
JsonNode array = mapper.readTree(body.charStream()); | ||
if (!array.isArray()) { | ||
throw new AssertionError("Format of Wiki page list not as expected"); | ||
} | ||
for (JsonNode node : array) { | ||
if (node.has("title") && node.has("keywords") && node.has("url") && node.has("content")) { | ||
pages.add(new WikiPage( | ||
node.get("title").asText(), | ||
asStringArray(node.get("keywords")), | ||
node.get("url").asText(), | ||
node.get("content").asText() | ||
)); | ||
} else { | ||
throw new AssertionError("Format of Wiki page list not as expected"); | ||
} | ||
} | ||
return pages; | ||
} | ||
|
||
private String[] asStringArray(JsonNode arrayNode) { | ||
if (!arrayNode.isArray()) { | ||
return new String[] {}; | ||
} | ||
String[] result = new String[arrayNode.size()]; | ||
int i = 0; | ||
for (JsonNode node : arrayNode) { | ||
result[i++] = node.asText(); | ||
} | ||
return result; | ||
} | ||
|
||
} |