]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/result/supplement/ProductResultInfoRetriever.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / result / supplement / ProductResultInfoRetriever.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner.result.supplement;
18
19 import android.content.Context;
20 import android.text.Html;
21 import android.widget.TextView;
22
23 import cz.cvut.fel.dce.barcodescanner.HttpHelper;
24 import cz.cvut.fel.dce.barcodescanner.LocaleManager;
25 import cz.cvut.fel.dce.barcodescanner.R;
26 import cz.cvut.fel.dce.barcodescanner.history.HistoryManager;
27
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 /**
34  * <p>Retrieves product information from Google Product search.</p>
35  *
36  * <p><strong>Please do not reuse this code.</strong> Using results in this way requires permission
37  * from Google, and that is not granted to users via this project.</p>
38  *
39  * @author Sean Owen
40  */
41 final class ProductResultInfoRetriever extends SupplementalInfoRetriever {
42
43   private static final Pattern[] PRODUCT_NAME_PRICE_PATTERNS = {
44     Pattern.compile(",event\\)\">([^<]+)</a></h3>.+<span class=psrp>([^<]+)</span>"),
45     Pattern.compile("owb63p\">([^<]+).+zdi3pb\">([^<]+)"),
46   };
47
48   private final String productID;
49   private final String source;
50   private final Context context;
51
52   ProductResultInfoRetriever(TextView textView, String productID, HistoryManager historyManager, Context context) {
53     super(textView, historyManager);
54     this.productID = productID;
55     this.source = context.getString(R.string.msg_google_product);
56     this.context = context;
57   }
58
59   @Override
60   void retrieveSupplementalInfo() throws IOException {
61
62     String encodedProductID = URLEncoder.encode(productID, "UTF-8");
63     String uri = "https://www.google." + LocaleManager.getProductSearchCountryTLD(context)
64             + "/m/products?ie=utf8&oe=utf8&scoring=p&source=zxing&q=" + encodedProductID;
65     CharSequence content = HttpHelper.downloadViaHttp(uri, HttpHelper.ContentType.HTML);
66
67     for (Pattern p : PRODUCT_NAME_PRICE_PATTERNS) {
68       Matcher matcher = p.matcher(content);
69       if (matcher.find()) {
70         append(productID,
71                source,
72                new String[] { unescapeHTML(matcher.group(1)), unescapeHTML(matcher.group(2)) },
73                uri);
74         break;
75       }
76     }
77   }
78
79   private static String unescapeHTML(String raw) {
80     return Html.fromHtml(raw).toString();
81   }
82
83 }