String name;
String shortName;
String iconUrl;
+ boolean selected;
+
public Channel(int id, String name, String shortName) {
this.id = id;
this.name = name;
FileUtils.forceMkdir(configFile.getParentFile());
PrintWriter out = new PrintWriter(new OutputStreamWriter( new FileOutputStream( configFile )));
for(Channel c: channels) {
- out.print(c.id + ": " + escape(c.name));
+ if (!c.selected) {
+ out.print("#");
+ }
+ out.print("channel: " + c.id + ": " + escape(c.name));
if (c.iconUrl != null) {
out.print(" : " + escape(c.iconUrl));
}
public static String unescape(String s) {
String result = s.trim();
- result = result.substring(1, result.length()-1);
- result = result.replaceAll("\\\"", "\"");
+ if (result.charAt(0)=='"') {
+ result = result.substring(1, result.length()-1);
+ result = result.replaceAll("\\:",":").replaceAll("\\\"", "\"");
+ }
return result;
}
public static String escape(String s) {
- return "\"" + s.replaceAll("\"", "\\\"") + "\"";
+ return "\"" + s.replaceAll("\"", "\\\"").replaceAll(":", "\\:") + "\"";
}
-
- public static String[] splitLine(String s) {
+
+
+ public static String[] old_splitLine(String s) {
int colon = 0;
while (true) {
colon = s.indexOf(':', colon+1);
String[] parts = {id,name,icon};
return parts;
}
+ public static List<String> splitLine(String s) {
+ int colon = 0;
+ int prev = 0;
+ List<String> parts = new ArrayList<String>(5);
+ while (true) {
+ // Find first unescaped colon
+ while (true) {
+ colon = s.indexOf(':', prev);
+ if (colon<0 || s.charAt(colon-1) != '\\') {
+ break;
+ }
+ }
+ if (colon<0) {
+ // Last part
+ parts.add(unescape(s.substring(prev)));
+ break;
+ } else {
+ parts.add(unescape(s.substring(prev,colon)));
+ prev = colon+1;
+ }
+ }
+ return parts;
+ }
- public static Config readConfig(File file) throws IOException {
+ public static Config readConfig(File file) {
Config result = new Config();
- BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file)));
- List<Channel> channels = new ArrayList<Channel>();
- while(true) {
- String s = reader.readLine();
- if(s==null) break;
- if (!s.contains(":")) continue;
- if (s.startsWith("#")) continue;
- String[] parts = splitLine(s);
- Channel c = new Channel(Integer.parseInt(parts[0]), parts[1], "");
- if (parts.length>2) {
- c.setIconUrl(parts[2]);
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file)));
+ List<Channel> channels = new ArrayList<Channel>();
+ while(true) {
+ String s = reader.readLine();
+ if(s==null) break;
+ if (!s.contains(":")) continue;
+ if (s.startsWith("#")) continue;
+ List<String> parts = splitLine(s);
+ if (parts.get(0).toLowerCase().equals("channel")) {
+ Channel c = new Channel(Integer.parseInt(parts.get(1)), parts.get(2), "");
+ if (parts.size()>3) {
+ c.setIconUrl(parts.get(3));
+ }
+ channels.add(c);
+ }
}
- channels.add(c);
+ result.setChannels(channels);
+ } catch (IOException e) {
+ e.printStackTrace();
+ System.out.println("Cannot read configuration file, continuing with empty configuration");
}
- result.setChannels(channels);
return result;
}
}
}
+
package org.vanbest.xmltv;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
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());
+ }
+
XmlTvWriter writer = new XmlTvWriter(outputWriter);
writer.writeChannels(config.channels);
}
writer.close();
+ if (!quiet) {
+ System.out.println("Number of fetch errors: " + gids.fetchErrors);
+ }
}
- public void configure() {
+ public void configure() throws IOException {
TvGids gids = new TvGids(cacheFile);
List<Channel> channels = gids.getChannels();
- //System.out.println(channels);
+
+ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+ boolean all = false;
+ boolean none = false;
+ for (Channel c: channels) {
+ System.out.print("add channel " + c.id + " (" + c.name + ") [[y]es,[n]o,[a]ll,[none] (default=yes)] ");
+ if (all) {
+ c.selected = true;
+ System.out.println("Y");
+ continue;
+ }
+ if (none) {
+ c.selected = false;
+ System.out.println("N");
+ continue;
+ }
+ while(true) {
+ String s = reader.readLine().toLowerCase();
+ if ( s.isEmpty() || s.startsWith("y")) {
+ c.selected = true;
+ break;
+ } else if ( s.startsWith("a")) {
+ c.selected = true;
+ all = true;
+ break;
+ } else if ( s.startsWith("none")) {
+ c.selected = false;
+ none = true;
+ break;
+ } else if ( s.startsWith("n")) {
+ c.selected = false;
+ break;
+ }
+ }
+ }
Config config = new Config();
config.setChannels(channels);
System.exit(0);
}
if (line.hasOption("n")) {
- configure();
+ try {
+ configure();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
System.exit(0);
}
this.genre = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(genre);
this.synop = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(synop);
if ((synop == null || synop.isEmpty()) && ( genre == null || (!genre.toLowerCase().equals("movies") && !genre.toLowerCase().equals("film")))) {
- //System.out.println("Splitting title: \"" + p.titel + "\"");
String[] parts = p.titel.split("[[:space:]]*:[[:space:]]*", 2);
if (parts.length >= 2 ) {
titel = parts[0].trim();
p.titel = titel;
synop = parts[1].trim();
- //System.out.println("Splitting title to : \"" + p.titel + "\"; synop: \"" + synop + "\"");
+ System.out.println("Splitting title to : \"" + p.titel + "\"; synop: \"" + synop + "\"");
}
}
+ this.synop = this.synop.replaceAll("<br>", " ");
+ this.synop = this.synop.replaceAll("<br />", " ");
+ this.synop = this.synop.replaceAll("<p>", " ");
+ this.synop = this.synop.replaceAll("</p>", " ");
+ this.synop = this.synop.replaceAll("<strong>", " ");
+ this.synop = this.synop.replaceAll("</strong>", " ");
+ this.synop = this.synop.replaceAll("<em>", " ");
+ this.synop = this.synop.replaceAll("</em>", " ");
this.presentatie = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(presentatie);
this.acteursnamen_rolverdeling = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(acteursnamen_rolverdeling);
this.regisseur = org.apache.commons.lang.StringEscapeUtils.unescapeHtml(regisseur);
ProgrammeCache cache;
static boolean initialised = false;
+ int fetchErrors = 0;
public TvGids(File cacheFile) {
cache = new ProgrammeCache(cacheFile);
Programme p = (Programme) JSONObject.toBean(programme, Programme.class);
p.fixup();
if (fetchDetails) {
- p.details = cache.getDetails(p.db_id);
- if ( p.details == null ) {
- p.details = getDetails(p.db_id);
- p.details.fixup(p);
- cache.add(p.db_id, p.details);
- }
+ fillDetails(p);
}
p.channel = c;
result.add( p );
JSONObject programme = programs.getJSONObject(o.toString());
Programme p = (Programme) JSONObject.toBean(programme, Programme.class);
if (fetchDetails) {
- try {
- p.details = getDetails(p.db_id);
- } catch (Exception e) {
- e.printStackTrace();
- continue;
- }
+ fillDetails(p);
}
p.channel = c;
result.add( p );
String s;
while ((s = reader.readLine()) != null) json.append(s);
} catch (IOException e) {
+ fetchErrors++;
throw new Exception("Error getting program data from url " + url, e);
}
return JSONObject.fromObject( json.toString() );
}
- private ProgrammeDetails getDetails(String db_id) throws Exception {
- URL url = detailUrl(db_id);
- JSONObject json = fetchJSON(url);
- //System.out.println( json );
- return (ProgrammeDetails) JSONObject.toBean(json, ProgrammeDetails.class);
+ private void fillDetails(Programme p) throws Exception {
+ p.details = cache.getDetails(p.db_id);
+ if ( p.details == null ) {
+ URL url = detailUrl(p.db_id);
+ JSONObject json = fetchJSON(url);
+ p.details = (ProgrammeDetails) JSONObject.toBean(json, ProgrammeDetails.class);
+ p.details.fixup(p);
+ cache.add(p.db_id, p.details);
+ }
}
}