def track_product(self, vald): username = vald['username'] product_id = vald['product_id'] try: res = self.pub.call({ 'method': 'get', 'resource': 'user', 'where': { 'username': username } }) user = unserialize(res['result']) res = self.pub.call({ 'method': 'get', 'resource': 'product', 'where': { 'id': product_id } }) product = unserialize(res['result']) except Exception as e: return {'message': str(e)} tracked = Tracked(wishlist=vald['wishlist']) tracked.product = product user.products.append(tracked) res = self.pub.call({'method': 'save', 'resource': serialize(user)}) if not res.get('success'): self.logger.info(res['message']) return res
def call(self, data_dict): data_json = json.dumps(data_dict) data_dict_formatted = data_dict.copy() for key, value in data_dict_formatted.items(): try: data_dict_formatted[key] = unserialize(value) except: pass print("Requesting {}".format(reprlib.repr(data_dict_formatted))) self.response = None self.corr_id = str(uuid.uuid4()) self.channel.basic_publish( exchange=self.exchange, routing_key=self.queue, properties=pika.BasicProperties( reply_to = self.callback_queue, correlation_id = self.corr_id, content_type = 'application/json' ), body=data_json ) while self.response is None: self.connection.process_data_events() response_formatted = self.response.copy() for key, value in response_formatted.items(): try: response_formatted[key] = unserialize(value) except Exception: pass print("Got {}".format(reprlib.repr(response_formatted))) return self.response
def get_price_changes(self, *args): ''' res = self.pub.call({'method': 'get', 'resource': 'product', 'where': {}}) products = unserialize(res['result']) price_changed = [] minus_one_week = str(datetime.now() - timedelta(weeks=1)) for product in products: if len(product.prices) > 1 and product.prices[-1].price and product.prices[-2].price: if str(product.prices[-1].created_at) > minus_one_week: price_changed.append(product) return {'price_changed': [product.to_dict() for product in price_changed], 'total_products': len(products)} ''' sql = "select product_id from prices where month(created_at) = month(now()) and stock = 'Available' group by product_id having count(*) > 1" res = self.pub.call({ 'method': 'sql_select_to_orm', 'resource': 'product', 'sql': sql }) products = unserialize(res['result']) products = [products] if type(products) != list else products return { 'price_changed': [ product.to_dict() for product in products if product.prices[-1].price and product.prices[-2].price and product.prices[-1].price < product.prices[-2].price ] }
def remove_product(self, vald): username = vald['username'] product_id = vald['product_id'] try: res = self.pub.call({ 'method': 'get', 'resource': 'user', 'where': { 'username': username } }) user = unserialize(res['result']) except Exception as e: return {'message': str(e)} for tracked in user.products: if str(tracked.product.id) == product_id: product_id = tracked.product.id user_id = user.id break res = self.pub.call({ 'method': 'delete', 'resource': 'tracked', 'where': { 'product_id': product_id, 'user_id': user_id } }) if not res.get('success'): self.logger.info(res['message']) return res
def search_query(self, query): result = Walcart.search(query.replace(' ', '%20')) if 'message' in result: return {'success': False, 'message': result['message']} elif 'items' in result: products = [ self.burro.make_product(product_data) for product_data in result['items'] ] res = self.pub.call({ 'method': 'save', 'resource': serialize(products) }) if 'success' in res: res = self.pub.call({ 'method': 'get', 'resource': 'product', 'where': { 'id': [p.id for p in products] } }) data = unserialize(res['result']) product = [p.to_dict() for p in data ] if type(data) == list else data.to_dict() return {'success': True, 'product': product} else: return {'success': False}
def paginated(self): self.logger.info('[PAGINATED] Beginning paginated products traversal...') res = self.pub.call({'method': 'get', 'resource': 'category', 'where': {}}) try: categories = unserialize(res['result']) except Exception as e: self.logger.critical(str(e)) self.logger.info('[PAGINATED] Received {} categories'.format(len(categories))) categories = categories[45:] for category in categories: time.sleep(self.delay * self.threads) print(self.delay * self.threads) paginated = Walcart.paginated(category.id) for i in range(category.pages_parsed): if 'nextPage' in paginated: time.sleep(self.delay * self.threads) paginated = Walcart.get_json(paginated['nextPage']) else: category.pages_parsed = -1 if 'items' in paginated: products = [] products_data = paginated['items'] for product_data in products_data: product = self.make_product(product_data) products.append(product) self.logger.info('\t[PAGINATED] Saving {} products...'.format(len(products))) res = self.pub.call({'method': 'save', 'resource': serialize(products)}) if 'success' not in res: self.logger.critical('[PAGINATED] '.format(res['message'])) category.pages_parsed += 1 self.pub.call({'method': 'save', 'resource': serialize(category)})
def search(self, product_id): res = self.pub.call({ 'method': 'get', 'resource': 'product', 'where': { 'id': product_id } }) product = unserialize(res['result']) if not product: product_data = Walcart.product(product_id) if product_data.get('message'): return product_data product = self.burro.make_product(product_data) res = self.pub.call({ 'method': 'save', 'resource': serialize(product) }) recommended_products = self.get_recommended_products(product.id) res = self.pub.call({ 'method': 'save', 'resource': serialize(recommended_products) }) res_recommended_products = [] res = self.pub.call({ 'method': 'get', 'resource': 'product', 'where': { 'id': [p.id for p in recommended_products] } }) if unserialize(res['result']): res_recommended_products = [ p.to_dict() for p in unserialize(res['result']) ] res = self.pub.call({ 'method': 'get', 'resource': 'product', 'where': { 'id': product.id } }) product = unserialize(res['result']) return { 'product': product.to_dict(), 'recommended': res_recommended_products }
def update_prices(self): res = self.pub.call({'method': 'get', 'resource': 'product', 'where': {}}) try: products = unserialize(res['result']) except Exception as e: self.logger.critical('[UPDATE_PRICES] {}'.format(str(e))) for product in products: time.sleep(self.delay * self.threads) self.update_price(product)
def login(self, user): res = self.pub.call({ 'method': 'get', 'resource': 'user', 'where': user }) user = unserialize(res['result']) if user: return {'hash': user.password} self.logger.info('Invalid username') return {'success': False, 'message': 'Invalid username'}
def get_user(self, username): try: res = self.pub.call({ 'method': 'get', 'resource': 'user', 'where': { 'username': username } }) user = unserialize(res['result']) except Exception as e: return {'message': str(e)} return {'success': True, 'user': user.to_dict()}
def save(self, resource): r_obj = unserialize(resource) if type(r_obj) is list: for obj in r_obj: self.session.add(obj) try: self.session.commit() except: self.session.rollback() else: self.session.add(r_obj) try: self.session.commit() self.session.expunge_all() #r_obj = self.session.query(type(r_obj)).filter_by(id=r_obj.id).options(joinedload_all()).one() return {'success': True} except Exception as e: self.logger.info(e) self.session.rollback() return {'message': str(e)}