package extras;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

import extras.urlparsers.FileParser;
import extras.urlparsers.FourChanParser;
import extras.urlparsers.SteamParser;
import extras.urlparsers.YoutubeParser;

public class URLTitles {
	
	
	public static String find(String s){
		if(s.contains("youtube.com/watch?") || s.contains("youtu.be/")) return YoutubeParser.find(s);
		if(s.contains("boards.4chan.org/") && s.contains("/thread/")) return FourChanParser.find(s);
		if(s.contains("steamcommunity.com") && (s.contains("/id/") || s.contains("/profiles/"))) return SteamParser.find(s);
		URL url;
		String title = "Title not found";
		String host = "";
		try {
			
			url = new URL(s);
			URLConnection urlc = url.openConnection();
			urlc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
			urlc.connect();
			if(!urlc.getContentType().startsWith("text/html")) return FileParser.find(s);
			
			host = urlc.getURL().getHost();
			
			Scanner scan = new Scanner(urlc.getInputStream());
			int i = 0;
			while(scan.hasNextLine()){
				String next = scan.nextLine().trim();
				if(!title.equals("Title not found")){
					title += next;
					if(title.contains("</title")){
						title = title.split("</title")[0].trim();
						break;
					}
				}
				if(next.contains("<title")){
					String[] aftertitle = next.split("<title")[1].split(">");
					if(aftertitle.length == 0) title = "";
					else title = aftertitle[1];
					if(title.contains("</title")){
						title = title.split("</title")[0].trim();
						break;
					}
				}
				if(i > 1000){
					title = "Title not found";
					break;
				}
				i++;
			}
			scan.close();
			if(title.equals("")) title = "Title not found";
			title = title.replaceAll("&.*;","");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			title = "Title not found";
			e.printStackTrace();
		}
		title = String.format("[URL] %s (%s)",title, host);
		return title;
	}
}

@@ -1179,20 +1179,16 @@
 .hasNext
-Line
 ())%7B%0A%09%09%09
@@ -1215,19 +1215,8 @@
 next
-Line().trim
 ();%0A
@@ -1270,16 +1270,22 @@
 title +=
+ %22 %22 +
  next;%0A%09
@@ -1289,32 +1289,46 @@
 ;%0A%09%09%09%09%09if(title.
+toLowerCase().
 contains(%22%3C/titl
@@ -1365,32 +1365,52 @@
 t(%22%3C/title%22)%5B0%5D.
+split(%22%3C/TITLE%22)%5B0%5D.
 trim();%0A%09%09%09%09%09%09br
@@ -1469,22 +1469,70 @@
 %09%09%09%09
-String%5B%5D after
+if(next.split(%22title.*%3E%22).length == 1 ) title = %22%22;%0A%09%09%09%09%09else 
 titl
@@ -1551,50 +1551,252 @@
 it(%22
-%3C
 title
+.*%3E
 %22)%5B1%5D
-.split(%22%3E%22);%0A%09%09%09%09%09if(aftertitle
+;%0A%09%09%09%09%09if(title.contains(%22%3C/title%22))%7B%0A%09%09%09%09%09%09title = title.split(%22%3C/title%22)%5B0%5D.trim();%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%09if(next.contains(%22%3CTITLE%22))%7B%0A%09%09%09%09%09System.out.println(next.split(%22TITLE.*%3E%22).length);%0A%09%09%09%09%09if(next.split(%22TITLE.*%3E%22)
 .len
@@ -1802,17 +1802,18 @@
 ngth == 
-0
+1 
 ) title 
@@ -1840,18 +1840,30 @@
 e = 
-aftertitle
+next.split(%22TITLE.*%3E%22)
 %5B1%5D;
@@ -1881,37 +1881,37 @@
 tle.contains(%22%3C/
-title
+TITLE
 %22))%7B%0A%09%09%09%09%09%09title
@@ -1920,37 +1920,37 @@
  title.split(%22%3C/
-title
+TITLE
 %22)%5B0%5D.trim();%0A%09%09
@@ -1984,17 +1984,17 @@
 %09if(i %3E 
-1
+3
 000)%7B%0A%09%09

fixed the titles