def send_newest_messages(self, url, post, user): post_update_date = DateHandler.parse_datetime(datetime=post.updated) url_update_date = DateHandler.parse_datetime(datetime=url[1]) if post_update_date > url_update_date: message = "[" + user[7] + "] <a href='" + post.link + \ "'>" + post.title + "</a>" try: self.bot.send_message( chat_id=user[0], text=message, parse_mode=ParseMode.HTML) except Unauthorized: self.db.update_user(telegram_id=user[0], is_active=0) except TelegramError: # handle all other telegram related errors pass
def send_newest_messages(self, url, post, user): post_update_date = DateHandler.parse_datetime(datetime=post.updated) url_update_date = DateHandler.parse_datetime(datetime=url[1]) if post_update_date > url_update_date: message = "Er is door {0} een nieuw artikel op de website geplaatst!\n" \ "<a href='{1}'>{2}</a>".format(post.author, post.link, post.title) print(message) try: self.bot.send_message( chat_id=user[0], text=message, parse_mode=ParseMode.HTML) except Unauthorized: if not user[0].startswith('@'): self.db.update_user(telegram_id=user[0], is_active=0) except TelegramError: # handle all other telegram related errors traceback.print_exc() pass
def update_feed(self, url): if not self._finished.isSet(): try: get_url_info = self.db.get_update_url(url) last_url = get_url_info['last_url'] date_last_url = DateHandler.parse_datetime( get_url_info['last_update']) feed = FeedHandler.parse_feed( url, 4, date_last_url + timedelta(days=-1)) for post in feed: if not hasattr(post, "published") and not hasattr( post, "daily_liturgy"): logger.warning('not published' + url) continue # for index, post in enumerate(feed): date_published = DateHandler.parse_datetime(post.published) if hasattr(post, "daily_liturgy"): if date_published > date_last_url and post.link != last_url \ and post.daily_liturgy != '': message = post.title + '\n' + post.daily_liturgy result = self.send_newest_messages(message, url) if post == feed[-1] and result: self.update_url(url=url, last_update=date_published, last_url=post.link) elif date_published > date_last_url and post.link != last_url: message = post.title + '\n' + post.link result = self.send_newest_messages(message, url) if result: self.update_url(url=url, last_update=date_published, last_url=post.link) else: pass return True, url except TypeError as e: logger.error(f"TypeError {url} {str(e)}") return False, url, 'update_feed' except TelegramError as e: logger.error( f"except update_feed TelegramError {url} {str(e)}") return False, url, 'update_feed'
def backup(self): now = DateHandler.get_datetime_now() last_backup = self.get_value_name_key('backup', 'last_backup') last_backup = DateHandler.parse_datetime(last_backup) date_last_backup = DateHandler.date(last_backup) hour_last_backup = DateHandler.time(last_backup) if date_last_backup < DateHandler.date(now): if hour_last_backup <= DateHandler.time(now): mapping = {'last_backup': str(now)} self.set_name_key('backup', mapping=mapping) self.redis.save() return True else: return False
def update_feed(url): try: get_url_info = db.get_update_url(url) last_url = get_url_info['last_url'] date_last_url = DateHandler.parse_datetime(get_url_info['last_update']) feed = FeedHandler.parse_feed(url, 4, date_last_url + timedelta(days=-1)) for post in feed: if not hasattr(post, "published") and not hasattr( post, "daily_liturgy"): logger.warning('not published' + url) continue date_published = DateHandler.parse_datetime(post.published) if hasattr(post, "daily_liturgy"): if date_published > date_last_url and post.link != last_url \ and post.daily_liturgy != '': message = post.title + '\n' + post.daily_liturgy result = send_newest_messages(message=message, url=url, disable_page_preview=True) if post == feed[-1] and result: update_url(url=url, last_update=date_published, last_url=post.link) elif date_published > date_last_url and post.link != last_url: message = post.title + '\n' + post.link result = send_newest_messages(message=message, url=url) if result: update_url(url=url, last_update=date_published, last_url=post.link) else: pass except TypeError as _: logger.error(f"TypeError {url} {str(_)}")
def send_newest_messages(self, url, post, user): post_update_date = DateHandler.parse_datetime(datetime=post.updated) url_update_date = DateHandler.parse_datetime(datetime=url[1]) if post_update_date > url_update_date: filters = self.db.get_filters(user[0], url[0]) send = len( filters) == 0 # if there are no filters. send everything for filter in filters: send |= self.match_filter(post, filter) if send: message = "[" + user[7] + "] <a href='" + post.link + \ "'>" + post.title + "</a>" try: self.bot.send_message(chat_id=user[0], text=message, parse_mode=ParseMode.HTML) except Unauthorized: self.db.update_user(telegram_id=user[0], is_active=0) except TelegramError: # handle all other telegram related errors pass
def update_feed(self, url): telegram_users = self.db.get_users_for_url(url=url[0]) for user in telegram_users: if user[6]: # is_active try: posts = FeedHandler.parse_feed(url[0]) for post in posts: self.send_newest_messages(url=url, post=post, user=user) except: traceback.print_exc() message = "Something went wrong when I tried to parse the URL: \n\n " + \ url[0] + "\n\nCould you please check that for me? Remove the url from your subscriptions using the /remove command, it seems like it does not work anymore!" self.bot.send_message(chat_id=user[0], text=message, parse_mode=ParseMode.HTML) if len(posts) > 0: last = str(DateHandler.parse_datetime(posts[0]['published'])) self.db.update_url(url=url[0], last_updated=last)