def backup(): while True: time.sleep(60) print(TAG(), "thread_backup:", "START BACKUP ROUTINE") watcher_manager.save_watchers() print(TAG(), "thread_backup:", "BACKUP COMPLETED") flush()
def thread_function(watchers_manager: WatcherManager): LOG = "thread_watchers:" print(TAG(), LOG, "started") while True: # acquire lock watchers_manager.watchers_lock.acquire() print(TAG(), LOG, "start updating watchers") try: # start selenium browser browser = webdriver.PhantomJS(executable_path=PATH_PHANTOM) # for each watcher for user_watchers in watchers_manager.watchers.values(): for watcher in user_watchers: if watcher.isRunning: browser.get(watcher.url) if watcher.type == Watcher.Selector.CSS: elements = browser.find_elements_by_css_selector( watcher.selector) text = "".join( [element.text for element in elements]) else: text = browser.find_element_by_css_selector( 'body').text if watcher.old_text is None: watcher.old_text = text else: if watcher.old_text != text: watcher.old_text = text watcher.update.message.\ reply_text("Notifier {0} has seen new changes! Go to see them:\n{1}" .format(watcher.name, watcher.url)) print( TAG(), LOG, "updated watcher {0}: change saved!". format(watcher.name)) else: print( TAG(), LOG, "watcher {0} checked -> no changes".format( watcher.name)) browser.close() print(TAG(), LOG, "checked every running watcher") except Exception as e: print(LOG, e, file=sys.stderr) finally: # release lock watchers_manager.watchers_lock.release() # wait for next iteration flush() time.sleep(TIMER)
def start_function(bot, update): print(TAG(), "called start: ", update.message) chat_id = update.message.chat_id args = update.message.text.split(" ") if len(args) > 1: name = args[1] ok = watcher_manager.start_watcher(chat_id, name) if ok == "started": bot.send_message( chat_id=chat_id, text="The notifier {0} now is running".format(name)) elif ok == "already": bot.send_message( chat_id=chat_id, text="The notifier {0} is already running".format(name)) else: bot.send_message( chat_id=chat_id, text="The notifier {0} doesn't exist".format(name)) else: bot.send_message( chat_id=chat_id, text="Insert the notifier name that you want to start") flush()
def clear_function(bot: Bot, update): print(TAG(), "called clear: ", update.message) chat_id = update.message.chat_id watcher_manager.clear_watcher(chat_id) bot.send_message(chat_id=chat_id, text="All notifiers are deleted") flush()
def set_function(bot: Bot, update: Update): print(TAG(), "called set: ", update.message) chat_id = update.message.chat_id args = update.message.text.split(" ") if len(args) < 2: bot.send_message(chat_id=chat_id, text="Invalid message. Command pattern is:") bot.send_message(chat_id=chat_id, text="/set name URL [CSS SELECTOR]") return name, url = args[1], args[2] watcher = Watcher(name, url, update) if len(args) > 3: watcher.selector = " ".join(args[3:]) watcher.type = Selector.CSS else: watcher.type = Selector.NONE ok = watcher_manager.add_watcher(chat_id, watcher) if ok: bot.send_message( chat_id=chat_id, text="Notifier {0} correctly created! (SELECTOR: '{1}' ({2}))". format(watcher.name, watcher.selector, watcher.type)) print("{0}: watcher {1} created.".format(chat_id, name)) else: bot.send_message( chat_id=chat_id, text="Notifier {0} already exists. Please delete it".format(name)) flush()
def list_function(bot: Bot, update): print(TAG(), "called list: ", update.message) chat_id = update.message.chat_id watchers = watcher_manager.get_watchers(chat_id) if len(watchers) > 0: text = "" for watcher in watchers: text += str(watcher) + "\n" else: text = "No notifiers available" bot.send_message(chat_id=chat_id, text=text) flush()
def del_function(bot: Bot, update): print(TAG(), "called del: ", update.message) chat_id = update.message.chat_id args = update.message.text.split(" ") if len(args) < 2: bot.send_message(chat_id=chat_id, text="Invalid message. Command pattern is:") bot.send_message(chat_id=chat_id, text="/del notifierName") return name = args[1] ok = watcher_manager.delete_watcher(chat_id, name) if ok: bot.send_message(chat_id=chat_id, text="Notifier {0} deleted!".format(name)) else: bot.send_message(chat_id=chat_id, text="Notifier {0} not found.".format(name)) flush()
print(TAG(), "thread_backup:", "START BACKUP ROUTINE") watcher_manager.save_watchers() print(TAG(), "thread_backup:", "BACKUP COMPLETED") flush() updater = Updater(token=TOKEN) updater.dispatcher.add_handler(CommandHandler('set', set_function)) updater.dispatcher.add_handler(CommandHandler('del', del_function)) updater.dispatcher.add_handler(CommandHandler('clear', clear_function)) updater.dispatcher.add_handler(CommandHandler('list', list_function)) updater.dispatcher.add_handler(CommandHandler('start', start_function)) updater.dispatcher.add_handler(CommandHandler('stop', stop_function)) # set watchers backup t = Thread(target=backup) t.start() # start watchers thread watcher_manager.start() # start telegram updater.start_polling() print(TAG(), "Bot started!") flush() # start idle updater.idle()