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