Back to project page WineDB.
The source code is released under:
MIT License
If you think the Android project WineDB listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.selesse.android.winedb.winescraper.impl; /* w ww .jav a2s . com*/ import android.util.Log; import com.google.common.collect.Lists; import com.google.common.io.CharStreams; import com.selesse.android.winedb.database.Wine; import com.selesse.android.winedb.winescraper.WineScraper; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.List; public class UPCDatabaseWineScraper implements WineScraper { private static final String TAG = UPCDatabaseWineScraper.class.getSimpleName(); private String url; private List<Exception> errors; private String barcode; public UPCDatabaseWineScraper(String barcode) { this.barcode = barcode; url = "http://www.upcdatabase.com/item/" + barcode; errors = Lists.newArrayList(); } @Override public List<Wine> scrape() { List<Wine> scrapedWines = Lists.newArrayList(); try { URL url = new URL(getQueryUrl()); URLConnection connection = url.openConnection(); String rawHtml = CharStreams.toString(new InputStreamReader(connection.getInputStream())); Document document = Jsoup.parse(rawHtml); Elements table = document.select("table.data"); // if we don't have any table data, we didn't find the wine in the UPC database if (table.size() == 0) { errors.add(new Exception("Wine was not found!")); return scrapedWines; } Wine wine = new Wine(); wine.setBarcode(barcode); Elements rows = table.select("tr"); // the rows in the result have 3 <td>s: 1st is key, 3rd is value for (Element tr : rows) { Elements tds = tr.select("td"); for (int i = 0; i < tds.size(); i++) { Element td = tds.get(i); // set the name to the description, it is usually short enough to fit in name if (td.text().contains("Description")) { wine.setName(tds.get(i + 2).text()); } else if (td.text().contains("Country")) { wine.setCountry(tds.get(i + 2).text()); } } } scrapedWines.add(wine); } catch (IOException e) { Log.e(TAG, "UPCDatabase.org threw exception : \n" + e); errors.add(e); } return scrapedWines; } @Override public String getQueryUrl() { return url; } @Override public List<Exception> getErrors() { return errors; } @Override public String getSourceName() { return "UPCDatabase"; } }