|
| 1 | +package net.earthcomputer.clientcommands; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import net.minecraft.ChatFormat; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.io.UnsupportedEncodingException; |
| 10 | +import java.net.*; |
| 11 | +import java.util.Locale; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.regex.Matcher; |
| 14 | +import java.util.regex.Pattern; |
| 15 | + |
| 16 | +public class WikiRetriever { |
| 17 | + |
| 18 | + private static final String WIKI_HOST = "https://minecraft.gamepedia.com/"; |
| 19 | + private static final String PAGE_SUMMARY_QUERY = WIKI_HOST + "api.php?action=query&prop=extracts&exintro=true&format=json&titles=%s"; |
| 20 | + private static final Pattern HTML_TAG_PATTERN = Pattern.compile("<\\s*(/)?\\s*(\\w+).*?>", Pattern.DOTALL); |
| 21 | + private static final ChatFormat CODE_COLOR = ChatFormat.DARK_GREEN; |
| 22 | + private static final Gson GSON = new Gson(); |
| 23 | + |
| 24 | + public static String decode(String html) { |
| 25 | + Matcher matcher = HTML_TAG_PATTERN.matcher(html); |
| 26 | + StringBuffer raw = new StringBuffer(); |
| 27 | + |
| 28 | + boolean bold = false, italic = false, underline = false, code = false; |
| 29 | + |
| 30 | + // -1 for not in list, 0 for unordered list, >= 1 for ordered list |
| 31 | + int listIndex = -1; |
| 32 | + |
| 33 | + while (matcher.find()) { |
| 34 | + matcher.appendReplacement(raw, ""); |
| 35 | + |
| 36 | + boolean endTag = matcher.group(1) != null; |
| 37 | + String tagName = matcher.group(2).toLowerCase(Locale.ENGLISH); |
| 38 | + |
| 39 | + if (!endTag) { |
| 40 | + switch (tagName) { |
| 41 | + case "b": |
| 42 | + raw.append(ChatFormat.BOLD); |
| 43 | + bold = true; |
| 44 | + break; |
| 45 | + case "i": |
| 46 | + raw.append(ChatFormat.ITALIC); |
| 47 | + italic = true; |
| 48 | + break; |
| 49 | + case "u": |
| 50 | + case "dt": |
| 51 | + raw.append(ChatFormat.UNDERLINE); |
| 52 | + underline = true; |
| 53 | + break; |
| 54 | + case "code": |
| 55 | + raw.append(CODE_COLOR); |
| 56 | + if (bold) raw.append(ChatFormat.BOLD); |
| 57 | + if (italic) raw.append(ChatFormat.ITALIC); |
| 58 | + if (underline) raw.append(ChatFormat.UNDERLINE); |
| 59 | + code = true; |
| 60 | + break; |
| 61 | + case "dd": |
| 62 | + raw.append(" "); |
| 63 | + break; |
| 64 | + case "ul": |
| 65 | + listIndex = 0; |
| 66 | + break; |
| 67 | + case "ol": |
| 68 | + listIndex = 1; |
| 69 | + break; |
| 70 | + case "li": |
| 71 | + if (listIndex >= 1) { |
| 72 | + raw.append(" ").append(listIndex).append(". "); |
| 73 | + listIndex++; |
| 74 | + } else { |
| 75 | + raw.append(" \u2022"); |
| 76 | + } |
| 77 | + break; |
| 78 | + case "br": |
| 79 | + raw.append("\n"); |
| 80 | + } |
| 81 | + } else { |
| 82 | + switch (tagName) { |
| 83 | + case "b": |
| 84 | + if (code) raw.append(CODE_COLOR); |
| 85 | + else raw.append(ChatFormat.RESET); |
| 86 | + if (italic) raw.append(ChatFormat.ITALIC); |
| 87 | + if (underline) raw.append(ChatFormat.UNDERLINE); |
| 88 | + bold = false; |
| 89 | + break; |
| 90 | + case "i": |
| 91 | + if (code) raw.append(CODE_COLOR); |
| 92 | + else raw.append(ChatFormat.RESET); |
| 93 | + if (bold) raw.append(ChatFormat.BOLD); |
| 94 | + if (underline) raw.append(ChatFormat.UNDERLINE); |
| 95 | + italic = false; |
| 96 | + break; |
| 97 | + case "dt": |
| 98 | + raw.append("\n"); |
| 99 | + //fallthrough |
| 100 | + case "u": |
| 101 | + if (code) raw.append(CODE_COLOR); |
| 102 | + else raw.append(ChatFormat.RESET); |
| 103 | + if (bold) raw.append(ChatFormat.BOLD); |
| 104 | + if (italic) raw.append(ChatFormat.ITALIC); |
| 105 | + underline = false; |
| 106 | + break; |
| 107 | + case "code": |
| 108 | + raw.append(ChatFormat.RESET); |
| 109 | + if (bold) raw.append(ChatFormat.BOLD); |
| 110 | + if (italic) raw.append(ChatFormat.ITALIC); |
| 111 | + if (underline) raw.append(ChatFormat.UNDERLINE); |
| 112 | + code = false; |
| 113 | + break; |
| 114 | + case "ul": |
| 115 | + case "ol": |
| 116 | + listIndex = -1; |
| 117 | + break; |
| 118 | + case "dd": |
| 119 | + case "li": |
| 120 | + case "br": |
| 121 | + case "p": |
| 122 | + raw.append("\n"); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + matcher.appendTail(raw); |
| 128 | + |
| 129 | + String rawStr = raw.toString(); |
| 130 | + rawStr = rawStr.replace(""", "\""); |
| 131 | + rawStr = rawStr.replace("'", "'"); |
| 132 | + rawStr = rawStr.replace("<", "<"); |
| 133 | + rawStr = rawStr.replace(">", ">"); |
| 134 | + rawStr = rawStr.replace("&", "&"); |
| 135 | + |
| 136 | + return rawStr; |
| 137 | + } |
| 138 | + |
| 139 | + public static String getWikiSummary(String pageName) { |
| 140 | + URL url; |
| 141 | + try { |
| 142 | + String encodedPage = URLEncoder.encode(pageName, "UTF-8"); |
| 143 | + url = new URL(String.format(PAGE_SUMMARY_QUERY, encodedPage)); |
| 144 | + } catch (UnsupportedEncodingException | MalformedURLException e) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + QueryResult result; |
| 149 | + try (InputStream in = url.openConnection().getInputStream()) { |
| 150 | + result = GSON.fromJson(new InputStreamReader(in), QueryResult.class); |
| 151 | + } catch (IOException e) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + if (result.query.pages.isEmpty()) |
| 156 | + return null; |
| 157 | + QueryResult.Query.Page page = result.query.pages.values().iterator().next(); |
| 158 | + if (page.missing != null || page.extract == null) |
| 159 | + return null; |
| 160 | + String html = page.extract; |
| 161 | + return decode(html); |
| 162 | + } |
| 163 | + |
| 164 | + private static class QueryResult { |
| 165 | + public String batchcomplete; |
| 166 | + public Query query; |
| 167 | + private static class Query { |
| 168 | + private Map<String, Page> pages; |
| 169 | + private static class Page { |
| 170 | + public int pageid; |
| 171 | + public String title; |
| 172 | + public String extract; |
| 173 | + public String missing; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments