From: Jan-Pascal van Best Date: Sun, 11 Mar 2012 20:06:33 +0000 (+0100) Subject: Heel stuk onderweg X-Git-Tag: 0.2~12 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=6063a7b4c3c8e5b08b680081c26dce8ddf6876fd;p=tv_grab_nl_java Heel stuk onderweg --- diff --git a/tv_grab_nl_java/src/org/vanbest/xmltv/Config.java b/tv_grab_nl_java/src/org/vanbest/xmltv/Config.java index 87a69d9..556a3f3 100644 --- a/tv_grab_nl_java/src/org/vanbest/xmltv/Config.java +++ b/tv_grab_nl_java/src/org/vanbest/xmltv/Config.java @@ -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 channels = new ArrayList(); 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); diff --git a/tv_grab_nl_java/src/org/vanbest/xmltv/Main.java b/tv_grab_nl_java/src/org/vanbest/xmltv/Main.java index f54ed3b..62a7ded 100644 --- a/tv_grab_nl_java/src/org/vanbest/xmltv/Main.java +++ b/tv_grab_nl_java/src/org/vanbest/xmltv/Main.java @@ -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 channels = gids.getChannels(); + TvGids gids = new TvGids(cacheFile); try { - List myChannels = channels; // .subList(0, 2); Set programmes = new HashSet(); - for( Channel c: myChannels ) { + for( Channel c: config.channels ) { ArrayList cs = new ArrayList(2); cs.add(c); Set 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 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(); + } } } diff --git a/tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java b/tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java index f30c1be..97da8e6 100644 --- a/tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java +++ b/tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java @@ -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 cache; - public ProgrammeCache() { + public ProgrammeCache(File cacheFile) { + this.cacheFile = cacheFile; if (cacheFile.canRead()) { try { cache = (Map) new ObjectInputStream( new FileInputStream( cacheFile ) ).readObject(); diff --git a/tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java b/tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java index 89db0d5..dd431bd 100644 --- a/tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java +++ b/tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java @@ -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")));