Exemplo n.º 1
0
def create_product():
    if not request.get_json(force=True) or not 'asin' in request.get_json(force=True):
        abort(400)
    prod_id = request.get_json(force=True)['asin']
    cat_id = request.get_json(force=True)['cid']
    pname = request.get_json(force=True)['title']
    url = request.get_json(force=True)['imurl']
    price = request.get_json(force=True)['price']
    rank = request.get_json(force=True)['rank']
    if len(prod_id) == 0:
        abort(404)
    # need to check if product is in the product table, if not create it, else throw error 
    if data.existProduct(prod_id):
        abort(405)
    product = {'pid': prod_id, 'cid': cat_id, 'pname': pname, 'imurl': url, 'price': price, 'rank': rank}
    data.insertProductFeatures(product)
    reviews = request.get_json(force=True)['reviews']
    for r in reviews:
      ret = requests.post(sa_url, data=json.dumps({'review': r, 'product': product}))
      review = json.loads(ret.text)
      if data.existReview(prod_id, review['rid']):
          abort(405)
      # review with that prod_id and reviewer_id should not exist, but if they
      # do throw error
      print(review)    
    data.commit()
    (uneval_reviews, names) = data.getUnevalReviews(prod_id)
    print('found some uneval reviews')
    if uneval_reviews is not None:
      print(json.dumps({'reviews': uneval_reviews, 'names': names}))
      r = requests.post(ml_url, data=json.dumps({'reviews': uneval_reviews, 'names': names}))
      result = json.loads(r.text)['result']
      data.updateResult(result)
    product = data.getProductInfo(prod_id)
    return jsonify({'product': product[0]})
Exemplo n.º 2
0
def get_product(prod_id):
    print "here?"
    (uneval_reviews, names) = data.getUnevalReviews(prod_id)
    if uneval_reviews is not None:
      print('found some uneval reviews')
      print(json.dumps({'reviews': uneval_reviews, 'names': names}))
      r = requests.post(ml_url, data=json.dumps({'reviews': uneval_reviews, 'names': names}))
      result = json.loads(r.text)['result']
      data.updateResult(result)

    product = data.getProductInfo(prod_id)
    if len(product) == 0:
        abort(404)

    return jsonify({'product': product[0]})