00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
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
00107 if(responseBody.contains("Item Not Found"))
00108 return null;
00109 else if(responseBody.contains("Item Record")) {
00110
00111
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
00154 public static String getUPCText(String upc) {
00155 String text = "";
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 return text;
00170 }
00171
00172
00173
00174
00175
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
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
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
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
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
00278
00279
00280
00281
00282
00283
00284 new BarcodeWebLookup();
00285
00286 }
00287
00288 }