package main; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.regex.*; public class Annotation { Information info = new Information(); Actions act = new Actions(); private String getUrlFromMsg(String inputLine) { String[] input = info.msg(inputLine).split(" "); for (String text : input) if (text.matches(Main.httpRegex)) { return text; } return ""; } public void getHeader(String inputLine) { String url = getUrlFromMsg(inputLine); try { URL uri = new URL(url); HttpURLConnection web = (HttpURLConnection) uri.openConnection(); web.setRequestMethod("GET"); web.connect(); int code = web.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { String titleSplitWhole = null; BufferedReader in = new BufferedReader(new InputStreamReader(web.getInputStream())); String httpString; Pattern titleSplitPattern = Pattern.compile("<(title|TITLE)([^a-zA-Z0-9>]+[^>]+)?>"); Pattern titleContentPattern = Pattern.compile(".*]+[^>]+)?>.*"); while ((httpString = in.readLine()) != null) { if (titleSplitPattern.matches(".*<(title|TITLE)([^a-zA-Z0-9>]+[^>]+)?>.*",httpString)) { Matcher titleSplitMatcher = titleSplitPattern.matcher(httpString); while (titleSplitMatcher.find()) titleSplitWhole = httpString.substring(titleSplitMatcher.end()); } if (titleSplitPattern.matches(".*]+[^>]+)?>.*",httpString)) { String title = null; Matcher titleContentMatcher = titleContentPattern.matcher(titleSplitWhole); while (titleContentMatcher.find()) title = titleSplitWhole.split("<")[0]; if (title != null) { if (url.length() >= 70) { URL tu = new URL("http://tinyurl.com/api-create.php?url=" + url); HttpURLConnection tuweb = (HttpURLConnection) tu.openConnection(); tuweb.setRequestMethod("GET"); tuweb.connect(); int tucode = web.getResponseCode(); if (tucode == HttpURLConnection.HTTP_OK) { BufferedReader tuin = new BufferedReader(new InputStreamReader(tuweb.getInputStream())); String turl = tuin.readLine(); System.out.println(turl); act.say("[" + title.replace('\n', ' ')+ "] - " + turl); } } else act.say("[" + title.replace('\n', ' ')+ "]"); } } } } } catch (Exception e) { e.printStackTrace(); } } }