Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 84e7593bc5
Fetching contributors…

Cannot retrieve contributors at this time

123 lines (103 sloc) 4.616 kB
<?xml version="1.0"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
<meta>
<author>Paul Tarjan</author>
</meta>
<bindings>
<select produces="XML" itemPath="result.item">
<urls>
<url>http://newegg.com</url>
</urls>
<inputs>
<key id="query" type="xs:string" paramType="query" required="true" />
<key id="order" type="xs:string" paramType="query" default="PRICE" />
<key id="subcategory" type="xs:string" paramType="query" default=""/>
<key id="category" type="xs:string" paramType="query" default=""/>
</inputs>
<execute><![CDATA[
var result = <result/>;
function fixItem(item) {
var price = parseFloat(item.price.toString().replace("$", "").replace(",", "").replace(/[ \t\r\n]*/g, ''));
item.price = <price>{price.toFixed(2)}</price>;
var shipping = item.shipping.toString();
if (! /.*Free Shipping.*/.test(shipping)) {
shipping = shipping.match(/\$[0-9,]\.[0-9]{2}/);
if (shipping) {
shipping = shipping.toString().replace("$", "").replace(",", "").replace(/[ \t\r\n]*/g, '');
shipping = parseFloat(shipping);
} else {
shipping = 0;
}
} else {
shipping = 0;
}
var itemCode = item.itemCode.toString();
var link = "";
if (itemCode) {
link = "http://www.newegg.com/Product/Product.aspx?Item=" + itemCode.toString() + "&nm_mc=AFC-C8Junction";
var affiliate = "http://www.kqzyfj.com/click-3775901-10440897?url=" + encodeURIComponent(link);
item.affiliateLink = <affiliateLink>{affiliate}</affiliateLink>;
link = "http://www.newegg.com/Product/Product.aspx?Item=" + itemCode.toString();
}
item.link = <link>{link}</link>;
item.shipping = <shipping>{shipping.toFixed(2)}</shipping>;
var totalPrice = price + shipping;
item.totalPrice = <totalPrice>{totalPrice.toFixed(2)}</totalPrice>;
return item;
}
var url = "http://www.newegg.com/Product/ProductList.aspx?Submit=ENE&Description=" + (query) + "&Order=" + order + "&Subcategory=" + subcategory + "&Category=" + category + "&Pagesize=100&Page=1";
var tries = 0;
while (tries < 4) {
var html = y.rest(url).accept("text/html").timeout(5000).get().response;
if (typeof(html) == "undefined")
tries ++;
else
break;
}
if (tries == 4)
y.exit();
fullpage = html;
// gcaProductCell == search page and synopsis == item
html = y.xpath(html, "//div[@id='bcaProductCell']//div[@class='itemCell'] | //*[@class='alert'] | //div[@id='synopsis']");
var good = true;
if (html.length() == 0) good = false;
if (html.(@['class'] == 'alert').length() != 0) {
good = false;
}
// For the single product page
var root = html.(@['id'] == "synopsis");
if (root.length() != 0) {
var item = <item/>;
item.name = <name>{root..div.(@['class']=="grpDesc boxConstraint")..h1[0].text().toString().replace(/\W+/g, ' ')}</name>;
var link = fullpage..link.(@rel=="canonical").@href.toString();
var itemCode = link.match(/Item=[^&]+/)[0].replace("Item=", "");
item.itemCode = <itemCode>{itemCode}</itemCode>
var priceNode = root..div.(@id=="singleFinalPrice").p;
item.price = <price>{priceNode.text() + priceNode.sup.text()}</price>;
item.shipping = <shipping>{root..div.(@id=="synShipping").p.text()}</shipping>;
item.image = <image>{root..img.(@id=="mainSlide_0").@src}</image>;
result.item += fixItem(item);
// For the product search page
} else if (good) {
for each (var node in html) {
var item = <item/>;
item.name = <name>{node..span.(@['class'] == "itemDescription").text().toString()}</name>;
var link = node..div.(@['class'] == "itemText").a.@href.text().toString();
var itemCode = link.match(/Item=[^&]+/)[0].replace("Item=", "");
item.itemCode = <itemCode>{itemCode}</itemCode>
var price = node..li.(@['class'] == "priceFinal");
price = price..strong.toString() + price..sup.toString();
item.price = <price>{price}</price>;
var shipping = node..span.(@['class'] == "priceShip").text().toString();
item.shipping = <shipping>{shipping}</shipping>;
item.image = <image>{node..a.img.(@width == 125).@src}</image>;
if (item.name.length() == 0 || item.itemCode.length() == 0) continue;
result.item += fixItem(item);
}
}
response.maxAge = 60 * 60 * 24; // 1 day cache
response.object = result;
]]></execute>
</select>
</bindings>
</table>
Jump to Line
Something went wrong with that request. Please try again.