def add_entry(): if not session.get('logged_in'): abort(401) url = request.form['url'] # Add product to DB existing = Product.query.filter(Product.url == url).first() e = None product = None if not existing: #Query Amazon try: product = azcrawler.crawl_url(url) if product is None: flash("Amazon doesn't sell this product today!") return redirect(url_for('show_entries')) e = Product(product[0], url , product[2], float(product[1][0])) e.prev_price = 0 db = get_db() db.session.add(e) db.session.commit() except: flash("Error fetching product...") return redirect(url_for('show_entries')) existing = existing or e # Add Subscription s = Subscription.query.filter(Subscription.user_id == session['userid'], Subscription.prod_id == existing.id).first() if not s: try: print "Adding subscription" sub = Subscription(existing.id, session['userid']) db = get_db() db.session.add(sub) updt = DinoUpdate(json.dumps( {"title": existing.title, "message": "Watching currently at $%s"%(existing.curr_price), "url": existing.url, "img": existing.img_url, "merchant": utils.get_merchant_name(existing.url), "mag" : 0, "userid": session['userid']}), session['userid'], existing.id) print "Adding subscription %s"% updt db.session.add(updt) db.session.commit() except Exception, e: print "DB Tx failed: %s"%str(e)
def push_updates(self): print "Updating..." from init import pushr subs = Subscription.query.all() print "updating 1" print "updating 2" for s in subs: up = None if s.prod_id in self.updated_prods: up = self.getupdate(s.prod_id) if up: #Push to Updates db m = utils.get_merchant_name(up[0].url) if up[1] > 0: msg = "Price increase! - $%.2f"%up[0].curr_price elif up[1] < 0: msg = 'Price decrease alert! - $%.2f'%up[0].curr_price print "Pushing update with msg ", msg try: update = { 'title': up[0].title, 'message': msg, 'url': up[0].url, 'img': up[0].img_url, 'merchant': m, 'mag': up[1], 'userid': s.user_id } exist_up = DinoUpdate.query.filter(DinoUpdate.update==json.dumps(update), DinoUpdate.prod_id==s.prod_id).first() except Exception, e: print str(e) if exist_up: continue try: u = DinoUpdate(json.dumps(update), s.user_id, up[0].id) db.session.add(u) db.session.commit() except Exception, e: print "Db TX failed", str(e) return print "Update ", update usernickname = User.query.filter(User.id == s.user_id).first().nickname channel = config['pusher']['channelpfx'] + usernickname pushr[channel].trigger('priceup', update)