Last active
January 10, 2022 15:40
-
-
Save mjdargen/37e74ac8d86161a04edd1c6c6ca71615 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import time | |
import hashlib | |
from urllib.request import urlopen | |
import smtplib | |
import ssl | |
import os | |
import filecmp | |
from dotenv import load_dotenv | |
DIR_PATH = os.path.dirname(os.path.realpath(__file__)) | |
base_url = 'https://www.ncfllandftc.com/' | |
URLs = ['ftc-hybrid-season-structure.html', 'ftc-tournaments.html', | |
'ftc-registration-payment.html'] | |
# send_email arguments: | |
# recipient - str - recipient's email address | |
# subject - str - subject line of email | |
# body - str - body of email | |
def send_email(recipient, subject, body): | |
port = 465 # For SSL | |
smtp_server = "smtp.gmail.com" | |
sender_email = "[email protected]" | |
pw = os.getenv('APP_PW') | |
msg = f"From: {sender_email}\nTo: {recipient}\nSubject: {subject}\n\n{body}" | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: | |
server.ehlo() | |
server.login(sender_email, pw) | |
server.sendmail(sender_email, recipient, msg) | |
def fetch_websites(): | |
current = [] | |
for URL in URLs: | |
response = urlopen(base_url + URL).read() | |
current.append(hashlib.sha224(response).hexdigest()) | |
# requests downloads the content, create a new file to store the data | |
with open(f"{DIR_PATH}/new_hash.txt", "w") as f: | |
f.write('\n'.join(current)) | |
# see if files match | |
try: | |
filecmp.cmp(f"{DIR_PATH}/old_hash.txt", f"{DIR_PATH}/new_hash.txt") | |
except IOError: | |
# old file doesn't exist yet, create empty file | |
f = open(f"{DIR_PATH}/old_hash.txt", "w") | |
f.close() | |
# files match, no new forms submitted | |
if filecmp.cmp(f"{DIR_PATH}/old_hash.txt", f"{DIR_PATH}/new_hash.txt"): | |
print("No new updates. Exiting...") | |
return False | |
# files don't match, so process new data | |
else: | |
print("Updates! Proceeding... ") | |
return True | |
def main(): | |
print("Fetching websites...") | |
new = fetch_websites() | |
if new: | |
print("Processing new data...") | |
recipient = '[email protected]' | |
subject = 'FTC Updates' | |
body = 'There\'s been an update!' | |
send_email(recipient, subject, body) | |
with open(f"{DIR_PATH}/new_hash.txt", "r") as f: | |
contents = f.read() | |
with open(f"{DIR_PATH}/old_hash.txt", "w") as f: | |
f.write(contents) | |
if __name__ == '__main__': | |
load_dotenv() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment