import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reader {

    private final static String ADDRESS = "http://www.gotv.at/titel_main.php";

    private final static long INTERVAL = 10000;
    
    private final static String FILENAME = "gotvtitle.txt";
    
    private static String lastOutput = "";

    private static String readContent(String address) {
        URL url;
        try {
            url = new URL(address);
        } catch (MalformedURLException e) {
            System.out.println("Die Addresse ist Schrott: " + address);
            e.printStackTrace();
            return null;
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            StringBuffer sb = new StringBuffer();
            int character;
            while ((character = reader.read()) != -1) {
                sb.append((char) character);
            }
            return sb.toString();
        } catch (IOException e) {
            System.out.println("Es gab irgendnen Lesefehler.");
            e.printStackTrace();
        }
        return null;
    }

    private static String[] parseContent(String content) {
        Pattern pattern = Pattern
                .compile("'VideoJetzt'\\).innerHTML = '<b>(.*)</b> - (.*)';");
        Matcher matcher = pattern.matcher(content);
        matcher.find();
        String interpret = matcher.group(1);
        String title = matcher.group(2);
        return new String[] { interpret, title };
    }

    private static void loop() throws InterruptedException {
        while (true) {
            String content = readContent(ADDRESS);
            String[] result = parseContent(content);
            writeToFile(result);
            Thread.sleep(INTERVAL);
        }

    }

    private static void writeToFile(String[] result) {
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileWriter(FILENAME, true));
            String output = formatOutput(result);
            if(!output.equals(lastOutput)) {
                writer.println(new Date() + " " + output);
                lastOutput = output;
                System.out.println("Wrote into file: " + new Date() + " " + output);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(writer != null) {
                writer.close();
            }
        }
    }
    
    private static String formatOutput(String[] input) {
        return input[0] + " - " + input[1];
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
//        String content = readContent("http://www.gotv.at/titel_main.php");
//        System.out.println(content);
//        String result = parseContent(content);
//        System.out.println(result);
        loop();
    }

}

