def get_random_products(limit=10): db = DBUtil() r = db.get_product_item_random(limit) # dx = [{"asin" : x[0], 'title': x[1], 'image_link': x[7]} for x in r] dx = [{"asin" : x[0], 'category' : x[1], 'price' : float(x[2]), 'brand' : x[3], 'title': x[4], 'image_link': x[5]} for x in r] db.close() return jsonify(dx)
def get_bpr_product_categories(reviewer_id): from random import randint d = { 'single' : [], 'multi' : [] } template = [3,3,1,3,4,1,1,1,1,1,1,1,1,4,4,1] if reviewer_id in bprmodel.keys(): asin_list = bprmodel[reviewer_id] db = DBUtil() r = db.breakdown_categories(asin_list[u'Recommendation list']) for x in r: if x[1] > 3: d['multi'].append(x[0]) else: d['single'].append(x[0]) db.close() categories = [] for x in template: if x == 1: categories.append( d['single'].pop(randint(0, len(d['single']) - 1)) ) else: categories.append( d['multi'].pop(randint(0, len(d['multi']) - 1)) ) dx = [{"category": x} for x in categories] return jsonify(dx)
def get_bpr_product_by_category(): if not request.json: return jsonify("error") # print request.json reviewer_id = request.json['reviewer_id'] category = request.json['category'] limit = request.json['limit'] if reviewer_id in bprmodel.keys(): asin_list = bprmodel[reviewer_id] # print category # print limit db = DBUtil() r = db.get_bpr_recommended_item(asin_list[u'Recommendation list'], category, limit) print r dx = [{ "asin": x[0], 'category': x[1], 'price': float(x[2]), 'brand': x[3], 'title': x[4], 'image_link': x[5] } for x in r] db.close() return jsonify(dx)
def get_bpr_recommendation(reviewer_id): dx = [] if reviewer_id in bprmodel.keys(): asin_list = bprmodel[reviewer_id] print asin_list[u'Recommendation list'] db = DBUtil() r = db.get_product_item(asin_list[u'Recommendation list']) dx = [{"asin": x[0], 'title': x[1], 'image_link': x[2], 'price' : float(x[3])} for x in r] db.close() return jsonify(dx)
def get_purchased_items(): if "user" not in session.keys(): return redirect("/", code=302) else: db = DBUtil() u = session['user'] asin_list = [x[1] for x in u] r = db.get_product_item(asin_list) dx = [{"asin": x[0], 'title': x[1], 'image_link': x[2], 'price' : float(x[3])} for x in r] db.close() return jsonify(dx)
def search(): if not request.json: return jsonify("error") print request.json keywords = request.json['field-keywords'] limit = request.json['limit'] # print category # print limit db = DBUtil() r = db.search(keywords, limit) dx = [{"asin" : x[0], 'category' : x[1], 'price' : float(x[2]), 'brand' : x[3], 'title': x[4], 'image_link': x[5]} for x in r] db.close() return jsonify(dx)
def get_random_product_by_category(): if not request.json: return jsonify("error") print request.json category = request.json['category'] limit = request.json['limit'] # print category # print limit db = DBUtil() r = db.get_random_product_by_category(category, limit) dx = [{"asin" : x[0], 'category' : x[1], 'price' : float(x[2]), 'brand' : x[3], 'title': x[4], 'image_link': x[5]} for x in r] db.close() return jsonify(dx)
def signin(): db = DBUtil() if request.method == 'POST': email = request.form['email'] print email session["email"] = email u = db.get_amazon_user(email) db.close() if len(u) > 0: print u session['user'] = u return redirect("purchase_history.html", code=302) else: print "no user" return redirect("/", code=302)
def get_random_category(limit=16): db = DBUtil() r = db.get_random_category(limit) dx = [{"category": x[0]} for x in r] db.close() return jsonify(dx)