PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/drtshock/willie/util/Dictionary.java

https://github.com/cnr/Willie
Java | 86 lines | 71 code | 15 blank | 0 comment | 12 complexity | e91b862b6167f57e40fadc973e81259c MD5 | raw file
  1. package com.drtshock.willie.util;
  2. import com.google.gson.JsonObject;
  3. import com.google.gson.JsonParser;
  4. import java.io.IOException;
  5. import java.net.MalformedURLException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import java.net.URL;
  9. public enum Dictionary {
  10. URBAN_DICTIONARY("http://api.urbandictionary.com/v0/define?term=%WORD%", 0, "Urban Dictionary"),
  11. DUCK_DUCK_GO("http://api.duckduckgo.com/?q=%WORD%&format=json", 1, "Duck Duck Go");
  12. private String url, name;
  13. private int id;
  14. Dictionary(String url, int id, String name) {
  15. this.url = url;
  16. this.id = id;
  17. this.name = name;
  18. }
  19. public URL getFormattedURL(String word) {
  20. try {
  21. return new URI(this.url.replace("%WORD%", word)).toURL();
  22. } catch (MalformedURLException | URISyntaxException e) {
  23. return null;
  24. }
  25. }
  26. public int getID() {
  27. return this.id;
  28. }
  29. @Override
  30. public String toString() {
  31. return this.name;
  32. }
  33. public static Dictionary getDictionaryFromID(int id) {
  34. for (Dictionary dict : Dictionary.values()) {
  35. if (dict.getID() == id) {
  36. return dict;
  37. }
  38. }
  39. return null;
  40. }
  41. public Dictionary.Definition getDefinition(String word) throws IOException {
  42. if (this == Dictionary.URBAN_DICTIONARY) {
  43. JsonObject obj = new JsonParser().parse(WebHelper.readURLToString(this.getFormattedURL(word))).getAsJsonObject();
  44. if (!obj.get("result_type").getAsString().equals("no_results") && !obj.getAsJsonArray("list").isJsonNull()) {
  45. return new Dictionary.Definition(obj.getAsJsonArray("list").get(0).getAsJsonObject().get("definition").getAsString(), WebHelper.shortenURL("http://www.urbandictionary.com/define.php?term=" + word));
  46. }
  47. } else if (this == Dictionary.DUCK_DUCK_GO) {
  48. JsonObject obj = new JsonParser().parse(WebHelper.readURLToString(this.getFormattedURL(word))).getAsJsonObject();
  49. if (!obj.get("AbstractText").getAsString().equals("")) {
  50. return new Dictionary.Definition(obj.get("AbstractText").getAsString(), WebHelper.shortenURL("http://www.thefreedictionary.com/" + word));
  51. }
  52. if (!obj.get("Definition").getAsString().equals("")) {
  53. return new Dictionary.Definition(obj.get("Definition").getAsString().substring(word.length() + 15), WebHelper.shortenURL("http://www.thefreedictionary.com/" + word));
  54. }
  55. }
  56. return null;
  57. }
  58. public static class Definition {
  59. private String definition, url;
  60. public Definition(String definition, String url) {
  61. this.definition = definition;
  62. this.url = url;
  63. }
  64. public String getDefinition() {
  65. return definition;
  66. }
  67. public String getUrl() {
  68. return url;
  69. }
  70. }
  71. }