Skip to content

Commit 4eb26fe

Browse files
authored
Merge pull request CloudBotIRC#93 from linuxdaemon/gonzobot+newegg-fix
Fix endpoints and API field names in newegg.py
2 parents b0a0292 + eef94a5 commit 4eb26fe

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

plugins/newegg.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
"""
1212

1313
import json
14-
import requests
1514
import re
1615

16+
import requests
17+
1718
from cloudbot import hook
1819
from cloudbot.util import formatting, web
1920

20-
2121
# CONSTANTS
2222

23-
ITEM_URL = "http://www.newegg.com/Product/Product.aspx?Item={}"
23+
ITEM_URL = "https://www.newegg.com/Product/Product.aspx?Item={}"
2424

25-
API_PRODUCT = "http://www.ows.newegg.com/Products.egg/{}/ProductDetails"
26-
API_SEARCH = "http://www.ows.newegg.com/Search.egg/Advanced"
25+
API_PRODUCT = "https://www.ows.newegg.com/Products.egg/{}"
26+
API_SEARCH = "https://www.ows.newegg.com/Search.egg/Advanced"
2727

2828
NEWEGG_RE = re.compile(r"(?:(?:www.newegg.com|newegg.com)/Product/Product\.aspx\?Item=)([-_a-zA-Z0-9]+)", re.I)
2929

@@ -37,11 +37,11 @@ def format_item(item, show_url=True):
3737
# format the rating nicely if it exists
3838
if not item["ReviewSummary"]["TotalReviews"] == "[]":
3939
rating = "Rated {}/5 ({} ratings)".format(item["ReviewSummary"]["Rating"],
40-
item["ReviewSummary"]["TotalReviews"][1:-1])
40+
item["ReviewSummary"]["TotalReviews"])
4141
else:
4242
rating = "No Ratings"
4343

44-
if not item["FinalPrice"] == item["OriginalPrice"]:
44+
if item["OriginalPrice"] and item["FinalPrice"] != item["OriginalPrice"]:
4545
price = "{FinalPrice}, was {OriginalPrice}".format(**item)
4646
else:
4747
price = item["FinalPrice"]
@@ -53,7 +53,7 @@ def format_item(item, show_url=True):
5353
else:
5454
tags.append("\x02Out Of Stock\x02")
5555

56-
if item["FreeShippingFlag"]:
56+
if item["IsFreeShipping"]:
5757
tags.append("\x02Free Shipping\x02")
5858

5959
if item.get("IsPremierItem"):
@@ -62,9 +62,6 @@ def format_item(item, show_url=True):
6262
if item["IsFeaturedItem"]:
6363
tags.append("\x02Featured\x02")
6464

65-
if item["IsShellShockerItem"]:
66-
tags.append("\x02SHELL SHOCKER\u00AE\x02")
67-
6865
# join all the tags together in a comma separated string ("tag1, tag2, tag3")
6966
tag_text = ", ".join(tags)
7067

@@ -92,12 +89,12 @@ def newegg_url(match):
9289
}
9390

9491
item = requests.get(API_PRODUCT.format(item_id), headers=headers).json()
95-
return format_item(item, show_url=False)
92+
return format_item(item['Basic'], show_url=False)
9693

9794

98-
@hook.command()
99-
def newegg(text):
100-
"""newegg <item name> - searches newegg.com for <item name>"""
95+
# @hook.command()
96+
def newegg(text, admin_log, reply):
97+
"""<item name> - searches newegg.com for <item name>"""
10198

10299
# form the search request
103100
request = {
@@ -124,8 +121,16 @@ def newegg(text):
124121

125122
r = request.json()
126123

127-
if r.get("Description", False):
128-
return "Newegg Error: {Description} (\x02{Code}\x02)". format(**r)
124+
if not request.ok:
125+
if r.get("Message"):
126+
msg = "{ExceptionMessage}\n{ExceptionType}\n{StackTrace}".format(**r).replace("\r", "")
127+
url = web.paste(msg)
128+
admin_log("Newegg API Error: {ExceptionType}: {url}".format(url=url, **r))
129+
return "Newegg Error: {Message} (\x02{code}\x02)".format(code=request.status_code, **r)
130+
else:
131+
reply("Unknown error occurred.")
132+
request.raise_for_status()
133+
return
129134

130135
# get the first result
131136
if r["ProductListItems"]:

0 commit comments

Comments
 (0)