2013-11-29 08:00:41 +01:00
|
|
|
from util import hook, http, text, web
|
|
|
|
import json
|
|
|
|
|
|
|
|
ITEM_URL = "http://www.newegg.com/Product/Product.aspx?Item={}"
|
|
|
|
|
2013-11-29 08:40:18 +01:00
|
|
|
|
2013-11-29 08:00:41 +01:00
|
|
|
@hook.command
|
|
|
|
def newegg(inp):
|
2013-11-29 08:40:18 +01:00
|
|
|
"""newegg <item name> -- Searches newegg.com for <item name>"""
|
2013-11-29 08:00:41 +01:00
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# form the search request
|
2013-11-29 08:00:41 +01:00
|
|
|
request = {
|
2013-11-29 08:40:18 +01:00
|
|
|
"PageNumber": 1,
|
|
|
|
"BrandId": -1,
|
|
|
|
"NValue": "",
|
|
|
|
"StoreDepaId": -1,
|
|
|
|
"NodeId": -1,
|
|
|
|
"Keyword": inp,
|
|
|
|
"IsSubCategorySearch": False,
|
|
|
|
"SubCategoryId": -1,
|
|
|
|
"Sort": "FEATURED",
|
|
|
|
"CategoryId": -1,
|
|
|
|
"IsUPCCodeSearch": False
|
2013-11-29 08:00:41 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# submit the search request
|
2013-11-29 08:00:41 +01:00
|
|
|
r = http.get_json(
|
|
|
|
'http://www.ows.newegg.com/Search.egg/Advanced',
|
|
|
|
post_data = json.dumps(request)
|
|
|
|
)
|
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# get the first result
|
2013-11-29 08:00:41 +01:00
|
|
|
item = r["ProductListItems"][0]
|
|
|
|
|
2013-11-29 08:40:18 +01:00
|
|
|
title = text.truncate_str(item["Title"], 50)
|
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# format the rating nicely if it exists
|
2013-11-29 08:40:18 +01:00
|
|
|
if not item["ReviewSummary"]["TotalReviews"] == "[]":
|
|
|
|
rating = "Rated {}/5 ({} ratings)".format(item["ReviewSummary"]["Rating"],
|
|
|
|
item["ReviewSummary"]["TotalReviews"][1:-1])
|
|
|
|
else:
|
|
|
|
rating = "No Ratings"
|
2013-11-29 08:00:41 +01:00
|
|
|
|
|
|
|
if not item["FinalPrice"] == item["OriginalPrice"]:
|
2013-11-29 08:40:18 +01:00
|
|
|
price = "{FinalPrice}, was {OriginalPrice}".format(**item)
|
2013-11-29 08:00:41 +01:00
|
|
|
else:
|
2013-11-29 08:40:18 +01:00
|
|
|
price = item["FinalPrice"]
|
2013-11-29 08:00:41 +01:00
|
|
|
|
|
|
|
tags = []
|
|
|
|
|
|
|
|
if item["Instock"]:
|
|
|
|
tags.append("\x02Stock Available\x02")
|
|
|
|
else:
|
|
|
|
tags.append("\x02Out Of Stock\x02")
|
|
|
|
|
|
|
|
if item["FreeShippingFlag"]:
|
|
|
|
tags.append("\x02Free Shipping\x02")
|
|
|
|
|
|
|
|
if item["IsFeaturedItem"]:
|
|
|
|
tags.append("\x02Featured\x02")
|
|
|
|
|
2013-11-29 08:40:18 +01:00
|
|
|
if item["IsShellShockerItem"]:
|
|
|
|
tags.append("\x02SHELL SHOCKER®\x02")
|
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# join all the tags together in a comma seperated string ("tag1, tag2, tag3")
|
2013-11-29 08:40:18 +01:00
|
|
|
tag_text = u", ".join(tags)
|
2013-11-29 08:00:41 +01:00
|
|
|
|
2013-11-29 09:59:48 +01:00
|
|
|
# create the item URL and shorten it
|
2013-11-29 08:40:18 +01:00
|
|
|
url = web.try_isgd(ITEM_URL.format(item["NeweggItemNumber"]))
|
2013-11-29 08:00:41 +01:00
|
|
|
|
2013-11-29 08:40:18 +01:00
|
|
|
return u"\x02{}\x02 ({}) - {} - {} - {}".format(title, price, rating,
|
|
|
|
tag_text, url)
|