Exemplo n.º 1
0
    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:
                    content_from_db = url[2]#self.db.get_url(url[0])
                    content_from_web = FeedHandler.get_url_content(url[0])
                    if content_from_web != content_from_db:
                        message = "Check " + url[0] + " for new changes."
                        self.bot.send_message(chat_id=user[0], text=message)
                    # for post in FeedHandler.parse_feed(url[0]):
                    #     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)

        #timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
        self.db.update_url(url=url[0], last_updated=str(
            timestamp), url_content=content_from_web)
    def add(self, bot, update, args):
        """
        Adds a rss subscription to user
        """

        telegram_user = update.message.from_user

        if len(args) != 2:
            message = "Sorry! I could not add the entry! Please use the the command passing the following arguments:\n\n /add <url> <entryname> \n\n Here is a short example: \n\n /add http://www.feedforall.com/sample.xml ExampleEntry"
            update.message.reply_text(message)
            return

        arg_url = FeedHandler.format_url_string(string=args[0])
        arg_url = args[0]
        arg_entry = args[1]

        # Check if argument matches url format
        # if not FeedHandler.is_parsable(url=arg_url):
        #     message = "Sorry! It seems like '" + \
        #         str(arg_url) + "' doesn't provide an RSS news feed.. Have you tried another URL from that provider?"
        #     update.message.reply_text(message)
        #     return

        # Check if entry does not exists
        entries = self.db.get_urls_for_user(telegram_id=telegram_user.id)
        print(entries)

        if any(arg_url.lower() in entry for entry in entries):
            message = "Sorry, " + telegram_user.first_name + \
                "! I already have that url with stored in your subscriptions."
            update.message.reply_text(message)
            return

        if any(arg_entry in entry for entry in entries):
            message = "Sorry! I already have an entry with name " + \
                arg_entry + " stored in your subscriptions.. Please choose another entry name or delete the entry using '/remove " + arg_entry + "'"
            update.message.reply_text(message)
            return
        
        # get current web page content
        try:
            url_content = FeedHandler.get_url_content(arg_url)
        #print("found: " + url_content)
        except:
            message = "Sorry, cannot connect to that url, check for typos."
            update.message.reply_text(message)
            return

        print("start")
        self.db.add_user_bookmark(
            telegram_id=telegram_user.id, url=arg_url.lower(), alias=arg_entry, url_content=url_content)
        message = "I successfully added " + arg_entry + " to your subscriptions!"
        update.message.reply_text(message)
        print("finished")