]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
Internal: provide charset for JSOn fetch
authorJan-Pascal van Best <janpascal@vanbest.org>
Fri, 24 Apr 2015 07:43:34 +0000 (09:43 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Fri, 24 Apr 2015 07:43:34 +0000 (09:43 +0200)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Horizon.java

index a0d418f64f0c1848ab5aa8071c48135d6427458b..4684ea7f852033ffdebdd81ae3403901bc673316 100644 (file)
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -59,13 +60,17 @@ public abstract class AbstractEPGSource implements EPGSource {
        }
 
        protected String fetchURL(URL url) throws Exception {
+               return fetchURL(url, Charset.defaultCharset().name());
+       }
+       
+       protected String fetchURL(URL url, String charset) throws Exception {
                StringBuffer buf = new StringBuffer();
                boolean done = false;
                for (int count = 0; !done; count++) {
                        Thread.sleep(config.niceMilliseconds*(1<<count));
                        try {
                                BufferedReader reader = new BufferedReader(
-                                               new InputStreamReader(url.openStream()));
+                                               new InputStreamReader(url.openStream(), charset));
                                String s;
                                while ((s = reader.readLine()) != null)
                                        buf.append(s);
@@ -86,12 +91,16 @@ public abstract class AbstractEPGSource implements EPGSource {
                return buf.toString();
        }
 
-       protected JSONObject fetchJSON(URL url) throws Exception {
-               String json = fetchURL(url);
+       protected JSONObject fetchJSON(URL url, String charset) throws Exception {
+               String json = fetchURL(url, charset);
                logger.debug(json);
                return JSONObject.fromObject(json);
        }
 
+       protected JSONObject fetchJSON(URL url) throws Exception {
+               return fetchJSON(url, Charset.defaultCharset().name());
+       }
+
        public void clearCache() {
                cache.clear(getName());
        }
index e38d96d9b119c69f660729548e8db83bac608a05..e3bd6fea22824cf82b286073ea18c66eff78cd37 100644 (file)
@@ -16,30 +16,20 @@ package org.vanbest.xmltv;
  The full license text can be found in the LICENSE file.
  */
 
-import java.io.BufferedReader;
 import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
-import java.util.Locale;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.apache.commons.lang.StringEscapeUtils;
-import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 
-import net.sf.json.JSON;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 
@@ -100,7 +90,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
                }
                JSONObject channels;
                try {
-                       channels = fetchJSON(url);
+                       channels = fetchJSON(url, "UTF8");
                } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
@@ -161,7 +151,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
                for (Channel c : channels) {
                        URL url = programmeUrl(c, day);
                        logger.debug("Programme url:" + url);
-                       JSONObject jsonObject = fetchJSON(url);
+                       JSONObject jsonObject = fetchJSON(url, "UTF-8");
                        logger.debug(jsonObject.toString());
                        JSONArray listings = jsonObject.getJSONArray("listings");
                        for (int i = 0; i < listings.size(); i++) {
@@ -317,6 +307,7 @@ public class Horizon extends AbstractEPGSource implements EPGSource {
         * @param args
         */
        public static void main(String[] args) {
+               Logger.getRootLogger().setLevel(Level.TRACE);
                Config config = Config.getDefaultConfig();
                Horizon horizon = new Horizon(config);
                horizon.clearCache();