This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/newegg.py

96 lines
2.6 KiB
Python
Raw Permalink Normal View History

2013-11-29 08:00:41 +01:00
import json
2013-11-30 04:37:01 +01:00
import re
2013-11-29 08:00:41 +01:00
2014-02-14 04:36:57 +01:00
from util import hook, http, text, web
2013-12-11 05:31:57 +01:00
## CONSTANTS
2013-11-29 08:00:41 +01:00
ITEM_URL = "http://www.newegg.com/Product/Product.aspx?Item={}"
2013-11-30 04:37:01 +01:00
API_PRODUCT = "http://www.ows.newegg.com/Products.egg/{}/ProductDetails"
API_SEARCH = "http://www.ows.newegg.com/Search.egg/Advanced"
2013-11-29 08:40:18 +01:00
2013-11-30 04:37:01 +01:00
NEWEGG_RE = (r"(?:(?:www.newegg.com|newegg.com)/Product/Product\.aspx\?Item=)([-_a-zA-Z0-9]+)", re.I)
2013-11-29 08:00:41 +01:00
2013-12-11 05:31:57 +01:00
## OTHER FUNCTIONS
2013-12-11 05:11:34 +01:00
def format_item(item, show_url=True):
2013-11-30 04:37:01 +01:00
""" takes a newegg API item object and returns a description """
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"],
2014-02-13 02:34:50 +01:00
item["ReviewSummary"]["TotalReviews"][1:-1])
2013-11-29 08:40:18 +01:00
else:
rating = "No Ratings"
2013-11-29 08:00:41 +01:00
2014-02-13 02:34:50 +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"]:
2014-02-13 12:47:01 +01:00
tags.append(u"\x02SHELL SHOCKER\u00AE\x02")
2013-11-29 08:40:18 +01:00
2014-02-13 02:34:50 +01:00
# join all the tags together in a comma separated 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-12-11 05:11:34 +01:00
if show_url:
# create the item URL and shorten it
url = web.try_isgd(ITEM_URL.format(item["NeweggItemNumber"]))
return u"\x02{}\x02 ({}) - {} - {} - {}".format(title, price, rating,
2014-02-13 02:34:50 +01:00
tag_text, url)
2013-12-11 05:11:34 +01:00
else:
return u"\x02{}\x02 ({}) - {} - {}".format(title, price, rating,
2014-02-13 02:34:50 +01:00
tag_text)
2013-11-30 04:37:01 +01:00
2013-12-02 11:10:08 +01:00
## HOOK FUNCTIONS
2013-11-30 04:37:01 +01:00
@hook.regex(*NEWEGG_RE)
def newegg_url(match):
item_id = match.group(1)
item = http.get_json(API_PRODUCT.format(item_id))
2013-12-11 05:11:34 +01:00
return format_item(item, show_url=False)
2013-11-30 04:37:01 +01:00
@hook.command
def newegg(inp):
"""newegg <item name> -- Searches newegg.com for <item name>"""
# form the search request
request = {
"Keyword": inp,
2013-12-02 11:10:08 +01:00
"Sort": "FEATURED"
2013-11-30 04:37:01 +01:00
}
# submit the search request
r = http.get_json(
2014-02-13 02:34:50 +01:00
'http://www.ows.newegg.com/Search.egg/Advanced',
post_data=json.dumps(request)
2013-11-30 04:37:01 +01:00
)
# get the first result
2013-11-30 05:40:09 +01:00
if r["ProductListItems"]:
2014-02-11 05:02:52 +01:00
return format_item(r["ProductListItems"][0])
2013-11-30 05:40:09 +01:00
else:
return "No results found."
2013-11-30 04:37:01 +01:00