def history(): """ ------------------------------------------------------- Checks history status of a user Use: history = Customer.history(conn, itemCode, userID) ------------------------------------------------------- Parameters: userId - a user ID number (int) Returns: history - the search history of the user (tuple) ------------------------------------------------------- """ userID = request.args['userID'] response = {'history': 'null'} error = False try: if userID != 'null': sql = "SELECT * FROM history WHERE userID = %s ORDER BY searchedDate DESC" conn.cursor.execute(sql, (userID, )) data = conn.cursor.fetchall() response['history'] = [] for item in data: response['history'].append({ 'itemName': item[3], 'searchedDate': item[2], 'itemID': item[1] }) except Exception as err: request['error'] = err error = True return sendResponse(response, error)
def displayData(): userID = request.args['userID'] response = {'result': 'null'} error = False try: #Displays all data in the saved items table, used for debugging. cursor.execute( "SELECT * FROM saved_table WHERE userID = '{}'".format(userID)) result = cursor.fetchall() response['result'] = [] for item in result: response['result'].append({ 'itemID': item[1], 'name': item[2], 'url': item[3], 'price': str(item[4]), 'company_name': item[5], 'image_url': item[6] }) except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def addHistory(): """ ------------------------------------------------------- add to user history ------------------------------------------------------- Parameters: itemName - item's name (int) userId - a user ID number (int) searchDate - search date (datetime) Returns: updatedHistory - user's new history (boolean) ------------------------------------------------------- """ userID = request.args['userID'] searchDate = request.args['searchDate'] itemName = request.args['itemName'] error = False response = {'update': 'null'} if userID != 'null': try: sql = "INSERT INTO history (userID, searchedDate, itemName) VALUES (%s, %s, %s)" conn.cursor.execute(sql, ( userID, searchDate, itemName, )) response['update'] = 'done' except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def api_all(): error = False response = {'result': 'null'} try: if 'search' in request.args: search = request.args['search'] else: response['error_2'] = "Error: No search field provided. Please search for products." link = "https://api.bestbuy.com/v1/products(search={})?format=json&show=name,salePrice,addToCartUrl,largeImage&apiKey=N0ReEPP28MPw3Gd2xSIAQ5dM".format(search) data = json.loads(urllib.request.urlopen(link).read().decode()) #Retrieve data from bestbuyAPI products = data['products'] for item in products: #Rename url and include retailer name to the items item['url'] = item.pop('addToCartUrl') item['retailer'] = 'BestBuy' #eBay Code api = Connection(config_file='ebay.yaml') #Connect to the API using authentication file res = api.execute('findItemsAdvanced', {'keywords': search}) #Retrieve data using eBay developer API items = res.reply.searchResult.item[:10] #Only use the first 10 results ebay_products = [] for item in items: ebay_product = {} name = item.get('title') #Retrieve name, price, image, link to purchase product sellingStatus = item.get('sellingStatus') currentPrice = sellingStatus.get('currentPrice') salePrice = float(currentPrice.get('value')) largeImage = item.get('galleryURL') url = item.get('viewItemURL') #Format dictionary to match the categories that were used for the Best Buy products ebay_product['name'] = name ebay_product['retailer'] = 'eBay' ebay_product['salePrice'] =salePrice ebay_product['largeImage'] = largeImage ebay_product['url'] = url ebay_products.append(ebay_product) products.extend(ebay_products) #Extend the dictionary to include the ebay products products = sorted(products, key = lambda i: i['salePrice']) #Sort by price response['result'] = products except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def cleanCart(): userID = request.args['userID'] response = {'result': 'null'} error = False try: #Emptys all saved items cursor.execute( "DELETE FROM saved_table WHERE userID = '{}'".format(userID)) response['result'] = 'done' except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def removeItem(): itemID = request.args['itemID'] response = {'result': 'null'} error = False try: cursor.execute( "DELETE FROM saved_table WHERE itemID = '{}'".format(itemID)) count = cursor.rowcount response['result'] = 'done' except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def addNewItem(): company_name = request.args['company_name'] name = request.args['name'] userID = request.args['userID'] url = request.args['url'] image_url = request.args['image_url'] price = request.args['price'] error = False response = {'result': 'null'} #Adds a saved item sql = 'INSERT INTO saved_table (userID, name, url, price, company_name,image_url) VALUES (%s, %s, %s, %s, %s, %s)' val = (userID, name, url, price, company_name, image_url) try: cursor.execute(sql, val) response['result'] = 'done' except Exception as err: response['error'] = str(err) error = True return sendResponse(response, error)
def endPointNotFound(error): return sendResponse("Endpoint not found", True)
def main(): return sendResponse("Server is running", False)