import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.apache.commons.io.FileUtils;
public class Config {
public List<Channel> channels;
+ public Map<String, String> cattrans;
+ protected File cacheFile;
- public Config() {
+ private Config() {
}
+ public Map<String,String> getCategories() {
+ return cattrans;
+ }
+
+ public static File defaultCacheFile() {
+ return FileUtils.getFile(FileUtils.getUserDirectory(), ".xmltv", "tv_grab_nl_java.cache");
+ }
+
+ static private Map<String,String> getDefaultCattrans() {
+ Map<String,String> result = new HashMap<String,String>();
+ result.put("amusement", "Animated");
+ result.put("comedy", "Comedy");
+ result.put("documentaire", "Documentary");
+ result.put("educatief", "Educational");
+ result.put("erotiek", "Adult");
+ result.put("film", "Film");
+ result.put("muziek", "Art/Music");
+ result.put("informatief", "Educational");
+ result.put("jeugd", "Children");
+ result.put("kunst/cultuur", "Arts/Culture");
+ result.put("misdaad", "Crime/Mystery");
+ result.put("muziek", "Music");
+ result.put("natuur", "Science/Nature");
+ result.put("nieuws/actualiteiten", "News");
+ result.put("overige", "Unknown");
+ result.put("religieus", "Religion");
+ result.put("serie/soap", "Drama");
+ result.put("sport", "Sports");
+ result.put("theater", "Arts/Culture");
+ result.put("wetenschap", "Science/Nature");
+ return result;
+ }
+
public void writeConfig(File configFile) throws IOException {
FileUtils.forceMkdir(configFile.getParentFile());
PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( configFile )));
+ out.println("cache-file: " + escape(cacheFile.getPath()));
for(Channel c: channels) {
if (!c.selected) {
out.print("#");
}
out.println();
}
+ for(Map.Entry<String,String> entry: cattrans.entrySet()) {
+ out.println("category: " + escape(entry.getKey()) + ": " + escape(entry.getValue()));
+ }
out.close();
}
public static String escape(String s) {
- return "\"" + s.replaceAll("\\\"", "\\\\\"") + "\"";
+ return "\"" + s.replace("\\", "\\\\").replaceAll("\\\"", "\\\\\"") + "\"";
}
public static List<String> splitLine(String s) {
for (; pos<s.length(); pos++) {
if (s.charAt(pos)=='"') {
if (quoted) {
- buf.append(s.substring(quoteStart, pos).replaceAll("\\\\\"", "\\\""));
+ //System.out.println(s.substring(quoteStart, pos));
+ buf.append(s.substring(quoteStart, pos).replaceAll("\\\\\"", "\\\"").replaceAll("\\\\\\\\","\\\\"));
} else {
quoteStart = pos+1;
}
try {
BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file)));
List<Channel> channels = new ArrayList<Channel>();
+ Map<String,String> cattrans = new HashMap<String,String>();
+ File cacheFile = defaultCacheFile();
while(true) {
String s = reader.readLine();
if(s==null) break;
if (parts.size()>3) {
c.setIconUrl(parts.get(3));
}
- channels.add(c);
+ channels.add(c);
+ }
+ if (parts.get(0).toLowerCase().equals("category")) {
+ cattrans.put(parts.get(1), parts.get(2));
+ }
+ if (parts.get(0).toLowerCase().equals("cache-file")) {
+ cacheFile = new File(parts.get(1));
}
}
result.setChannels(channels);
+ result.cattrans = cattrans;
+ result.cacheFile = cacheFile;
+ System.out.println("CAche file: "+cacheFile.getPath());
} catch (IOException e) {
e.printStackTrace();
System.out.println("Cannot read configuration file, continuing with empty configuration");
+ return getDefaultConfig();
}
return result;
}
+ public static Config getDefaultConfig() {
+ Config result = new Config();
+ result.channels = new ArrayList<Channel>();
+ result.cattrans = getDefaultCattrans();
+ result.cacheFile = defaultCacheFile();
+ return result;
+ }
+
public void setChannels(List<Channel> channels) {
this.channels = channels;
}
import org.apache.commons.io.FileUtils;
public class Main {
-
private File configFile;
+ private Config config;
private PrintStream outputWriter;
- private File cacheFile;
private int days = 5;
private int offset = 0;
private boolean quiet = false;
public Main() {
this.configFile = defaultConfigFile();
this.outputWriter = System.out;
- this.cacheFile = defaultCacheFile();
}
public void run() throws FactoryConfigurationError, Exception {
- Config config = Config.readConfig(configFile);
-
if (!quiet) {
System.out.println("Fetching programme data for days " + this.offset + "-" + (this.offset+this.days-1));
System.out.println("... from " + config.channels.size() + " channels");
- System.out.println("... using cache file " + cacheFile.getCanonicalPath());
+ System.out.println("... using cache file " + config.cacheFile.getCanonicalPath());
}
XmlTvWriter writer = new XmlTvWriter(outputWriter);
writer.writeChannels(config.channels);
- TvGids gids = new TvGids(cacheFile);
+ TvGids gids = new TvGids(config);
for (int day=offset; day<offset+days; day++) {
if (!quiet) System.out.print("Fetching information for day " + day);
}
public void configure() throws IOException {
- TvGids gids = new TvGids(cacheFile);
+ TvGids gids = new TvGids(config);
List<Channel> channels = gids.getChannels();
}
}
- Config config = new Config();
config.setChannels(channels);
try {
config.writeConfig(configFile);
// TODO Auto-generated catch block
e.printStackTrace();
}
-
-
}
public void processOptions(String[] args) throws FileNotFoundException {
if(line.hasOption("f")) {
configFile = new File(line.getOptionValue("f"));
}
+ config = Config.readConfig(configFile);
+
if (line.hasOption("o")) {
this.outputWriter = new PrintStream( new FileOutputStream(line.getOptionValue("o")));
}
if (line.hasOption("h")) {
- this.cacheFile = new File(line.getOptionValue("h"));
+ config.cacheFile = new File(line.getOptionValue("h"));
}
if (line.hasOption("y")) {
this.days = Integer.parseInt(line.getOptionValue("y"));
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();
try {
ProgrammeDetails details = null;
Channel channel = null;
- public void fixup() {
+ public void fixup(Config config) {
this.titel = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(titel);
this.genre = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(genre);
this.soort = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(soort);
this.highlight_content = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(highlight_content);
+ if(config.getCategories().containsKey(genre.toLowerCase())) {
+ genre = config.getCategories().get(genre.toLowerCase());
+ }
}
public String toString() {
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
if (cacheFile.canRead()) {
try {
cache = (Map<String,ProgrammeDetails>) new ObjectInputStream( new FileInputStream( cacheFile ) ).readObject();
- } catch (Exception e) {
+ } catch (InvalidClassException e) {
+ // TODO Auto-generated catch block
+
+ cache = new HashMap<String,ProgrammeDetails>();
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ cache = new HashMap<String,ProgrammeDetails>();
+ } catch (FileNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ cache = new HashMap<String,ProgrammeDetails>();
+ } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
cache = new HashMap<String,ProgrammeDetails>();
static String programme_base_url="http://www.tvgids.nl/json/lists/programs.php";
static String detail_base_url = "http://www.tvgids.nl/json/lists/program.php";
+ Config config;
ProgrammeCache cache;
static boolean initialised = false;
int fetchErrors = 0;
int cacheHits = 0;
int cacheMisses = 0;
- public TvGids(File cacheFile) {
- cache = new ProgrammeCache(cacheFile);
+ public TvGids(Config config) {
+ this.config = config;
+ cache = new ProgrammeCache(config.cacheFile);
if ( ! initialised ) {
init();
initialised = true;
for( int i=0; i<programs.size(); i++ ) {
JSONObject programme = programs.getJSONObject(i);
Programme p = (Programme) JSONObject.toBean(programme, Programme.class);
- p.fixup();
+ p.fixup(config);
if (fetchDetails) {
fillDetails(p);
}
for( Object o: programs.keySet() ) {
JSONObject programme = programs.getJSONObject(o.toString());
Programme p = (Programme) JSONObject.toBean(programme, Programme.class);
+ p.fixup(config);
if (fetchDetails) {
fillDetails(p);
}
writer.writeAttribute("source-info-url", "http://tvgids.nl/");
writer.writeAttribute("source-info-name", "TvGids.nl");
writer.writeAttribute("generator-info-name", "tv_grab_nl_java $VERSION");
- writer.writeCharacters("\n");
+ writeln();
+ }
+
+ public void writeln() throws XMLStreamException {
+ writer.writeCharacters(System.getProperty("line.separator"));
}
public void writeChannels(List<Channel> channels) throws XMLStreamException {
}
writer.writeEndElement();
- writer.writeCharacters("\n");
- }
+ writeln(); }
}
/* TODO:
writer.writeAttribute("start", df.format(p.datum_start));
writer.writeAttribute("stop", df.format(p.datum_end));
writer.writeAttribute("channel", ""+p.channel.getChannelId());
- writer.writeCharacters("\n");
+ writeln();
writer.writeStartElement("title");
writer.writeAttribute("lang", "nl");
writer.writeCharacters(p.titel);
writer.writeEndElement();
- writer.writeCharacters("\n");
-
+ writeln();
+
if(p.details.synop != null && ! p.details.synop.isEmpty()) {
writer.writeStartElement("desc");
writer.writeAttribute("lang", "nl");
writer.writeCharacters(p.details.synop);
writer.writeEndElement();
- writer.writeCharacters("\n");
+ writeln();
}
if (p.details != null) {
}
}
writer.writeEndElement();
- writer.writeCharacters("\n");
+ writeln();
}
writer.writeStartElement("category");
writer.writeAttribute("lang", "en");
writer.writeCharacters(p.genre); // soort? FIXME translation to mythtv categories
writer.writeEndElement();
- writer.writeCharacters("\n");
+ writeln();
if (p.details.kijkwijzer != null && !p.details.kijkwijzer.isEmpty()) {
writer.writeStartElement("rating");
writer.writeCharacters(p.details.kijkwijzer);
writer.writeEndElement();
writer.writeEndElement();
- writer.writeCharacters("\n");
+ writeln();
}
}
writer.writeEndElement();
- writer.writeCharacters("\n");
+ writeln();
}
}