-
Notifications
You must be signed in to change notification settings - Fork 4
Expand the !wiki command to enable searching the wiki. #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
78 changes: 78 additions & 0 deletions
78
src/main/java/org/javacord/bot/util/wiki/parser/WikiPage.java
This file contains hidden or 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,78 @@ | ||
package org.javacord.bot.util.wiki.parser; | ||
|
||
/** | ||
* A class representing a page on the wiki. | ||
*/ | ||
public class WikiPage implements Comparable<WikiPage> { | ||
|
||
private final String title; | ||
private final String[] keywords; | ||
private final String url; | ||
private final String content; | ||
|
||
/** | ||
* Creates a new wiki page. | ||
* | ||
* @param title The title of the page. | ||
* @param keywords The keywords the page is tagged with. | ||
* @param url The URL of the page, relative to the wiki's base URL. | ||
* @param content The content of the page. | ||
*/ | ||
public WikiPage(String title, String[] keywords, String url, String content) { | ||
this.title = title; | ||
this.keywords = keywords; | ||
this.url = url; | ||
this.content = content; | ||
} | ||
|
||
/** | ||
* Gets the title. | ||
* | ||
* @return The title of the page. | ||
*/ | ||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
/** | ||
* Gets the keywords. | ||
* | ||
* @return The keywords for the page. | ||
*/ | ||
public String[] getKeywords() { | ||
return keywords; | ||
} | ||
|
||
/** | ||
* Gets the relative URL. | ||
* | ||
* @return The page URL. | ||
*/ | ||
public String getUrl() { | ||
return url; | ||
} | ||
|
||
/** | ||
* Gets the content. | ||
* | ||
* @return The content of the page. | ||
*/ | ||
public String getContent() { | ||
return content; | ||
} | ||
|
||
/** | ||
* Gets a markdown-formatted link to the page. | ||
* | ||
* @return The markdown for a link to the page. | ||
*/ | ||
public String asMarkdownLink() { | ||
return String.format("[%s](%s)", title, WikiParser.BASE_URL + url); | ||
} | ||
|
||
@Override | ||
public int compareTo(WikiPage that) { | ||
return this.title.compareTo(that.title); | ||
} | ||
|
||
} |
114 changes: 114 additions & 0 deletions
114
src/main/java/org/javacord/bot/util/wiki/parser/WikiParser.java
This file contains hidden or 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,114 @@ | ||
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; | ||
|
||
/** | ||
* A parser for the Javacord wiki. | ||
*/ | ||
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; | ||
|
||
/** | ||
* Creates a new wiki parser. | ||
* | ||
* @param api The Discord Api of which to use the HTTP client. | ||
*/ | ||
public WikiParser(DiscordApi api) { | ||
this(api, API_URL); | ||
} | ||
|
||
/** | ||
* Creates a new Wiki parser. | ||
* | ||
* @param api The Discord Api of which to use the HTTP client. | ||
* @param apiUrl The URL for the json file with the page list. | ||
*/ | ||
public WikiParser(DiscordApi api, String apiUrl) { | ||
this.discordApi = api; | ||
this.apiUrl = apiUrl; | ||
} | ||
|
||
/** | ||
* Gets the pages asynchronously. | ||
* | ||
* @return The pages of the wiki. | ||
*/ | ||
public CompletableFuture<Set<WikiPage>> getPages() { | ||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return getPagesBlocking(); | ||
} catch (Throwable t) { | ||
throw new CompletionException(t); | ||
} | ||
}, discordApi.getThreadPool().getExecutorService()); | ||
} | ||
|
||
/** | ||
* Gets the pages synchronously. | ||
* | ||
* @return The pages of the wiki. | ||
* @throws IOException If the connection to the wiki failed. | ||
*/ | ||
public 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; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.