]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Heel stuk onderweg
authorJan-Pascal van Best <janpascal@vanbest.org>
Sun, 11 Mar 2012 20:06:33 +0000 (21:06 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Sun, 11 Mar 2012 20:06:33 +0000 (21:06 +0100)
tv_grab_nl_java/src/org/vanbest/xmltv/Config.java
tv_grab_nl_java/src/org/vanbest/xmltv/Main.java
tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java
tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java

index 87a69d9cfa1b52cac10ce494778e51b9cfba460f..556a3f3e83616797b0532e7efb28b1db9ec01bbb 100644 (file)
@@ -21,26 +21,18 @@ public class Config {
        public Config() {
        }
        
-       public void writeConfig(String filename) throws IOException {
-               FileUtils.forceMkdir(new File(filename).getParentFile());
-               PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( filename )));
+       public void writeConfig(File configFile) throws IOException {
+               FileUtils.forceMkdir(configFile.getParentFile());
+               PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( configFile )));
                for(Channel c: channels) {
                        out.println(c.id + ": " + c.name);
                }
                out.close();
        }
        
-       public static File defaultConfigFile() {
-               return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv", "tv_grab_nl_java.conf");
-       }
-
-       public static Config readConfig() throws IOException {
-               return readConfig(defaultConfigFile().getCanonicalPath());
-       }
-
-       public static Config readConfig(String filename) throws IOException {
+       public static Config readConfig(File file) throws IOException {
                Config result = new Config();
-               BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( filename)));
+               BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file)));
                List<Channel> channels = new ArrayList<Channel>();
                while(true) {
                        String s = reader.readLine();
@@ -48,7 +40,7 @@ public class Config {
                        if (!s.contains(":")) continue;
                        if (s.startsWith("#")) continue;
                        String[] parts = s.split("[[:space:]]*:[[:space:]]*", 2);
-                       Channel c = new Channel(Integer.parseInt(parts[0]), parts[1], "");
+                       Channel c = new Channel(Integer.parseInt(parts[0]), parts[1].trim(), "");
                        channels.add(c);
                }
                result.setChannels(channels);
index f54ed3b9e7ca8ab90f4b3dcac8ae01e0cf77137d..62a7dedf6b97a446326ebcb3ab1294ca1c7eaf63 100644 (file)
@@ -1,8 +1,11 @@
 package org.vanbest.xmltv;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -13,30 +16,41 @@ import org.apache.commons.cli.GnuParser;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.Parser;
+import org.apache.commons.io.FileUtils;
 
 public class Main {
        
+       private File configFile;
+       private PrintStream outputWriter;
+       private File cacheFile;
+       private int days = 1;
+       private int offset = 0;
        /**
         * @param args
         */
        
-       public static void test() {
-               TvGids gids = new TvGids();
+       public Main() {
+               this.configFile = defaultConfigFile();
+               this.outputWriter = System.out;
+               this.cacheFile = defaultCacheFile();
+       }
+       
+       public void run() throws IOException {
+               Config config = Config.readConfig(configFile);
                
-               List<Channel> channels = gids.getChannels();
+               TvGids gids = new TvGids(cacheFile);
                
                try {
-                       List<Channel> myChannels = channels; // .subList(0,  2);
                        Set<Programme> programmes = new HashSet<Programme>();
-                       for( Channel c: myChannels ) {
+                       for( Channel c: config.channels ) {
                                ArrayList<Channel> cs = new ArrayList<Channel>(2);
                                cs.add(c);
                                Set<Programme> p = gids.getProgrammes(cs, 0, true);
                                programmes.addAll( p );
                        }
                        
-                       XmlTvWriter writer = new XmlTvWriter(new FileOutputStream("/tmp/tv_grab_nl_java.xml"));
-                       writer.writeChannels(myChannels);
+                       XmlTvWriter writer = new XmlTvWriter(outputWriter);
+                       writer.writeChannels(config.channels);
                        writer.writePrograms(programmes);
                        writer.close();
                        
@@ -57,7 +71,7 @@ public class Main {
        }
        
        public void configure() {
-               TvGids gids = new TvGids();
+               TvGids gids = new TvGids(cacheFile);
                
                List<Channel> channels = gids.getChannels();
                System.out.println(channels);
@@ -65,7 +79,7 @@ public class Main {
                Config config = new Config();
                config.setChannels(channels);
                try {
-                       config.writeConfig(Config.defaultConfigFile().getCanonicalPath());
+                       config.writeConfig(configFile);
                } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
@@ -77,13 +91,13 @@ public class Main {
                
        }
        
-       public void processOptions(String[] args) {
+       public void processOptions(String[] args) throws FileNotFoundException {
                Options options = new Options();
                options.addOption("d", "description", false, "Display a description to identify this grabber");
                options.addOption("c", "capablities", false, "Show grabber capabilities");
                options.addOption("q", "quiet", false, "Be quiet");
                options.addOption("o", "output", true, "Set xlmtv output filename");
-               options.addOption("d", "days", true, "Number of days to grab");
+               options.addOption("y", "days", true, "Number of days to grab");
                options.addOption("s", "offset", true, "Start day for grabbing (0=today)");
                options.addOption("n", "configure", false, "Interactive configuration");
                options.addOption("f", "config-file", true, "Configuration file location");
@@ -102,6 +116,15 @@ public class Main {
                        System.out.println("tv_grab_nl_java is a parser for Dutch TV listings using the tvgids.nl JSON interface");
                        System.exit(0);
                }
+               if(line.hasOption("f")) { 
+                       configFile = new File(line.getOptionValue("f"));        
+               }
+               if (line.hasOption("o")) {
+                       this.outputWriter = new PrintStream( new FileOutputStream(line.getOptionValue("o")));
+               }
+               if (line.hasOption("h")) {
+                       this.cacheFile = new File(line.getOptionValue("h"));
+               }
                if (line.hasOption("c")) {
                        System.out.println("baseline");
                        System.out.println("manualconfig");
@@ -113,12 +136,26 @@ public class Main {
                        configure();
                        System.exit(0);
                }
-                       
+
        }
        
-       public static void main(String[] args) {
+       public static File defaultConfigFile() {
+               return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv", "tv_grab_nl_java.conf");
+       }
+       
+       public static File defaultCacheFile() {
+               return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv", "tv_grab_nl_java.cache");
+       }
+
+       public static void main(String[] args)  {
                Main main = new Main();
-               main.processOptions(args);
+               try {
+                       main.processOptions(args);
+                       main.run();
+               } catch (IOException e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
        }
 
 }
index f30c1be591ef15d18c6ebd84422da928ac8af1fd..97da8e6684330c15d22015f796bb145d6ad79cfd 100644 (file)
@@ -14,13 +14,11 @@ import java.util.Map;
 import org.apache.commons.io.FileUtils;
 
 public class ProgrammeCache {
-       
-       static String cacheDir = "/tmp/tv_grab_nl_java";
-       
-       private File cacheFile = new File(cacheDir);
+       private File cacheFile;
        private Map<String,ProgrammeDetails> cache;
        
-       public ProgrammeCache() {
+       public ProgrammeCache(File cacheFile) {
+               this.cacheFile = cacheFile;
                if (cacheFile.canRead()) {
                        try {
                                cache = (Map<String,ProgrammeDetails>) new ObjectInputStream( new FileInputStream( cacheFile ) ).readObject();
index 89db0d583c379a4be95d65b1be9ca1a9f4252dfc..dd431bd84c1eca5fafcbdc819cc5188bb8eafd2f 100644 (file)
@@ -1,6 +1,7 @@
 package org.vanbest.xmltv;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -30,8 +31,8 @@ public class TvGids {
        ProgrammeCache cache;
        boolean initialised = false;
        
-       public TvGids() {
-               cache = new ProgrammeCache();
+       public TvGids(File cacheFile) {
+               cache = new ProgrammeCache(cacheFile);
                if ( ! initialised ) {
                        String[] formats = {"yyyy-MM-dd HH:mm:ss"};
                        JSONUtils.getMorpherRegistry().registerMorpher( new DateMorpher(formats, new Locale("nl")));