Created
June 11, 2021 11:36
-
-
Save chateaufiesta/071df67ddf192393ebfe6f08091341fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing libraries | |
import hashlib | |
import os | |
import threading | |
import time | |
from urllib.request import urlopen, Request | |
# baseado neste post - https://codingshiksha.com/python/python-3-script-to-monitor-website-url-changes-website-monitoring-full-python-script-for-beginners/ | |
import telebot | |
telegram_api_key = os.environ['TELEGRAM_API_KEY'] | |
bot = telebot.TeleBot(telegram_api_key) | |
mybots = {} | |
BOT_INTERVAL = 3 | |
BOT_TIMEOUT = 30 | |
@bot.message_handler(commands=['start']) | |
def send(message): | |
mybots[message.chat.id] = bot | |
bot.send_message(message.chat.id, "ligado") | |
def send_later(mensagem): | |
for id, bot in mybots.items(): | |
bot.send_message(id, text=mensagem) | |
# setting the URL you want to monitor | |
url = Request('https://covid19.min-saude.pt/pedido-de-agendamento/', | |
headers={'User-Agent': 'Mozilla/5.0'}) | |
# to perform a GET request and load the | |
# content of the website and store it in a var | |
response = urlopen(url).read() | |
# to create the initial hash | |
currentHash = hashlib.sha224(response).hexdigest() | |
time.sleep(10) | |
# bot.polling() | |
def thread_function(name): | |
bot.polling() | |
x = threading.Thread(target=thread_function, args=(1,)) | |
x.start() | |
while True: | |
try: | |
# bot.polling(none_stop=True, interval=BOT_INTERVAL, timeout=BOT_TIMEOUT) | |
# perform the get request and store it in a var | |
response = urlopen(url).read() | |
# create a hash | |
currentHash = hashlib.sha224(response).hexdigest() | |
print(currentHash) | |
# wait for 60 seconds | |
time.sleep(60) | |
# perform the get request | |
response = urlopen(url).read() | |
# create a new hash | |
newHash = hashlib.sha224(response).hexdigest() | |
# check if new hash is same as the previous hash | |
if newHash == currentHash: | |
continue | |
# if something changed in the hashes | |
else: | |
# notify | |
# print("alteração na página") | |
send_later('alteração na página') | |
# again read the website | |
response = urlopen(url).read() | |
# create a hash | |
currentHash = hashlib.sha224(response).hexdigest() | |
# wait for 60 seconds | |
time.sleep(60) | |
continue | |
# To handle exceptions | |
except Exception as e: | |
print("error") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment