2013-11-29 20:00:41 +13:00
import json
2013-11-30 16:37:01 +13:00
import re
2013-11-29 20:00:41 +13:00
2014-02-14 16:36:57 +13:00
from util import hook , http , text , web
2013-12-11 17:31:57 +13:00
## CONSTANTS
2013-11-29 20:00:41 +13:00
ITEM_URL = " http://www.newegg.com/Product/Product.aspx?Item= {} "
2013-11-30 16:37:01 +13:00
API_PRODUCT = " http://www.ows.newegg.com/Products.egg/ {} /ProductDetails "
API_SEARCH = " http://www.ows.newegg.com/Search.egg/Advanced "
2013-11-29 20:40:18 +13:00
2013-11-30 16:37:01 +13:00
NEWEGG_RE = ( r " (?:(?:www.newegg.com|newegg.com)/Product/Product \ .aspx \ ?Item=)([-_a-zA-Z0-9]+) " , re . I )
2013-11-29 20:00:41 +13:00
2013-12-11 17:31:57 +13:00
## OTHER FUNCTIONS
2013-12-11 17:11:34 +13:00
def format_item ( item , show_url = True ) :
2013-11-30 16:37:01 +13:00
""" takes a newegg API item object and returns a description """
2013-11-29 20:40:18 +13:00
title = text . truncate_str ( item [ " Title " ] , 50 )
2013-11-29 21:59:48 +13:00
# format the rating nicely if it exists
2013-11-29 20:40:18 +13:00
if not item [ " ReviewSummary " ] [ " TotalReviews " ] == " [] " :
rating = " Rated {} /5 ( {} ratings) " . format ( item [ " ReviewSummary " ] [ " Rating " ] ,
2014-02-13 14:34:50 +13:00
item [ " ReviewSummary " ] [ " TotalReviews " ] [ 1 : - 1 ] )
2013-11-29 20:40:18 +13:00
else :
rating = " No Ratings "
2013-11-29 20:00:41 +13:00
2014-02-13 14:34:50 +13:00
if not item [ " FinalPrice " ] == item [ " OriginalPrice " ] :
2013-11-29 20:40:18 +13:00
price = " {FinalPrice} , was {OriginalPrice} " . format ( * * item )
2013-11-29 20:00:41 +13:00
else :
2013-11-29 20:40:18 +13:00
price = item [ " FinalPrice " ]
2013-11-29 20:00:41 +13:00
tags = [ ]
if item [ " Instock " ] :
tags . append ( " \x02 Stock Available \x02 " )
else :
tags . append ( " \x02 Out Of Stock \x02 " )
if item [ " FreeShippingFlag " ] :
tags . append ( " \x02 Free Shipping \x02 " )
if item [ " IsFeaturedItem " ] :
tags . append ( " \x02 Featured \x02 " )
2013-11-29 20:40:18 +13:00
if item [ " IsShellShockerItem " ] :
2014-02-14 00:47:01 +13:00
tags . append ( u " \x02 SHELL SHOCKER \u00AE \x02 " )
2013-11-29 20:40:18 +13:00
2014-02-13 14:34:50 +13:00
# join all the tags together in a comma separated string ("tag1, tag2, tag3")
2013-11-29 20:40:18 +13:00
tag_text = u " , " . join ( tags )
2013-11-29 20:00:41 +13:00
2013-12-11 17:11:34 +13: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 14:34:50 +13:00
tag_text , url )
2013-12-11 17:11:34 +13:00
else :
return u " \x02 {} \x02 ( {} ) - {} - {} " . format ( title , price , rating ,
2014-02-13 14:34:50 +13:00
tag_text )
2013-11-30 16:37:01 +13:00
2013-12-02 23:10:08 +13:00
## HOOK FUNCTIONS
2013-11-30 16:37:01 +13: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 17:11:34 +13:00
return format_item ( item , show_url = False )
2013-11-30 16:37:01 +13: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 23:10:08 +13:00
" Sort " : " FEATURED "
2013-11-30 16:37:01 +13:00
}
# submit the search request
r = http . get_json (
2014-02-13 14:34:50 +13:00
' http://www.ows.newegg.com/Search.egg/Advanced ' ,
post_data = json . dumps ( request )
2013-11-30 16:37:01 +13:00
)
# get the first result
2013-11-30 17:40:09 +13:00
if r [ " ProductListItems " ] :
2014-02-11 17:02:52 +13:00
return format_item ( r [ " ProductListItems " ] [ 0 ] )
2013-11-30 17:40:09 +13:00
else :
return " No results found. "
2013-11-30 16:37:01 +13:00