2 * Copyright (C) 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package cz.cvut.fel.dce.barcodescanner.result.supplement;
19 import android.content.Context;
20 import android.text.Html;
21 import android.widget.TextView;
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;
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
34 * <p>Retrieves product information from Google Product search.</p>
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>
41 final class ProductResultInfoRetriever extends SupplementalInfoRetriever {
43 private static final Pattern[] PRODUCT_NAME_PRICE_PATTERNS = {
44 Pattern.compile(",event\\)\">([^<]+)</a></h3>.+<span class=psrp>([^<]+)</span>"),
45 Pattern.compile("owb63p\">([^<]+).+zdi3pb\">([^<]+)"),
48 private final String productID;
49 private final String source;
50 private final Context context;
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;
60 void retrieveSupplementalInfo() throws IOException {
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);
67 for (Pattern p : PRODUCT_NAME_PRICE_PATTERNS) {
68 Matcher matcher = p.matcher(content);
72 new String[] { unescapeHTML(matcher.group(1)), unescapeHTML(matcher.group(2)) },
79 private static String unescapeHTML(String raw) {
80 return Html.fromHtml(raw).toString();