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();
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);
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;
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();
}
public void configure() {
- TvGids gids = new TvGids();
+ TvGids gids = new TvGids(cacheFile);
List<Channel> channels = gids.getChannels();
System.out.println(channels);
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();
}
- 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");
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");
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();
+ }
}
}