Advertisement
Dr_HaZaRd

Untitled

Oct 7th, 2022
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import requests
  2. import os
  3. from bs4 import BeautifulSoup
  4. import time
  5. import logging
  6.  
  7. URL_TO_MONITOR = "https://gog-games.com/"  # change this to the URL you want to monitor
  8. DELAY_TIME = 15  # seconds
  9.  
  10. def process_html(string):
  11.     soup = BeautifulSoup(string, features="lxml")
  12.  
  13.     # make the html look good
  14.     soup.prettify()
  15.  
  16.     # remove script tags
  17.     for s in soup.select('script'):
  18.         s.extract()
  19.  
  20.     # remove meta tags
  21.     for s in soup.select('meta'):
  22.         s.extract()
  23.  
  24.     # convert to a string, remove '\r', and return
  25.     return str(soup).replace('\r', '')
  26.  
  27.  
  28. def webpage_was_changed():
  29.     """Returns true if the webpage was changed, otherwise false."""
  30.     headers = {
  31.         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  32.         'Pragma': 'no-cache', 'Cache-Control': 'no-cache'}
  33.     response = requests.get(URL_TO_MONITOR, headers=headers)
  34.  
  35.     # create the previous_content.txt if it doesn't exist
  36.     if not os.path.exists("previous_content.txt"):
  37.         open("previous_content.txt", 'w+').close()
  38.  
  39.     filehandle = open("previous_content.txt", 'r')
  40.     previous_response_html = filehandle.read()
  41.     filehandle.close()
  42.  
  43.     processed_response_html = process_html(response.text)
  44.  
  45.     if processed_response_html == previous_response_html:
  46.         return False
  47.     else:
  48.         filehandle = open("previous_content.txt", 'w')
  49.         filehandle.write(processed_response_html)
  50.         filehandle.close()
  51.         return True
  52.  
  53. def main():
  54.     log = logging.getLogger(__name__)
  55.     logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"), format='%(asctime)s %(message)s')
  56.     log.info("Running Website Monitor")
  57.     while True:
  58.         try:
  59.             if webpage_was_changed():
  60.                 log.info("WEBPAGE WAS CHANGED.")
  61.                 send_email_alert(f"URGENT! {URL_TO_MONITOR} WAS CHANGED!")
  62.             else:
  63.                 log.info("Webpage was not changed.")
  64.         except:
  65.             log.info("Error checking website.")
  66.         time.sleep(DELAY_TIME)
  67.  
  68.  
  69. if __name__ == "__main__":
  70.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement