コード例 #1
0
ファイル: main.py プロジェクト: gegbo/lowest_current_deals
    def get(self):

        user = users.get_current_user()
        person_id = user.user_id()

        store_type = self.request.get('type')
        product_id = self.request.get('id')

        item_to_add = None

        if store_type.lower() == "walmart":

            logging.info('True for walmart')
            walmart_url=("http://api.walmartlabs.com/v1/items/%s?format=json&apiKey=cz9kfm3vuhssnk6hn33zg86k" %product_id)
            walmart_JSON_string=json.load(urllib2.urlopen(walmart_url))

            walmart_image_source="<img src=%s>" %walmart_JSON_string["thumbnailImage"]

            walmart_name=walmart_JSON_string["name"]

            sales_Price = str(walmart_JSON_string["salePrice"])

            walmart_link_to_buy = walmart_JSON_string["productUrl"]

            walmart_link_to_remove = "/remove?id=%s" %product_id

            walmart_upc = str(walmart_JSON_string["upc"])

            walmart_compare_upc = "/compare?upc=%s" %str(walmart_JSON_string["upc"])


            check_product=WishList.query(WishList.user_id==person_id,WishList.item_id==product_id).get()

            if check_product == None:

                walmart = WishList(user_id = person_id, store = store_type, item_id = product_id,name = walmart_name, price = sales_Price, image = walmart_image_source, url = walmart_link_to_buy,removeUrl=walmart_link_to_remove,upc_id=walmart_upc)
                walmart = WishList(user_id = person_id, store = store_type, item_id = product_id,name = walmart_name, price = sales_Price, image = walmart_image_source, url = walmart_link_to_buy,removeUrl=walmart_link_to_remove,compare_url=walmart_compare_upc)

                walmart.put()
                item_to_add = walmart


        if store_type.lower() == "bestbuy":

            logging.info('True for bestbuy')
            bestbuy_url=("http://api.remix.bestbuy.com/v1/products/%s.json?show=sku,name,salePrice,url,image,upc&apiKey=24ta6vtsr78a22fmv8ngfjet" %product_id)
            bestbuy_JSON_string=json.load(urllib2.urlopen(bestbuy_url))

            bestbuy_image_source="<img src=%s>" %bestbuy_JSON_string["image"]

            bestbuy_name=bestbuy_JSON_string["name"]

            bestbuy_Price = str(bestbuy_JSON_string["salePrice"])

            bestbuy_link_to_buy = bestbuy_JSON_string["url"]

            bestbuy_link_to_remove = "/remove?id=%s" %product_id

            bestbuy_upc = str(bestbuy_JSON_string["upc"])

            check_product=WishList.query(WishList.user_id==person_id,WishList.item_id==product_id).get()
            if check_product == None:
                bestbuy = WishList(user_id = person_id, store = store_type, item_id = product_id,name = bestbuy_name, price = bestbuy_Price, image = bestbuy_image_source, url = bestbuy_link_to_buy,removeUrl=bestbuy_link_to_remove,upc_id = bestbuy_upc,bestbuy_compare_upc = "/compare?upc=%s" %str(bestbuy_JSON_string["upc"]))

            check_product=WishList.query(WishList.user_id==person_id,WishList.item_id==product_id).get()
            if check_product == None:
                bestbuy = WishList(user_id = person_id, store = store_type, item_id = product_id,name = bestbuy_name, price = bestbuy_Price, image = bestbuy_image_source, url = bestbuy_link_to_buy,removeUrl=bestbuy_link_to_remove,compare_url = bestbuy_compare_upc)

                bestbuy.put()
                item_to_add = bestbuy

        if store_type.lower() == "amazon":
            amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG) #initiates a new Amazon API
            product = amazon.lookup(ItemId='%s' %product_id)

            amazon_image_source="<img src=%s>" %product.medium_image_url

            amazon_name=product.title

            amazon_Price = str(product.price_and_currency[0])

            amazon_link_to_buy = product.offer_url

            amazon_link_to_remove = "/remove?id=%s" %product_id

            amazon_upc = str(product.upc)

            check_product=WishList.query(WishList.user_id==person_id,WishList.item_id==product_id).get()
            if check_product == None:
                amazon = WishList(user_id = person_id, store = store_type, item_id = product_id,name = amazon_name, price = amazon_Price, image = amazon_image_source, url = amazon_link_to_buy,removeUrl=amazon_link_to_remove,upc_id = amazon_upc)

            amazon_Price = str(product.price_and_currency[0])

            amazon_link_to_buy = product.offer_url

            amazon_link_to_remove = "/remove?id=%s" %product_id

            amazon_compare_upc = "/compare?upc=%s" %str(product.upc)

            check_product=WishList.query(WishList.user_id==person_id,WishList.item_id==product_id).get()
            if check_product == None:
                amazon = WishList(user_id = person_id, store = store_type, item_id = product_id,name = amazon_name, price = amazon_Price, image = amazon_image_source, url = amazon_link_to_buy,removeUrl=amazon_link_to_remove,compare_url = amazon_compare_upc)

                amazon.put()
                item_to_add = amazon

        template=jinja_environment.get_template('/templates/wishlist.html')
        wishlist_items = WishList.query(WishList.user_id==person_id).fetch()
        if item_to_add:
            wishlist_items.append(item_to_add)

        for item in wishlist_items:
            i = wishlist_items.index(item)
            if str(item.price)[len(str(item.price))-2] == '.':
                item.price = str(item.price) + '0'
                wishlist_items[i] = item

        self.response.write(template.render({'wishlist' : wishlist_items,  'user': users.get_current_user(),  'logout' : users.create_logout_url('/'), 'login' : users.create_login_url('/')}))