import java.io.*; import java.util.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Imdb { Nuffy nm; Imdb (Nuffy nm){ this.nm = nm; } String line2 = null; String res2 = null; String year = null; private static final Pattern _pattern_no_match = Pattern.compile(".*No Matches..*"); private static final Pattern _pattern_found_many = Pattern.compile(".*
\\E.*?\\Qsize=+1>\\E([^<]*?)\\Q\\E.*$");
private static String _searchBase = "http://www.imdb.com/find?s=tt&q=";
private static String COMMAND_GOOGLE = "!imdb";
public void singleSearch(String searchString) {
try {
searchString = URLEncoder.encode(searchString, "UTF-8");
}
catch (UnsupportedEncodingException e) {
//return e.toString();
}
String result = null;
HttpURLConnection huc = null;
HttpURLConnection huc2 = null;
try {
Matcher matcher;
boolean flag=false;
FileOutputStream fileOut = new FileOutputStream ("index.html");
URL url = new URL(_searchBase + searchString);
huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; PircBot; JVM)");
huc.setRequestProperty("Pragma", "no-cache");
huc.connect();
huc.setFollowRedirects(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null && !flag) {
matcher=_pattern_no_match.matcher(line);
flag = matcher.matches();
if (flag){
result = "Ingen resultater funnet";
nm.setTitle(result);
}
matcher=_pattern_to_many.matcher(line);
flag = matcher.matches();
if (flag){
result = "For mange resultater (over 500)";
nm.setTitle(result);
}
matcher=_pattern_found_right.matcher(line);
if(matcher.matches()){
huc.disconnect();
result = "http://imdb.com"+matcher.group(1);
System.out.println("FANT FILM, PRøVER og ACCESSERE");
System.out.println(result);
try{
URL url2 = new URL(result);
huc2 = (HttpURLConnection)url2.openConnection();
huc2.setRequestMethod("GET");
huc2.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; PircBot; JVM)");
huc2.setRequestProperty("Pragma", "no-cache");
huc2.connect();
BufferedReader reader2 = new BufferedReader(new InputStreamReader(huc2.getInputStream()));
flag = false;
while ((line2 = reader2.readLine())!=null){
new PrintStream(fileOut).println(line2);
matcher=_pattern_rank.matcher(line2);
if(matcher.matches()){
String next = reader2.readLine();
matcher = _pattern_rank_nmb.matcher(next);
flag = matcher.matches();
if(matcher.matches()){
nm.setRanking(matcher.group(1));
System.out.println(matcher.group(1));
}
}
matcher=_pattern_tag.matcher(line2);
if(matcher.matches()){
String next = reader2.readLine();
System.out.println(next);
matcher = _pattern_tag_tx.matcher(next);
flag = matcher.matches();
if(matcher.matches()){
nm.setTag(matcher.group(1));
System.out.println(matcher.group(1));
break;
}
}
matcher=_pattern_found.matcher(line2);
flag = matcher.matches();
if(flag){
nm.setTitle(matcher.group(1));
nm.setUrl(result);
nm.setYear(matcher.group(2));
nm.setUrl(result);
nm.setYear(matcher.group(2));
}
}
}catch (Exception e) {}
}else{
String imdburl;
String path_url;
matcher=_pattern_rank.matcher(line);
if(matcher.matches()){
String next = reader.readLine();
matcher = _pattern_rank_nmb.matcher(next);
if(matcher.matches()){
nm.setRanking(matcher.group(1));
System.out.println(matcher.group(1));
}
}
matcher=_pattern_tag.matcher(line);
if(matcher.matches()){
String next = reader.readLine();
matcher = _pattern_tag_tx.matcher(next);
if(matcher.matches()){
nm.setTag(matcher.group(1));
System.out.println(matcher.group(1));
break;
}
}
matcher=_pattern_found.matcher(line);
flag = matcher.matches();
if(flag){
nm.setTitle(matcher.group(1));
nm.setYear(matcher.group(2));
nm.setTag("tagline");
path_url=huc.toString();
System.out.println(path_url);
matcher=_pattern_url.matcher(path_url);
if(matcher.matches()){
imdburl = matcher.group(1);
nm.setUrl(imdburl);
}else{
path_url=huc.toString();
matcher=_pattern_url2.matcher(path_url);
if(matcher.matches()){
imdburl = matcher.group(1);
nm.setUrl(imdburl);
}
}
}flag=false;
}
}fileOut.close();
}
catch (Exception e) {
} finally {
if(huc != null) {
huc.disconnect();
}
}
if (result!=null) {
result = result.replaceAll("<.*?>", "");
result = result.replaceAll("&.*?;", "");
}
}
}
}