def parsing_book(self, link: str) -> dict: """ Parse the site and writes data about the book to the dictionary. If AttributeError (is an invalid reference), the cause is logged and the exception is passed to a higher level. """ try: if 'https://www.labirint.ru/books/' in link: lab = labirint.Labirint() detail_book = lab.parsing(link) self.save_photo(detail_book) return detail_book.copy() elif 'https://www.chitai-gorod.ru/catalog/book/' in link: ch_gorod = chitai_gorod.ChitaiGorod() detail_book = ch_gorod.parsing(self.shorten_link(link)) self.save_photo(detail_book) return detail_book.copy() elif 'https://book24.ru/product/' in link: book24_ = book24.Book24() detail_book = book24_.parsing(self.shorten_link(link)) self.save_photo(detail_book) return detail_book.copy() except Exception as er: logger.show_error(system="parser_manager", error=repr(er)) raise AttributeError
def check_book(): """ This method checks book discounts every 36 minutes and removes books from which no one is subscribed. If there is a discount, then it calls the method of notifying subscribers. """ parser = parser_manager.ParserManager() bot_ = bot.Bot() while True: database = Database() books = database.get_books() database.delete_empty_subscriptions() for book in books: try: book_detail = parser.parsing_book(book['link']) if book_detail['price'] != book['price']: database.change_price(book['link'], book_detail['price']) if book_detail['price'] < book['price']: sale = 100 - int( book_detail['price'] / book['price'] * 100) for follower in book['followers']: if database.check_service(follower): bot_.send_notification( follower, book_detail, sale) except AttributeError as ae: logger.show_error(system="Book24", error=repr(ae)) database.__del__() time.sleep(2200)
def parsing(self, link): detail_book = {} soup = self.get_soup(link) try: detail_book['link'] = link detail_book['title'] = self.get_title(soup) detail_book['price'] = self.get_price(soup) detail_book['image_link'] = self.get_image_link(soup) detail_book['image_name'] = self.get_image_name(detail_book['image_link']) except (AttributeError, IndexError) as ae: logger.show_error(system="ChitaiGorod", error=repr(ae)) raise AttributeError return detail_book