Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/sales.git
src/main/java/com/whitemagicsoftware/sales/Flyer.java
+/*
+ * The MIT License
+ *
+ * Copyright 2016 White Magic Software, Ltd..
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+package com.whitemagicsoftware.sales;
+
+import java.util.Date;
+
+/**
+ *
+ * @author White Magic Software, Ltd.
+ */
+public class Flyer extends BusinessEntity {
+
+ private Date expiry;
+
+ private Flyer() {
+ }
+
+ protected void setExpiry( Date expiry ) {
+ this.expiry = expiry;
+ }
+
+ public static final class Builder extends BusinessEntity.Builder<Flyer, Builder> {
+
+ @Override
+ protected Flyer createObject() {
+ return new Flyer();
+ }
+
+ @Override
+ protected Builder getBuilder() {
+ return this;
+ }
+
+ public Builder withExpiry( Date expiry ) {
+ getObject().setExpiry( expiry );
+ return getBuilder();
+ }
+ }
+}
src/main/java/com/whitemagicsoftware/sales/Main.java
import org.webharvest.definition.ScraperConfiguration;
import org.webharvest.runtime.Scraper;
+import org.webharvest.runtime.variables.Variable;
import org.xml.sax.InputSource;
private void run() throws IOException {
- List<Vendor> vendors = getVendors();
- List<Subscriber> subscribers = getSubscribers();
+ final List<Vendor> vendors = getVendors();
+ final List<Subscriber> subscribers = getSubscribers();
+ Product product = new Product.Builder()
+ .withName( "Sunflower Seeds" )
+ .withUrlPath( "sunflower-seeds/00000_000000000000005091" )
+ .build();
+
Scraper scraper = getScraper( "com_thriftyfoods.xml" );
+
+ scraper.addVariableToContext( "product", product );
scraper.execute();
+
+ Variable price = scraper.getContext().getVar( "price" );
+// Variable content = scraper.getContext().getVar( "content" );
+
+ System.out.println( "Content = " + price );
// Jarvest jarvest = new Jarvest();
src/main/java/com/whitemagicsoftware/sales/Product.java
package com.whitemagicsoftware.sales;
-import java.util.Date;
-
/**
* @author White Magic Software, Ltd.
*/
public class Product extends BusinessEntity {
+
private Price sale;
private Price regular;
-
- private Date expiry;
- protected Product() {
+ /**
+ * Path to the product page for products that have their own web page.
+ */
+ private String urlPath;
+
+ private Product() {
}
/**
* Answers whether the product is on sale.
- *
+ *
* @return true The product is offering its sale price.
*/
public boolean isSale() {
return sale != null;
+ }
+
+ /**
+ * Establishes the sale price for the product. Once this is set to a non-null
+ * value, the isSale method will return true to indicate that this product
+ * is on sale.
+ *
+ * @param price
+ */
+ public void setSalePrice( Price price ) {
+ this.sale = price;
+ }
+
+ /**
+ * Establishes the regular price for the product. The sale price can be
+ * set without a regular price and vice-versa. A sale price might be found
+ * in a flyer, for example, where the regular price is not known.
+ *
+ * @param price
+ */
+ public void setRegularPrice( Price price ) {
+ this.regular = price;
+ }
+
+ /**
+ * Sets the web page to the product for a vendor's web site.
+ *
+ * @param urlPath The unique path component to the product URL.
+ */
+ private void setUrlPath( String urlPath ) {
+ this.urlPath = urlPath;
+ }
+
+ public static final class Builder extends BusinessEntity.Builder<Product, Builder> {
+
+ @Override
+ protected Product createObject() {
+ return new Product();
+ }
+
+ @Override
+ protected Builder getBuilder() {
+ return this;
+ }
+
+ public Builder withUrlPath( String urlPath ) {
+ getObject().setUrlPath( urlPath );
+ return getBuilder();
+ }
+
+ public Builder withName( String name ) {
+ getObject().setEntityName( new EntityName( name ) );
+ return getBuilder();
+ }
}
}
src/main/java/com/whitemagicsoftware/sales/Vendor.java
package com.whitemagicsoftware.sales;
+import java.net.URL;
+
/**
* Represents a company that sells products.
*
* @author White Magic Software, Ltd.
*/
public class Vendor extends BusinessEntity {
-
- /** Vendor web site. */
- private String webSite;
+
+ /**
+ * Vendor web site.
+ */
+ private URL webSite;
public Vendor() {
src/main/resources/com_thriftyfoods.xml
<?xml version="1.0" encoding="UTF-8"?>
-<config scriptlang="Javascript" charset="UTF-8">
-<!--
- xpath( '(//span[@class="price" and @itemprop="price"])[last()]' )
--->
+<config charset="UTF-8">
+
+ <exit condition='${!sys.isVariableDefined("product")}' message="Provide a product." />
+
+ <var-def name="vendor">https://www.thriftyfoods.com/product/sunflower-seeds/00000_000000000000005091</var-def>
+
+ <var-def name="price">
+ <xpath expression="(//span[@class='price' and @itemprop='price'])[last()]">
+ <html-to-xml>
+ <http url="${vendor}" />
+ </html-to-xml>
+ </xpath>
+ </var-def>
+
+ <!--
+ xpath( '(//span[@class="price" and @itemprop="price"])[last()]' )
+ -->
</config>

Obtain price from Thrifty Food web page.

Author djarvis <email>
Date 2016-06-09 20:46:24 GMT-0700
Commit eeed0b779fe63993a12d70b39d457483fadbacbe
Parent b231cc8
Delta 161 lines added, 15 lines removed, 146-line increase