]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Properly implement --configure
authorJP <jp@here>
Tue, 13 Mar 2012 09:39:39 +0000 (10:39 +0100)
committerJP <jp@here>
Tue, 13 Mar 2012 09:39:39 +0000 (10:39 +0100)
some refactoring
cleanup html from programme description

tv_grab_nl_java/src/org/vanbest/xmltv/Channel.java
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/ProgrammeDetails.java
tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java

index 9f06133346ed4531d0211de03ab0a0e8939f0e32..5182d10df715f6484d6a2992e4c10bd040b2a50e 100644 (file)
@@ -5,6 +5,8 @@ public class Channel {
     String name;
     String shortName;
        String iconUrl;
+       boolean selected;
+       
     public Channel(int id, String name, String shortName) {
        this.id = id;
        this.name = name;
index 9ad9e604f2a4399b5eb1c04c7d965433b1298e5b..666acac8e3559617526d125d698de9c11a2245b1 100644 (file)
@@ -25,7 +25,10 @@ public class Config {
                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));
                        }
@@ -36,16 +39,19 @@ public class Config {
        
        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);
@@ -76,24 +82,54 @@ public class Config {
                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;
        }
        
@@ -102,3 +138,4 @@ public class Config {
        }
 
 }
+
index 7b042cdca5e5a5f719c99796c42775472d3a573b..0d0a6d685b2fcecdc7ecec6170ec7fc16de0cd9b 100644 (file)
@@ -1,9 +1,11 @@
 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;
@@ -42,6 +44,12 @@ public class Main {
        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);
 
@@ -70,13 +78,50 @@ public class Main {
                }
 
                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);
@@ -144,7 +189,12 @@ public class Main {
                        System.exit(0);
                }
                if (line.hasOption("n")) {
-                       configure();
+                       try {
+                               configure();
+                       } catch (IOException e) {
+                               // TODO Auto-generated catch block
+                               e.printStackTrace();
+                       }
                        System.exit(0);
                }
 
index 6cf663d01511cae53f46dce4ab17b0fedc07e603..2638c5cce0b73f813b3564367536f59fbaa7e275 100644 (file)
@@ -21,15 +21,22 @@ public class ProgrammeDetails implements Serializable {
                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);
index db49ff1ac3dd599266986fd69cb0397316eca55d..33b3900b14da2c17b0da4d138892d5f1b96c8418 100644 (file)
@@ -32,6 +32,7 @@ public class TvGids {
 
        ProgrammeCache cache;
        static boolean initialised = false;
+       int fetchErrors = 0;
        
        public TvGids(File cacheFile) {
                cache = new ProgrammeCache(cacheFile);
@@ -147,12 +148,7 @@ public class TvGids {
                                        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 );
@@ -163,12 +159,7 @@ public class TvGids {
                                        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 );
@@ -187,15 +178,20 @@ public class TvGids {
                        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);
+               }
        }
 }