]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Translate categories
authorJan-Pascal van Best <janpascal@vanbest.org>
Wed, 14 Mar 2012 13:16:33 +0000 (14:16 +0100)
committerJan-Pascal van Best <janpascal@vanbest.org>
Wed, 14 Mar 2012 13:16:33 +0000 (14:16 +0100)
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/Programme.java
tv_grab_nl_java/src/org/vanbest/xmltv/ProgrammeCache.java
tv_grab_nl_java/src/org/vanbest/xmltv/TvGids.java
tv_grab_nl_java/src/org/vanbest/xmltv/XmlTvWriter.java

index 45a2687d258c7aa569dd981472c3d03923039dec..095f8c1a218e522c6a490eed5b65cbe0b0e9e677 100644 (file)
@@ -11,19 +11,57 @@ import java.io.InputStreamReader;
 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("#");
@@ -34,11 +72,14 @@ public class Config {
                        }
                        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) {
@@ -52,7 +93,8 @@ public class Config {
                        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;
                                        }
@@ -79,6 +121,8 @@ public class Config {
                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;
@@ -90,17 +134,35 @@ public class Config {
                                        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;
        }
index f30467bd84de007f49056bac4dd647f6ed29972e..bef8acde73949bc235cb83c45f1b2339a1d75626 100644 (file)
@@ -25,10 +25,9 @@ import org.apache.commons.cli.Parser;
 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;
@@ -39,22 +38,19 @@ public class Main {
        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);
@@ -89,7 +85,7 @@ public class Main {
        }
        
        public void configure() throws IOException {
-               TvGids gids = new TvGids(cacheFile);
+               TvGids gids = new TvGids(config);
                
                List<Channel> channels = gids.getChannels();
                
@@ -128,7 +124,6 @@ public class Main {
                        }
                }
                
-               Config config = new Config();
                config.setChannels(channels);
                try {
                        config.writeConfig(configFile);
@@ -140,8 +135,6 @@ public class Main {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
-               
-               
        }
        
        public void processOptions(String[] args) throws FileNotFoundException {
@@ -182,11 +175,13 @@ public class Main {
                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"));
@@ -217,10 +212,6 @@ public class Main {
                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 {
index b90def785371730082add90627bfd886d95efc7b..e5ae3e1f1f5d0511e44e8fd3b46a2846686d280c 100644 (file)
@@ -105,11 +105,14 @@ public class Programme {
          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() {
index 97da8e6684330c15d22015f796bb145d6ad79cfd..c9a98589979e3cb4f7ed64320f06e675e38030e8 100644 (file)
@@ -6,6 +6,7 @@ import java.io.FileNotFoundException;
 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;
@@ -22,7 +23,19 @@ public class ProgrammeCache {
                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>();
index 652e0b238217120d8eb65361893454b167d26301..30e6a31e350c26e786f8a63328853a81456a91ec 100644 (file)
@@ -30,14 +30,16 @@ public class TvGids {
        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;
@@ -148,7 +150,7 @@ public class TvGids {
                                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);
                                        }
@@ -160,6 +162,7 @@ public class TvGids {
                                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);
                                        }
index a07b78b20179a63b4982267487df1ead1ac6cca8..7cb94867afb2576609e19deb5487f0ecb26132c0 100644 (file)
@@ -30,7 +30,11 @@ public class XmlTvWriter {
                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 {
@@ -49,8 +53,7 @@ public class XmlTvWriter {
                                }
 
                        writer.writeEndElement();
-                       writer.writeCharacters("\n");
-               }
+                       writeln();              }
        }
 
        /* TODO: 
@@ -67,20 +70,20 @@ public class XmlTvWriter {
                                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) {
@@ -126,13 +129,13 @@ public class XmlTvWriter {
                                                        }
                                                }
                                                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");
@@ -141,11 +144,11 @@ public class XmlTvWriter {
                                                                writer.writeCharacters(p.details.kijkwijzer);
                                                        writer.writeEndElement();
                                                writer.writeEndElement();
-                                               writer.writeCharacters("\n");
+                                               writeln();
                                        }
                                }
                        writer.writeEndElement();
-                       writer.writeCharacters("\n");
+                       writeln();
                }
        }