def main(self, update, context): """ Handles every text message. Checks if it's a link to Lenta's catalog. Saves this link and good info. """ url = update.message.text user_id = update.message.chat_id if self.first_time is True: self.first_time = False logger.info("check discount init") self.check_discount_cycle(context) if not url.startswith(SITE_WHITELIST[0]): replies.not_valid_msg(update, context) return if (user_id in self.json_goods_data) and (url in self.json_goods_data[user_id]): context.bot.send_message(update.message.chat_id, text="Данный товар уже добавлен в список желаемых") return url = self.add_store_in_url(user_id, url) good_info = self.get_new_good_info(url) if not good_info: replies.not_valid_msg(update, context) return self.update_goods_data(user_id, url, good_info) filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION) context.bot.send_message(update.message.chat_id, text="%s по цене %s руб добавлено" % (good_info["title"], good_info["price"]))
def good_not_found(self, context, user_id, url): good_title = self.json_goods_data[user_id][url]["title"] not_fount_phrase = ". Ссылка на товар больше не действительна!" if not_fount_phrase in good_title: # already notified about this issue. return replies.good_not_found_msg(context, user_id, good_title) losted_good_info = {"title": good_title + not_fount_phrase, "price": 0, "promoDate": "", "isPromoForCardPrice": False} self.update_goods_data(user_id, url, losted_good_info) filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION)
def new_discount(self, context, user_id, url, new_good_info): self.update_goods_data(user_id, url, new_good_info, repeatNotif=False) filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION) title = new_good_info['title'] price = new_good_info['price'] promoDate = new_good_info['pomoDate'] try: replies.new_discount_msg(context, user_id, title, price, promoDate) except telegram.error.BadRequest: logger.error("The User has blocked the Bot!")
def handle_del_good(self, update, context): """ Deleting good from json_goods_data. After delete confirmation. """ query = update.callback_query user_id = query.message.chat_id url_part = query.data for url in self.json_goods_data[user_id]: if url_part in url: del self.json_goods_data[user_id][url] filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION) break self.send_user_goods(update, context) return GOOD_LIST
def choose_end(self, update, context): """ Remove buttons after choosing a store. """ query = update.callback_query for lists in query.message.reply_markup.inline_keyboard: for element in lists: if query.data in element['callback_data']: store_name = element["text"] break self.users_stores = {query.message.chat_id: [query.data, store_name]} filesoper.write_json(self.users_stores, self.USERS_STORES_LOCATION) bot = context.bot bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text="Ваш магазин '%s'\nСкиньте ссылку на товар" % store_name ) return ConversationHandler.END
def check_discount_cycle(self, context): for user_id, url in self.iter_goods(): new_good_info = self.get_new_good_info(url) good_title = new_good_info['title'] if new_good_info == "not_available": logger.info(f"Site is not available! for {good_title}") break if new_good_info == "not_found": logger.info(f"Site is not found for {good_title}") self.good_not_found(context, user_id, url) break if not self.is_discount(new_good_info): logger.info(f"No new discounts for {good_title}") self.update_goods_data(user_id, url, new_good_info, repeatNotif=True) filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION) continue if self.is_repeat_notif(user_id, url): logger.info(f"Found old discount for {good_title}") self.old_discount(user_id, url, new_good_info) else: logger.info(f"Found new discount for {good_title}") self.new_discount(user_id, url, new_good_info)
def old_discount(self, user_id, url, new_good_info): self.update_goods_data(user_id, url, new_good_info, repeatNotif=True), filesoper.write_json(self.json_goods_data, self.GOODS_DATA_LOCATION)