BarcodeWebLookup.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 by Moritz Tenorth
00003  * 
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 3 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 package edu.tum.cs.ias.knowrob;
00019 
00020 import ros.*;
00021 import ros.pkg.comp_cop.srv.LookupBarcode;
00022 
00023 import java.util.regex.Matcher;
00024 import java.util.regex.Pattern;
00025 import java.io.IOException;
00026 import java.io.UnsupportedEncodingException;
00027 
00028 import org.apache.http.HttpEntity;
00029 import org.apache.http.HttpResponse;
00030 import org.apache.http.HttpVersion;
00031 import org.apache.http.client.*;
00032 import org.apache.http.client.methods.HttpGet;
00033 import org.apache.http.impl.client.DefaultHttpClient;
00034 import org.apache.http.params.CoreProtocolPNames;
00035 import org.apache.http.util.EntityUtils;
00036 
00037 import com.google.api.translate.Language;
00038 import com.google.api.translate.Translate;
00039 
00040 
00041 public class BarcodeWebLookup {
00042 
00043         static Boolean rosInitialized = false;
00044         static Ros ros;
00045         static NodeHandle n;
00046         
00047         
00048         public BarcodeWebLookup() {
00049 
00050                 try {
00051 
00052                         initRos();
00053                         
00054                         n.advertiseService("/barcode_web_lookup",  new LookupBarcode(),  new LookupBarcodeCallback());
00055                         ros.spin();
00056                         
00057                 } catch(RosException e) {
00058                         e.printStackTrace();
00059                 }
00060         }
00061         
00065         protected static void initRos() {
00066 
00067         ros = Ros.getInstance();
00068 
00069                 if(!Ros.getInstance().isInitialized()) {
00070                 ros.init("knowrob_barcode_web_lookup");
00071                 }
00072                 n = ros.createNodeHandle();
00073 
00074         }
00075         
00076 
00077         public static String lookUpUpcDatabase(String ean) {
00078                 
00079                 String res="";
00080                 if (ean != null && ean.length()>0) {
00081                         
00082                         HttpClient httpclient = new DefaultHttpClient();
00083 
00084                         try {
00085                                 
00086                                 HttpGet httpget = new HttpGet("http://www.upcdatabase.com/item/"+ean);
00087                                 httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
00088                                 
00089                         // Create a response handler
00090                                 
00091                                 ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
00092                                     public byte[] handleResponse(
00093                                             HttpResponse response) throws ClientProtocolException, IOException {
00094                                         HttpEntity entity = response.getEntity();
00095                                         if (entity != null) {
00096                                             return EntityUtils.toByteArray(entity);
00097                                         } else {
00098                                             return null;
00099                                         }
00100                                     }
00101                                 };
00102 
00103                                 String responseBody= new String(httpclient.execute(httpget, handler), "UTF-8");
00104                         
00105                         
00106                                 // return null if not found
00107                                 if(responseBody.contains("Item Not Found"))
00108                                         return null;
00109                                 else if(responseBody.contains("Item Record")) {
00110 
00111                                         // Parse response document
00112                                         Matcher matcher = Pattern.compile("<tr><td>Description<\\/td><td><\\/td><td>(.*)<\\/td><\\/tr>").matcher(responseBody);
00113                                         if(matcher.find()) {
00114                                                 res=matcher.group(1);
00115                                         }
00116                                         
00117                                 }
00118                                 
00119                         } catch (UnsupportedEncodingException uee) {
00120                                 uee.printStackTrace();
00121                         } catch (ClientProtocolException cpe) {
00122                                 cpe.printStackTrace();
00123                         } catch (IOException ioe) {
00124                                 ioe.printStackTrace();
00125                         } finally {
00126                                 if (httpclient != null) {
00127                                         httpclient.getConnectionManager().shutdown();
00128                                 }
00129                         }
00130                         
00131                 }
00132                 return res;
00133         }
00134         
00135         
00136         public static String translate(String word) {
00137                 
00138             String translatedText="";
00139             if(word!=null && word.length()>0) {
00140                     try{
00141                         Translate.setHttpReferrer("http://ias.cs.tum.edu");
00142                         translatedText = Translate.execute(word, Language.GERMAN, Language.ENGLISH);
00143                     } catch (Exception e) {
00144                         e.printStackTrace();
00145                     }
00146             }
00147             
00148             return translatedText;
00149         }
00150 
00151 
00152         
00153         // MT: xmlrpc seems to be broken, is always adding a '0' at the beginning
00154         public static String getUPCText(String upc) {
00155             String text = "";
00156 //          try
00157 //          {
00158 //              XmlRpcClient client  = new XmlRpcClient( "http://www.upcdatabase.com/rpc", false);
00159 //              String[] args = new String[1];
00160 //              args[0]=upc;
00161 //              Object result  = client.invoke( "lookupUPC", args );
00162 //              return result.toString();
00163 //
00164 //          }
00165 //          catch (Exception e)
00166 //          {
00167 //              e.printStackTrace();
00168 //          }
00169             return text;
00170         }
00171         
00172 
00173 
00174         
00175         // MT: does not work -- they seem to check if there is a real user agent here (?)
00176         public static String lookUpEANsearch(String ean) {
00177                 
00178                 String res="";
00179                 if (ean != null && ean.length()>0) {
00180                         
00181 
00182                         HttpClient httpclient = new DefaultHttpClient();
00183                         httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
00184                         //httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-16");
00185 
00186 
00187                         try {
00188                                 
00189                                 HttpGet httpget = new HttpGet("http://www.ean-search.org/perl/ean-search.pl?ean="+ean+"&os=1");
00190                                 httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
00191                                 httpget.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "ASCII");
00192 
00193                                  
00194                                 System.out.println(httpget.getURI());
00195 
00196                         // Create a response handler
00197                                 
00198                                 ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
00199                                     public byte[] handleResponse(
00200                                             HttpResponse response) throws ClientProtocolException, IOException {
00201                                         HttpEntity entity = response.getEntity();
00202                                         if (entity != null) {
00203                                             return EntityUtils.toByteArray(entity);
00204                                         } else {
00205                                             return null;
00206                                         }
00207                                     }
00208                                 };
00209                                 
00210                         byte[] response = httpclient.execute(httpget, handler);
00211 
00212                         
00213                         
00214                         //      String responseBody = httpclient.execute(httpget, handler);
00215                                 
00216 
00217 //                              new HeapByteBuffer(responseBody.getBytes(), 0, responseBody.getBytes().length));
00218 //                              
00219 //                              Charset a  = Charset.forName("UTF-8");
00220 //                              a.newEncoder().encode(responseBody.getBytes());
00221 //                              
00222 //                              System.out.println(responseBody);
00223 
00224                                 // Parse response document
00225                                 res = response.toString();
00226                                 
00227                         } catch (UnsupportedEncodingException uee) {
00228                                 uee.printStackTrace();
00229                         } catch (ClientProtocolException cpe) {
00230                                 cpe.printStackTrace();
00231                         } catch (IOException ioe) {
00232                                 ioe.printStackTrace();
00233                         } finally {
00234                                 if (httpclient != null) {
00235                                         httpclient.getConnectionManager().shutdown();
00236                                 }
00237                         }
00238                         
00239                 }
00240                 return res;
00241         }
00242         
00243         
00251                 class LookupBarcodeCallback implements ServiceServer.Callback<LookupBarcode.Request, LookupBarcode.Response> {
00252                         
00253                         @Override
00254                         public LookupBarcode.Response call(LookupBarcode.Request req) {
00255 
00256                                 LookupBarcode.Response res = new LookupBarcode.Response();
00257                                 res.objectclass="";
00258 
00259                                 if (req.ean != null) {
00260                                         
00261                                         if(! ((req.ean.length()==8 ) || (req.ean.length()==13 ))) {
00262                                                 ros.logError("LookupBarcode: malformed EAN (not 8 or 13 digits)");
00263                                         }
00264                                         
00265                                         res.objectclass = BarcodeWebLookup.lookUpUpcDatabase(req.ean);
00266                                         
00267                                 }
00268                                 return res;
00269                                 
00270                         }
00271                 }
00272         
00273         
00274         
00275         public static void main(String[] args) {
00276         
00277                 // test client: lookup all EANs in the array eans               
00278                 //String[] eans = new String[] {"4006144617636", "40084015", "87303810", "4305399070501", "4305399060052", "4004764841561"};
00279                 //
00280                 //for(String ean : eans)
00281                 //      System.out.println(translate(BarcodeWebLookup.lookUpUpcDatabase(ean)));
00282                 
00283                 // default: launch the ROS node
00284                 new BarcodeWebLookup();
00285                 
00286         }
00287 
00288 }


comp_cop
Author(s): Moritz Tenorth
autogenerated on Sat Dec 28 2013 17:10:07