package extras;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
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.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
			urlc.addRequestProperty("User-Agent", "Mozilla");
			//urlc.addRequestProperty("Referer", "google.com");
			urlc.connect();
			
			
			
			
			Scanner scan = new Scanner(urlc.getInputStream());
			host = urlc.getURL().getHost();
			
			System.out.println(urlc.getContentType());
			if(!urlc.getContentType().startsWith("text/html")) {
				scan.close();
				return FileParser.find(urlc);
			}
			boolean titlefound = false;
			boolean allcaps = false;
			String titlecode = "";
			while(scan.hasNextLine()){
				String next = scan.nextLine();
				if(next.contains("<title") && !titlefound){
					titlefound = true;
					titlecode += next;
					continue;
					
				}
				if(next.contains("<TITLE") && !titlefound){
					allcaps = true;
					titlefound = true;
					titlecode += next;
					continue;
					
				}
				if(titlefound){
					titlecode +=  next;
				}
				if(titlecode.length() > 200){
					titlecode += "...";
					break;
				}
				if(titlecode.contains("</title>") || titlecode.contains("</TITLE>")){
					break;
				}
				
				
			}
			if(title.length() > 100){
				title = title.substring(0, 95) + "...";
			}
			scan.close();
			
			if(allcaps){
				title = titlecode.split("<TITLE?>")[1].split("</TITLE")[0];
			}
			else{
				title = titlecode.split("<title?>")[1].split("</title")[0];
			}
			
			if(title.equals("")) title = "Title not found";
			title = title.replaceAll("&.*;","");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
			title = "Title not found";
		}
		title = String.format("[URL] %s (%s)",title.trim(), host);
		return title;
	}
}

@@ -1404,36 +1404,8 @@
 se;%0A
-%09%09%09boolean allcaps = false;%0A
 %09%09%09S
@@ -1426,16 +1426,16 @@
 e = %22%22;%0A
+
 %09%09%09while
@@ -1666,29 +1666,8 @@
 d)%7B%0A
-%09%09%09%09%09allcaps = true;%0A
 %09%09%09%09
@@ -2071,32 +2071,43 @@
 ();%0A
-%09%09%09%0A
 %09%09%09if(
-allcaps)%7B%0A%09%09%09%09
+titlecode.contains(%22%3CTITLE%22))
 titl
@@ -2136,24 +2136,78 @@
 TITLE?%3E%22)%5B1%5D
+;%0A%09%09%09if(titlecode.contains(%22%3Ctitle%22))title = titlecode
 .split(%22%3C/TI
@@ -2207,39 +2207,55 @@
 t(%22%3C
-/TITLE%22)%5B0
+title?%3E%22)%5B1
 %5D;%0A%09%09%09
-%7D%0A%09%09%09else%7B%0A%09%09%09%09
+if(title.contains(%22%3C/TITLE%3E%22))
 titl
@@ -2255,36 +2255,32 @@
 %22))title = title
-code
 .split(%22%3Ctitle?%3E
@@ -2272,28 +2272,75 @@
 split(%22%3C
-title?%3E%22)%5B1%5D
+/TITLE%22)%5B0%5D;%0A%09%09%09if(title.contains(%22%3C/title%3E%22))title = title
 .split(%22
@@ -2352,24 +2352,26 @@
 le%22)%5B0%5D;%0A%09%09%09
+//
 %7D%0A%09%09%09%0A%09%09%09if(
@@ -2430,34 +2430,23 @@
 e = 
-title.replaceAll(%22&.*;%22,%22%22
+makeClean(title
 );%0A%09
@@ -2671,13 +2671,143 @@
  title;%0A
-
 %09%7D%0A
+%09%0A%09public static String makeClean(String s)%7B%0A%09%09s = s.replaceAll(%22&#039;%22,%22'%22);%0A%09%09s = s.replaceAll(%22&#124;%22,%22%7C%22);%0A%09%09return s;%0A%09%7D%0A%09%0A
 %7D%0A

fix titles