1313import java .net .URL ;
1414import java .net .URLEncoder ;
1515import java .nio .charset .StandardCharsets ;
16+ import java .util .Arrays ;
17+ import java .util .List ;
1618import java .util .Locale ;
1719import java .util .Map ;
1820import java .util .regex .Matcher ;
1921import java .util .regex .Pattern ;
22+ import java .util .stream .Collectors ;
2023
2124public class WikiRetriever {
2225
2326 private static final String WIKI_HOST = "https://minecraft.wiki/" ;
27+ private static final String SEARCH_QUERY = WIKI_HOST + "api.php?action=query&list=search&srlimit=1&srprop=snippet&format=json&srsearch=%s" ;
2428 private static final String PAGE_SUMMARY_QUERY = WIKI_HOST + "api.php?action=query&prop=extracts&exintro=true&format=json&titles=%s" ;
2529 private static final Pattern HTML_TAG_PATTERN = Pattern .compile ("<\\ s*(/)?\\ s*(\\ w+).*?>|<!--.*?-->|\n " , Pattern .DOTALL );
2630 private static final ChatFormatting CODE_COLOR = ChatFormatting .DARK_GREEN ;
@@ -184,19 +188,78 @@ public static String decode(String html) {
184188 }
185189
186190 @ Nullable
187- public static String getWikiSummary (String pageName ) {
191+ public static QueryResult getResult (URL url ) {
192+ QueryResult result ;
193+ try (InputStream in = url .openConnection ().getInputStream ()) {
194+ result = GSON .fromJson (new InputStreamReader (in ), QueryResult .class );
195+ } catch (IOException e ) {
196+ return null ;
197+ }
198+ return result ;
199+ }
200+
201+ @ Nullable
202+ public static URL buildURL (String page , String query ) {
203+ String result = Arrays .stream (page .split ("\\ s+" ))
204+ .map (w -> w .substring (0 , 1 ).toUpperCase (Locale .ROOT )
205+ + w .substring (1 ).toLowerCase (Locale .ROOT ))
206+ .collect (Collectors .joining (" " ));
207+
208+
188209 URL url ;
189210 try {
190- String encodedPage = URLEncoder .encode (pageName , StandardCharsets .UTF_8 );
191- url = URI .create (String .format (PAGE_SUMMARY_QUERY , encodedPage )).toURL ();
211+ String encodedPage = URLEncoder .encode (result , StandardCharsets .UTF_8 );
212+ url = URI .create (String .format (query , encodedPage )).toURL ();
192213 } catch (MalformedURLException e ) {
193214 return null ;
194215 }
216+ return url ;
217+ }
218+
219+ @ Nullable
220+ public static String searchArticleName (String pageInput ){
221+ URL url = buildURL (pageInput .trim (), SEARCH_QUERY );
222+ if (url == null ) {
223+ return null ;
224+ }
195225
196- QueryResult result ;
197- try (InputStream in = url .openConnection ().getInputStream ()) {
198- result = GSON .fromJson (new InputStreamReader (in ), QueryResult .class );
199- } catch (IOException e ) {
226+ QueryResult result = getResult (url );
227+ if (result == null ) {
228+ return null ;
229+ }
230+
231+ var query = result .query ;
232+ if (query == null ) {
233+ return null ;
234+ }
235+
236+ var search = query .search ;
237+ var searchinfo = query .searchinfo ;
238+ if (search == null || search .isEmpty () || searchinfo == null || searchinfo .totalhits == 0 ) {
239+ return null ;
240+ }
241+
242+ if (searchinfo .suggestion != null ) {
243+ return searchinfo .suggestion ;
244+ }
245+
246+ return query .search .getFirst ().title ;
247+ }
248+
249+ @ Nullable
250+ public static String getWikiSummary (String pageName ) {
251+ String title = searchArticleName (pageName );
252+ if (title == null ) {
253+ return null ;
254+ }
255+
256+ URL url = buildURL (title , PAGE_SUMMARY_QUERY );
257+ if (url == null ) {
258+ return null ;
259+ }
260+
261+ QueryResult result = getResult (url );
262+ if (result == null ) {
200263 return null ;
201264 }
202265
@@ -212,12 +275,12 @@ public static String getWikiSummary(String pageName) {
212275 }
213276
214277 @ SuppressWarnings ("unused" )
215- private static class QueryResult {
278+ public static class QueryResult {
216279 @ Nullable
217280 public String batchcomplete ;
218281 @ Nullable
219282 public Query query ;
220- private static class Query {
283+ public static class Query {
221284 @ SuppressWarnings ("MismatchedQueryAndUpdateOfCollection" )
222285 @ Nullable
223286 private Map <String , Page > pages ;
@@ -230,6 +293,21 @@ private static class Page {
230293 @ Nullable
231294 public String missing ;
232295 }
296+ @ SuppressWarnings ("MismatchedQueryAndUpdateOfCollection" )
297+ @ Nullable
298+ private List <Search > search ;
299+ private static class Search {
300+ @ Nullable
301+ public String title ;
302+ }
303+ @ SuppressWarnings ("MismatchedQueryAndUpdateOfCollection" )
304+ @ Nullable
305+ private SearchInfo searchinfo ;
306+ private static class SearchInfo {
307+ public int totalhits ;
308+ @ Nullable
309+ public String suggestion ;
310+ }
233311 }
234312 }
235313
0 commit comments