id
stringlengths
36
36
text
stringlengths
1
1.25M
20c527cb-ebea-41eb-8911-691e11b15f39
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("do"); Dao dao = new Dao(); if ("delete".equals(action)) { try { dao.deleteRow(request.getParameter("id")); } catch (SQLException e) { e.printStackTrace(); } } List<Object> objects = new ArrayList<Object>(); String search = request.getParameter("searchString"); if (search != null) { try { objects = dao .search(request.getParameter("searchString")); } catch (SQLException e) { e.printStackTrace(); } } else { try { objects = dao.findAllObjects(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } request.setAttribute("objects", objects); request.getRequestDispatcher("WEB-INF/jsp/Search.jsp").forward(request, response); }
a7715424-709a-45f3-b741-d55eb0710035
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
e861c535-ac71-42e9-881d-2eca121e64db
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("WEB-INF/jsp/Add.jsp").forward(request, response); }
1df0c6a8-7cbe-4012-bf55-495ea96c324c
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String code = request.getParameter("code"); if(name != null && code != null){ try { new Dao().addRow(name, code); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } response.setHeader("Location", "Search"); response.setStatus(HttpServletResponse.SC_FOUND); }
4a5f215e-23ed-4d63-8269-d482f22c69b6
public Word(String string) { this.setWs(string); setPossibleTypes(string); this.setWordType(this.possibleTypes); }
a45d1567-2858-4f62-8990-237d1a0e9fc5
public Word(Word w) { this.setWs(w.ws); for (Iterator<WordType> iterator = w.possibleTypes.iterator(); iterator.hasNext();) { WordType wt = iterator.next(); this.possibleTypes.add(wt); } this.setWordType(this.possibleTypes); }
4bfe54e5-90d4-4c3c-94f9-5e9bc33b2e58
private void setPossibleTypes(String string) { // getClosedClassTypes(string); // getOpenClassTypes(string); possibleTypes.addAll(WiktionaryParser.getTypes(string)); if(possibleTypes.isEmpty()){ possibleTypes.addAll(WiktionaryParser.getTypes(string.toLowerCase())); } if(possibleTypes.isEmpty() & Character.isUpperCase(string.charAt(0))){ // A NAME possibleTypes.add(new Pron()); } }
53d81308-6590-4f5a-b5fa-8d33037e9ac3
private Collection<? extends WordType> getWiktionaryTypes(String string) { LinkedHashSet<WordType> lhs = new LinkedHashSet<WordType>(); try { Document doc = Jsoup.connect("http://en.wiktionary.org/wiki/"+string).get(); Elements h2 = doc.getElementsByTag("h2"); for (Iterator<Element> iterator = h2.iterator(); iterator.hasNext();) { Element element = (Element) iterator.next(); Elements eng = element.getElementsMatchingText("English"); if(eng.size()>0){ Element e = eng.get(0).nextElementSibling(); while(e!=null && e.tagName()!="h2"){ if(!e.getElementsMatchingOwnText("Noun").isEmpty()){ lhs.add(new Noun()); } if(!e.getElementsMatchingOwnText("Verb").isEmpty()){ lhs.add(new Verb()); while(e.tagName()!="ol"){ System.out.println(e.text()); e=e.nextElementSibling(); } } if(!e.getElementsMatchingOwnText("Adjective").isEmpty()){ lhs.add(new Adj()); } if(!e.getElementsMatchingOwnText("Adverb").isEmpty()){ lhs.add(new Adv()); } if(!e.getElementsMatchingOwnText("Determiner").isEmpty()){ lhs.add(new Det()); } if(!e.getElementsMatchingOwnText("Article").isEmpty()){ lhs.add(new Det()); } if(!e.getElementsMatchingOwnText("Pronoun").isEmpty()){ lhs.add(new Pron()); } if(!e.getElementsMatchingOwnText("Preposition").isEmpty()){ lhs.add(new Prep()); } if(!e.getElementsMatchingOwnText("Conjunction").isEmpty()){ lhs.add(new Conj()); } e=e.nextElementSibling(); } } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return lhs; }
43cf9993-8cc2-4ddf-b382-ea243f6d3ce2
public int possibleTypesSize() { return this.possibleTypes.size(); }
a34a4469-ee84-4566-8db2-c3ff16d86dba
public void removeFirstWordType() { if(!this.possibleTypes.isEmpty()){ remove(this.possibleTypes.iterator().next()); } }
b148fa2e-17c6-42c8-8587-e08196d29301
public void remove(WordType wt) { this.possibleTypes.remove(wt); setWordType(this.possibleTypes); }
a8b9bff5-4fe4-4455-b861-9179cb2dac15
public WordType getWordType() { return wt; }
ddf774e7-f94d-4c64-9c4c-4a1678561222
public void setWordType(Set<WordType> pt) { if(!pt.isEmpty()){ wt = pt.iterator().next(); } }
4611914a-ddf2-43b9-bbad-570652a56ae1
public String getWs() { return ws; }
272889e6-615c-4fbe-a498-cc933a06861e
public void setWs(String ws) { this.ws = ws; }
ada59acc-1d5f-4f38-8205-b7454e7436fa
public String printPossibleTypes(){ String s = ""; for (Iterator<WordType> iterator = this.possibleTypes.iterator(); iterator.hasNext();) { WordType type = (WordType) iterator.next(); if(s.length()>0){ s += "/"; } s += type.getClass().getSimpleName(); } return s; }
363a7cc7-4573-4a46-8a4c-14d146e2ef3a
public static void main(String[] args) { Word w = new Word("Through"); System.out.println(w.printPossibleTypes()); if(w.wt != null){ System.out.println(w.wt.getClass().getName()); } }
cc7742d3-cbd8-492d-8986-f186fe47725d
public Sentence(String string) { type = getSentenceType(string); String s = removePunctuation(string); s = separatePunctuation(s); s = fixConjunctions(s); List<Word> words = separateWords(s); // this.fixAmbiguity((ArrayList<Word>) this.words); seperateSubjectObject(words); }
f2b44743-7517-432e-9b4b-95bcbb855c14
private String removePunctuation(String s) { s = s.replaceAll("\\.", ""); s = s.replaceAll("!", ""); s = s.replaceAll("\\?", ""); return s; }
b2887ac9-d641-45de-847d-ea277e9c3a13
private SentenceType getSentenceType(String string) { if(string.endsWith("?")){ return SentenceType.INTERROGATIVE; } else if(string.endsWith("!")){ return SentenceType.EXCLAMATORY; } else { return SentenceType.DECLARATIVE; } }
4eba9888-8a1e-49ea-9bff-95bcaeb44096
public Sentence(Sentence sentence) { for (Iterator<Word> iterator = sentence.subject.iterator(); iterator.hasNext();) { Word w = iterator.next(); this.subject.add(new Word(w)); } this.verb = sentence.verb; for (Iterator<Word> iterator = sentence.object.iterator(); iterator.hasNext();) { Word w = iterator.next(); this.object.add(new Word(w)); } }
c5a97aab-a137-49c7-a051-8e1698c67a4d
private String separatePunctuation(String s) { s = s.replaceAll(",", " ,"); return s; }
4cbe4526-8a79-4733-aa24-0e8ce6b63bb5
private String fixConjunctions(String s) { Scanner sc = new Scanner(s); String ns = new String(); while (sc.hasNext()) { String w = sc.next(); if(w.contains("'")){ w=w.replaceAll("ain't$", "is not"); w=w.replaceAll("n't$", " not"); w=w.replaceAll("^let's$", "let us"); w=w.replaceAll("'m$", " am"); w=w.replaceAll("'re$", " are"); w=w.replaceAll("^he's$", "he is"); w=w.replaceAll("^she's$", "she is"); w=w.replaceAll("^it's$", "it is"); w=w.replaceAll("'ve$", " have"); w=w.replaceAll("'d$", " had"); w=w.replaceAll("'ll$", " will"); w=w.replaceAll("^o'", "of "); // w=w.replaceAll("^'t", "it "); w=w.replaceAll("'em$", " them"); } ns += w; if(sc.hasNext()){ ns += " "; } } sc.close(); return ns; }
2b7f38a4-ed7e-4dce-83f5-d579b95486b8
private List<Word> separateWords(String string) { List<Word> words = new ArrayList<Word>(); Scanner s = new Scanner(string); while (s.hasNext()) { String w = s.next(); words.add(new Word(w)); } s.close(); return words; }
b68c3c47-d1ce-4e95-8709-de7d2b3c90d2
private List<Word> joinSubjectObject() { List<Word> words = new ArrayList<Word>(); words.addAll(subject); words.add(verb); words.addAll(object); return words; }
a277de69-a7da-460e-8695-d67c7fff628e
private void seperateSubjectObject(List<Word> words) { int verbIndex = getVerbIndex(words); for (int i = 0; i < verbIndex; i++) { this.subject.add(words.get(i)); } if(verbIndex>=0){ this.verb = words.get(verbIndex); } for (int i = verbIndex+1; i < words.size(); i++) { this.object.add(words.get(i)); } }
22b6bdde-5056-48d0-9b87-8212a3768931
private int getVerbIndex(List<Word> words) { for (int i = 0; i < words.size(); i++) { WordType wordType = words.get(i).getWordType(); if(wordType==null){ continue; } if(wordType.getClass().getName().contains("Verb")){ return i; } } return -1; }
8096d5a4-0365-4392-9164-11d320d4fc46
public Sentence reverseSentence() { Sentence reverseSentence = new Sentence(this); if(((Verb)reverseSentence.verb.getWordType()).getBaseForm().matches("do")){ reverseSentence.verb = null; } List<Word> temp = reverseSentence.object; reverseSentence.object = reverseSentence.subject; reverseSentence.subject = temp; return reverseSentence; }
d91e705a-4482-4fa4-bd93-5df6badb68f2
public Sentence replaceWH(String info) { List<Word> words = joinSubjectObject(); String s = this.printWords(words,1); String [] wh = {"What","Where","When","Why","How","Who","Which"}; for (int i = 0; i < wh.length; i++) { s = s.replace(wh[i], "").trim(); } String[] ss = info.split("([\\.!?])\\s+"); for (int i = 0; i < ss.length; i++) { if(ss[i].contains(s)){ Sentence sn = new Sentence(ss[i]); return sn; } } return null; }
a8763b75-98c1-4193-9028-88fadb805bdc
public String printWords(List<Word> wl, int width) { String s = ""; for (int i = 0; i < wl.size(); i++) { Word word = wl.get(i); String wordString = ""; if(word!=null){ wordString = word.getWs(); } s += String.format("%"+width+"s", wordString); s += " "; } return s; }
b4cceb38-57af-4e46-adbe-2c680fe667bb
public String printWordTypes(List<Word> wl, int width) { String s = ""; for (int i = 0; i < wl.size(); i++) { Word word = wl.get(i); String wordTypeString = "Null"; if(word!=null){ WordType wt = word.getWordType(); if(wt != null){ wordTypeString = wt.getClass().getSimpleName(); } } s += String.format("%"+width+"s", wordTypeString); s += " "; } return s; }
66a43650-6365-4854-8cd1-0d4442d19f02
public String printWordPossibleTypes(List<Word> wl,int width) { String s = ""; for (int i = 0; i < wl.size(); i++) { Word word = wl.get(i); String wordTypesString = "Null"; if(word!=null){ wordTypesString = word.printPossibleTypes(); } s += String.format("%"+width+"s", wordTypesString); s += " "; } return s; }
455e4172-b95e-4960-acfe-1992728c829b
@Override public String toString() { String s = ""; List<Word> words = joinSubjectObject(); int width = -25; s+=printWords(words,width)+"\n"; s+=printWordTypes(words,width)+"\n"; s+=printWordPossibleTypes(words,width)+"\n"; return s; }
22b18fe0-1e50-455c-b32a-27f3ff3c2ce0
public ArrayList<Sentence> getNextPossibleSenses() { ArrayList<Sentence> sl = new ArrayList<Sentence>(); List<Word> words = joinSubjectObject(); for (int i = 0; i < words.size(); i++) { if(words.get(i).possibleTypesSize() > 1){ Sentence s = new Sentence(this); List<Word> sWords = s.joinSubjectObject(); sWords.get(i).removeFirstWordType(); sl.add(s); } } return sl; }
18625c5a-7857-4abe-a747-e4f6cabaf83b
public static Collection<? extends WordType> getTypes(String string) { LinkedHashSet<WordType> lhs = new LinkedHashSet<WordType>(); try { Document doc = Jsoup.connect("http://en.wiktionary.org/wiki/"+string).get(); Element english = doc.getElementById("English"); if(english == null){ return lhs; } Elements englishContent = getContent(english.parent()); for (Element el : englishContent) { if(!el.getElementsMatchingOwnText("Noun").isEmpty()){ lhs.add(new Noun()); } if(!el.getElementsMatchingOwnText("Verb").isEmpty()){ Verb v = new Verb(); Elements verbContent = getContent(el); String baseForm = getBaseForm(verbContent); if(baseForm==""){ baseForm = string; } v.setBaseForm(baseForm); lhs.add(v); } if(!el.getElementsMatchingOwnText("Adjective").isEmpty()){ lhs.add(new Adj()); } if(!el.getElementsMatchingOwnText("Adverb").isEmpty()){ lhs.add(new Adv()); } if(!el.getElementsMatchingOwnText("Determiner").isEmpty()){ lhs.add(new Det()); } if(!el.getElementsMatchingOwnText("Article").isEmpty()){ lhs.add(new Det()); } if(!el.getElementsMatchingOwnText("Pronoun").isEmpty()){ lhs.add(new Pron()); } if(!el.getElementsMatchingOwnText("Preposition").isEmpty()){ lhs.add(new Prep()); } if(!el.getElementsMatchingOwnText("Conjunction").isEmpty()){ lhs.add(new Conj()); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return lhs; }
503eed8b-7ab3-45d3-8c14-d9d4102e1de6
private static String getBaseForm(Elements verbContent) { String baseForm = ""; for (Element element : verbContent) { if(element.tagName() == "ol"){ String definition = element.child(0).text(); if(definition.matches(".* form of \\w+")){ baseForm = definition.replaceAll("^.*? (\\w+)$", "$1"); } } } return baseForm; }
3ed85d0d-e52b-44b4-908f-8a5679338565
private static Elements getContent(Element title) { if(title == null){ return null; } Element el = title.nextElementSibling(); Elements els = new Elements(); while(el!=null && tagLevel(el)>tagLevel(title)){ els.add(el); el = el.nextElementSibling(); } return els; }
a483ac58-98d0-4269-9568-458f1037c3bc
private static int tagLevel(Element el) { Pattern p = Pattern.compile("\\d"); Matcher m = p.matcher(el.tagName()); if(m.find()){ return Integer.parseInt(m.group()); } else { return Integer.MAX_VALUE; } }
8a7c56d7-50af-4397-b8e4-9938ccc7d8b1
public static Sentence findGoodSentence(Sentence s, int credits){ List<Sentence> sl = new ArrayList<Sentence>(); List<Sentence> nsl = new ArrayList<Sentence>(); sl.add(s); for (int i = 0; i < credits; i++) { System.out.println("credit: "+i); if(sl.size() == 0){ System.out.println("No more senses found"); return null; } for (Iterator<Sentence> iterator = sl.iterator(); iterator.hasNext();) { Sentence sentence = (Sentence) iterator.next(); if(checkGrammar(sentence)){ return sentence; } else { nsl.addAll(sentence.getNextPossibleSenses()); } } sl = new ArrayList<Sentence>(nsl); nsl.clear(); } return null; }
1ea57d7b-b6f5-4a3e-aff2-0ae3b521f9ce
private static boolean checkGrammar(Sentence sentence) { // System.out.println(sentence); return true; }
90102562-02ad-44f2-bb32-dbb9f8f1fcb3
private static Sentence answer(Sentence question, String info){ // reverse sentence: what is that -> that is what Sentence answer = question.reverseSentence(); System.out.println(answer); // replace interrogative word answer = answer.replaceWH(info); return answer; }
1de5a890-51c5-4278-b655-7da9fdb976af
public static void main(String[] args) { Sentence s; String st; // st = "Dani ate the apple that stood on the table"; // s = new Sentence(st); // findGoodSentence(s, 1); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "he did the thing and was on the thing"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); String info = "Dani sees trains."; String q = "What does Dani see?"; s = new Sentence(q); System.out.println(s); System.out.println(answer(s, info)); // st = "I've watched through his eyes, I've listened through his ears, and tell you he's the one"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "I have been in his things , I have been in his things , and [I] (tell) you [that] he is the thing"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "He could feel his legs thrashing, and his hands were clenching each other, wringing each other so tightly that they ached"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "He would (feel) his things doing , and his things were doing them , doing them very well that they were"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "Of course, they had bigger keyboards -- but how could their thick fingers draw a fine line, the way Ender could, a thin line so precise that he could make it spiral seventy-nine times from the center to the edge of the desk without the lines ever touching or overlapping"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "well , they were good things -- and how would their good things do a good thing , the thing he would , a good thing so good (that) he would (make) it do good things (from) the thing (to) the thing (of) the thing (without) the things well being or being?"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // // st = "For a moment, the others backed away and Stilson lay motionless"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "in a thing, the things were well and he was well"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // // st = "I'm sorry, Ender"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "I am good, you"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // // st = "it's better to play the war games, and have a better chance of surviving when the buggers came again"; // s = new Sentence(st); // System.out.println(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // st = "it is good to do the thing things , and be a good thing in being (in-the-thing-that) the things were well"; // s = new Sentence(st); //// System.out.println(s); // System.out.println(findGoodSentence(s, 1)); // // st = "I'm dying to see how he handles them, too."; // s = new Sentence(st); // System.out.println(st); // System.out.println(s); // st = "I am (dying) to be how he does them, well"; // s = new Sentence(st); // System.out.println(s); // // st = "It only works, during your regularly scheduled practice sessions."; // s = new Sentence(st); // System.out.println(st); // System.out.println(s); // st = "it (only) is, in your well being thing things"; // s = new Sentence(st); // System.out.println(s); // // st = "His belly spilled over both armrests now, even when he sat upright."; // s = new Sentence(st); // System.out.println(st); // System.out.println(s); // st = "his thing was in good things now, and (in-the-thing-that) he was well"; // s = new Sentence(st); // System.out.println(s); }
b07b1579-69ae-490c-bb8c-a00365033ee1
public Verb() { }
a17b5b3e-2d45-48bb-8f21-a5062b7d00d3
public void setBaseForm(String s) { baseForm = s; }
826f9594-d38a-4784-8e45-04d1b0a8978b
public String getBaseForm() { return baseForm; }
bfeb864d-b834-42ce-a258-307ea4da98e4
public Noun() { }
6316c55d-c15f-437f-acb3-c71fa89801ad
public Prep() { }
5c2c24aa-66b6-4a5c-b872-94910ed4bb14
public Pron() { }
080c9d59-a7d1-4bcd-92e5-ae59a702883a
public Adj() { }
f39a7419-199e-4bf6-b738-92246fe27384
public Conj() { }
4ca85def-6c79-40e2-9ae6-db37ef2ea64e
public Adv() { }
68bc64f9-2160-4916-bd99-7e9d985d9146
public Det() { }
c09afc1a-2ec1-487a-97a1-95b60e092e11
public TwitchTVChannelLinks(final String username) throws FetchLinksException { if (username == null || username.isEmpty()) { throw new IllegalArgumentException("username cannot be null or \"\""); } this.TWITCH_USERNAME = username; try { this.RESPONSE = HttpRequest.performRequest(this.TWITCH_API_CHANNELS_BASE_URL + this.TWITCH_USERNAME, this.MIME_TYPE); } catch (IOException | MimeTypeParseException ex) { throw new FetchLinksException(); } this.FOLLOWS_URL = this.extractProperty(this.FOLLOWS_PROPERTY); this.TEAMS_URL = this.extractProperty(this.TEAMS_PROPERTY); this.CHANNEL_URL = this.extractProperty(this.CHANNEL_PROPERTY); this.COMMERCIAL_URL = this.extractProperty(this.COMMERCIAL_PROPERTY); this.STREAM_KEY_URL = this.extractProperty(this.STREAM_KEY_PROPERTY); this.CHAT_URL = this.extractProperty(this.CHAT_PROPERTY); this.FEATURES_URL = this.extractProperty(this.FEATURES_PROPERTY); this.SUBSCRIPTIONS_URL = this.extractProperty(this.SUBSCRIPTIONS_PROPERTY); this.EDITORS_URL = this.extractProperty(this.EDITORS_PROPERTY); this.VIDEOS_URL = this.extractProperty(this.VIDEOS_PROPERTY); }
0ae61c04-6fd1-4e64-a581-8fd8c8cc15f4
private String extractProperty(String propertyName) throws FetchLinksException { JsonParser parser = new JsonParser(); try { JsonObject responseJson = (JsonObject) parser.parse(this.RESPONSE); if (!responseJson.isJsonNull()) { JsonObject linksJson = responseJson.getAsJsonObject(this.SELFDESCRIBING_PROPERTY); if (!linksJson.isJsonNull()) { JsonPrimitive propertyJson = linksJson.getAsJsonPrimitive(propertyName); if (!propertyJson.isJsonNull()) { String returnValue = propertyJson.getAsString(); if (!returnValue.endsWith("/")) returnValue += "/"; return returnValue; } } } } catch (JsonParseException ex) { throw new FetchLinksException(); } throw new FetchLinksException(); }
c0a878d3-efcc-42ed-9144-09fb98e72359
public String getTwitchUsername() { return this.TWITCH_USERNAME; }
0f874fde-a833-4bf5-b27f-4baeccc3c035
public String getFollowsUrl() { return this.FOLLOWS_URL; }
909d9f86-eaca-418b-8ec2-25df3f78538f
public String getTeamsUrl() { return this.TEAMS_URL; }
819c640e-e5c7-4a93-8639-6f39a87f5287
public String getChannelUrl() { return this.CHANNEL_URL; }
64fd8d70-dd3b-4357-87c9-0c227ecdfb02
public String getCommercialUrl() { return this.COMMERCIAL_URL; }
a99aa54f-4df4-4bb1-9189-90fce95bec1a
public String getStreamKeyUrl() { return this.STREAM_KEY_URL; }
9a1c438c-6922-407b-95ed-322d44e2a68e
public String getChatUrl() { return this.CHAT_URL; }
e08297c7-a822-4de9-ad15-45d7c821d833
public String getFeaturesUrl() { return this.FEATURES_URL; }
c0a63fc7-da9a-430d-9404-3bd5965ebb5f
public String getSubscriptionsUrl() { return this.SUBSCRIPTIONS_URL; }
383b84e7-a453-40ae-9001-0167b4cdae4a
public String getEditorsUrl() { return this.EDITORS_URL; }
b0f5a1f3-52e4-4ebb-b504-d19c8c348da1
public String getVideosUrl() { return this.VIDEOS_URL; }
d1ffb84e-836f-4e9c-89e7-faaea06003b5
public TwitchTVStream(final String username) { if (username == null || username.isEmpty()) { throw new IllegalArgumentException("username cannot be null or empty"); } this.TWITCH_USERNAME = username; }
9d6e8570-031e-4c6a-8bb4-ec22c1bed6cb
public boolean isLive() throws IOException, MimeTypeParseException { String response = HttpRequest.performRequest(this.TWITCH_API_STREAM_BASE_URL + this.TWITCH_USERNAME, this.MIME_TYPE); JsonParser parser = new JsonParser(); JsonObject streamJson = (JsonObject) parser.parse(response); boolean isLive = streamJson.get(this.STREAM_PROPERTY).isJsonNull() == false; if (isLive) { JsonObject channelJson = (JsonObject) ((JsonObject) streamJson.get(this.STREAM_PROPERTY)).get(this.STREAM_CHANNEL_PROPERTY); this.STREAM_TITLE_VALUE = channelJson.get(this.STREAM_TITLE_PROPERTY).getAsString(); JsonElement gameJson = channelJson.get(this.STREAM_GAME_PROPERTY); this.STREAM_GAME_VALUE = gameJson.isJsonNull()?"":gameJson.getAsString(); this.STREAM_VIEWERS_VALUE = ((JsonObject) streamJson.get(this.STREAM_PROPERTY)).get(this.STREAM_VIEWERS_PROPERTY).getAsString(); this.STREAM_URL_VALUE = channelJson.get(this.STREAM_URL_PROPERTY).getAsString(); this.TWITCH_DISPLAYNAME_VALUE = channelJson.get(this.TWITCH_DISPLAYNAME_PROPERTY).getAsString(); JsonObject previewJson = ((JsonObject) streamJson.get(this.STREAM_PROPERTY)).get(this.STREAM_PREVIEW_PROPERTY).getAsJsonObject(); this.STREAM_PREVIEW_SMALL_VALUE = previewJson.get(this.STREAM_PREVIEW_SMALL_PROPERTY).getAsString(); this.STREAM_PREVIEW_MEDIUM_VALUE = previewJson.get(this.STREAM_PREVIEW_MEDIUM_PROPERTY).getAsString(); this.STREAM_PREVIEW_LARGE_VALUE = previewJson.get(this.STREAM_PREVIEW_LARGE_PROPERTY).getAsString(); } else { this.STREAM_TITLE_VALUE = ""; this.STREAM_GAME_VALUE = ""; this.STREAM_VIEWERS_VALUE = ""; this.STREAM_URL_VALUE = ""; this.TWITCH_DISPLAYNAME_VALUE = this.TWITCH_USERNAME; this.STREAM_PREVIEW_SMALL_VALUE = ""; this.STREAM_PREVIEW_MEDIUM_VALUE = ""; this.STREAM_PREVIEW_LARGE_VALUE = ""; } return isLive; }
da014843-0d21-4cfa-8712-3dc2e30f5086
public String getViewers() { if (this.STREAM_VIEWERS_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_VIEWERS_VALUE; }
297b46b3-69bb-48a8-bf58-f73680b4f306
public String getTitle() { if (this.STREAM_VIEWERS_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_TITLE_VALUE; }
f805193e-c489-4470-824a-c27c28d84da3
public String getGame() { if (this.STREAM_VIEWERS_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_GAME_VALUE; }
bce85965-dc65-4a7d-9b80-26608a216bae
public String getUrl() { if (this.STREAM_VIEWERS_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_URL_VALUE; }
384de011-4b00-48ce-94e9-d7c2d6bf890f
public String getDisplayName() { if (this.TWITCH_DISPLAYNAME_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.TWITCH_DISPLAYNAME_VALUE; }
63be756a-91a3-46bc-a0e8-043a217bf215
public String getPreviewSmall() { if (this.STREAM_PREVIEW_SMALL_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_PREVIEW_SMALL_VALUE; }
9208df17-0e56-4c1f-abca-03836bf820ae
public String getPreviewMedium() { if (this.STREAM_PREVIEW_MEDIUM_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_PREVIEW_MEDIUM_VALUE; }
3e56ecf5-57f2-4622-848b-c6f95b8890d7
public String getPreviewLarge() { if (this.STREAM_PREVIEW_LARGE_VALUE == null) { throw new IllegalStateException("isLive() method has to be called first"); } return this.STREAM_PREVIEW_LARGE_VALUE; }
7dfe720f-1994-474d-b63b-bec9c1a0a734
public static String performRequest(URL url, MimeType mime) throws IOException { HttpURLConnection conn = null; BufferedReader br = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", mime.toString()); if (conn.getResponseCode() != 200) throw new IOException("Failed : HTTP error code : " + conn.getResponseCode()); br = new BufferedReader(new InputStreamReader(conn.getInputStream())); return br.readLine(); } catch (IOException ex) { throw ex; } finally { if (conn != null) conn.disconnect(); if (br != null) br.close(); } }
0ecdd789-1590-48c8-9595-5668071fe96d
public static String performRequest(String url, String mime) throws IOException, MimeTypeParseException { return performRequest(new URL(url), new MimeType(mime)); }
67178118-2d43-4a24-8ec0-5aa4d2ee082b
public static String performRequest(URL url, String mime) throws MimeTypeParseException, IOException { return performRequest(url, new MimeType(mime)); }
6b0a335f-612a-4ce6-8a95-9021cdf324d8
public static String performRequest(String url, MimeType mime) throws IOException { return performRequest(new URL(url), mime); }
e14889f8-8d91-4fce-8bda-76fa0e357482
@Override public String getMessage() { return "Could not fetch links."; }
a6123ed1-e429-4ef3-b8aa-ec7f93c61aa1
@Override public String getLocalizedMessage() { return "Could not fetch links."; }
8ba7dbd2-c23a-4f93-aaac-8b1401b1659d
public TwitchTVAppGUI() { initComponents(); lblUrlValue.setCursor(new Cursor(Cursor.HAND_CURSOR)); }
37f6ea82-ca67-4b56-aa99-600dff56f90d
@SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { lblChannelName = new javax.swing.JLabel(); tfChannelName = new javax.swing.JTextField(); btnCheckLive = new javax.swing.JButton(); lblStreamStatus = new javax.swing.JLabel(); lblTitle = new javax.swing.JLabel(); lblGame = new javax.swing.JLabel(); lblViewers = new javax.swing.JLabel(); lblUrl = new javax.swing.JLabel(); lblTitleValue = new javax.swing.JLabel(); lblGameValue = new javax.swing.JLabel(); lblViewersValue = new javax.swing.JLabel(); lblUrlValue = new javax.swing.JLabel(); lblPreview = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); lblChannelName.setText("Channel name:"); btnCheckLive.setText("Check"); btnCheckLive.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCheckLiveActionPerformed(evt); } }); lblStreamStatus.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N lblTitle.setText("Title:"); lblGame.setText("Game:"); lblViewers.setText("Viewers:"); lblUrl.setText("URL:"); lblUrlValue.setForeground(new java.awt.Color(0, 51, 255)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lblChannelName) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(tfChannelName, javax.swing.GroupLayout.PREFERRED_SIZE, 831, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnCheckLive)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblGame) .addComponent(lblStreamStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 650, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblViewers) .addComponent(lblUrl)) .addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblUrlValue, javax.swing.GroupLayout.PREFERRED_SIZE, 587, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblViewersValue, javax.swing.GroupLayout.PREFERRED_SIZE, 587, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(lblTitle) .addGap(41, 41, 41) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblGameValue, javax.swing.GroupLayout.PREFERRED_SIZE, 587, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblTitleValue, javax.swing.GroupLayout.PREFERRED_SIZE, 587, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblPreview, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblChannelName) .addComponent(tfChannelName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnCheckLive)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lblStreamStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(46, 46, 46) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblTitle) .addComponent(lblTitleValue, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblGame, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblGameValue, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lblViewers) .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblUrlValue, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblUrl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addComponent(lblViewersValue, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(45, 45, 45)) .addGroup(layout.createSequentialGroup() .addComponent(lblPreview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()))) ); pack(); }// </editor-fold>//GEN-END:initComponents
98d1eae6-1dc7-462a-b74b-a348eba9a079
public void actionPerformed(java.awt.event.ActionEvent evt) { btnCheckLiveActionPerformed(evt); }
dfadef3c-d39f-4719-91f0-ab4eec20d0b2
private void btnCheckLiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCheckLiveActionPerformed if (this.isLiveThreadRunning == true) return; Thread thread = new Thread(() -> { // THREAD BEGIN this.isLiveThreadRunning = true; this.clearLabels(); String username = this.tfChannelName.getText(); if (username == null || username.isEmpty()) { this.printErrorToStreamStatusLbl(); this.isLiveThreadRunning = false; return; } this.lblStreamStatus.setForeground(Color.BLACK); this.lblStreamStatus.setText("Checking ..."); TwitchTVStream stream = new TwitchTVStream(username.trim()); try { if (stream.isLive()) { this.lblStreamStatus.setForeground(new Color(7, 171, 78)); this.lblStreamStatus.setText(stream.getDisplayName() + " is LIVE!"); this.lblTitleValue.setText(stream.getTitle()); this.lblGameValue.setText(stream.getGame()); this.lblViewersValue.setText(stream.getViewers()); String url = stream.getUrl(); this.lblUrlValue.setText("<HTML><U>" + url + "<U><HTML>"); for (MouseListener listener : this.lblUrlValue.getMouseListeners()) { this.lblUrlValue.removeMouseListener(listener); } this.lblUrlValue.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent me) { if (Desktop.isDesktopSupported() == true) { try { Desktop.getDesktop().browse(new URI(url)); } catch (IOException | URISyntaxException ex) { Logger.getLogger(TwitchTVAppGUI.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void mousePressed(MouseEvent me) { } @Override public void mouseReleased(MouseEvent me) { } @Override public void mouseEntered(MouseEvent me) { } @Override public void mouseExited(MouseEvent me) { } }); Icon icon = new ImageIcon(new URL(stream.getPreviewMedium())); this.lblPreview.setIcon(icon); } else { this.clearLabels(); this.lblStreamStatus.setForeground(Color.RED); this.lblStreamStatus.setText(stream.getDisplayName() + " is OFFLINE!"); } } catch (IOException | MimeTypeParseException ex) { this.printErrorToStreamStatusLbl(); } finally { this.isLiveThreadRunning = false; } // THREAD END }); thread.start(); }//GEN-LAST:event_btnCheckLiveActionPerformed
f837678d-7f9c-4928-98de-41d1ecf80721
@Override public void mouseClicked(MouseEvent me) { if (Desktop.isDesktopSupported() == true) { try { Desktop.getDesktop().browse(new URI(url)); } catch (IOException | URISyntaxException ex) { Logger.getLogger(TwitchTVAppGUI.class.getName()).log(Level.SEVERE, null, ex); } } }
f26e688d-63c0-45db-b002-096cb3785bc5
@Override public void mousePressed(MouseEvent me) { }
cf823a48-f414-4ab3-9b70-6c86c59409ce
@Override public void mouseReleased(MouseEvent me) { }
8f0ab104-ad7f-4db8-adbb-bcdf9b1b0367
@Override public void mouseEntered(MouseEvent me) { }
70db9f2f-4d1f-4488-9ae1-4137e622d765
@Override public void mouseExited(MouseEvent me) { }
8cb90a0b-a419-42d2-904a-a42a65ca8345
private void printErrorToStreamStatusLbl() { this.lblStreamStatus.setForeground(Color.BLACK); this.lblStreamStatus.setText("Something went wrong..."); }
8750222b-9f4b-4a88-bdf4-b8ddbfd74e63
private void clearLabels() { this.lblTitleValue.setText(""); this.lblGameValue.setText(""); this.lblViewersValue.setText(""); this.lblUrlValue.setText(""); for (MouseListener listener : this.lblUrlValue.getMouseListeners()) { this.lblUrlValue.removeMouseListener(listener); } if (this.lblPreview.getIcon() != null) ((ImageIcon) this.lblPreview.getIcon()).getImage().flush(); this.lblPreview.setIcon(null); }
bd24a309-d776-4cd3-a749-1b61350f9558
public static void main(String args[]) { /* Set the Windows look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TwitchTVAppGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> new TwitchTVAppGUI().setVisible(true)); }
9e69a7ff-ceee-4bb9-8700-64b3244e39a5
public ImageProxy( URL imageURL ) { this.imageURL = imageURL; }
ca5b9a26-4c5e-47f5-a60b-2dcdc744c216
@Override public void paintIcon( final Component c, Graphics g, int x, int y ) { if ( imageIcon != null ) { imageIcon.paintIcon( c, g, x, y ); } else { g.drawString( "Loading Stream Preview, please wait...", x, y ); if ( !retrieving ) { retrieving = true; retrievalThread = new Thread( () -> { try { imageIcon = new ImageIcon( imageURL, "Stream Preview" ); c.repaint(); } catch ( Exception e ) { e.printStackTrace(); } }); retrievalThread.start(); } } }
cba34394-26f1-4a06-897f-09a8da816d09
@Override public int getIconWidth() { if ( imageIcon != null ) { return imageIcon.getIconWidth(); } else { return 320; } }
78eea594-16b2-498f-adf2-e9a5f2d6c493
@Override public int getIconHeight() { if ( imageIcon != null ) { return imageIcon.getIconHeight(); } else { return 200; } }
643546fb-e5f5-49d3-8a73-da99bed3f554
public ImageComponent( Icon icon ) { this.icon = icon; }
b4594dce-54e8-4f1f-a652-a01e6c17e001
public void setIcon( Icon icon ) { this.icon = icon; }
8f84610d-486c-4deb-8aac-4f2b2493d26f
@Override public void paintComponent( Graphics g ) { super.paintComponent( g ); int x = 0; int y = 0; icon.paintIcon( this, g, x, y ); }