Skip to content

Instantly share code, notes, and snippets.

@DerSchimi
Last active September 12, 2023 05:47
Show Gist options
  • Save DerSchimi/b6138a9e22e27a899284845852ea0775 to your computer and use it in GitHub Desktop.
Save DerSchimi/b6138a9e22e27a899284845852ea0775 to your computer and use it in GitHub Desktop.
AWS Website Monitor
import json
import requests
import boto3
import os
bucketname='webcrawlertempdata'
urltocheck='https://www.google.com'
s3bucketsrn='arn:aws:sns:eu-central-1:783377464316:WebsiteTracker'
def fileExists(filename):
client = boto3.client('s3')
results = client.list_objects(Bucket=bucketname, Prefix=filename)
return 'Contents' in results
def getTempFile(filename):
s3 = boto3.resource('s3')
obj = s3.Object(bucketname, filename)
print(obj.get())
return obj.get()['Body'].read().decode()
def saveTempFile(content, filename):
s3 = boto3.resource('s3')
object = s3.Object(
bucket_name=bucketname,
key=filename
)
object.put(Body=content)
print("temp file saved to: " + filename)
def getWebpage(url):
headers = {'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',
'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Connection':'close'}
response = requests.get(url, headers=headers, timeout=1)
return response.text
def sendNotification(changed, result):
client = boto3.client('sns')
snsArn = s3bucketsrn
message = "Website Changed " + str(changed)
print("Sending notification " + str(changed))
response = client.publish(
TargetArn = snsArn,
Message = message ,
Subject='Website Tracker'
)
def lambda_handler(event, context):
url = urltocheck
filename = "website.txt"
print("Starting checking "+ url)
webpageContent = getWebpage(url)
if not fileExists(filename):
saveTempFile(webpageContent,filename)
return {
'statusCode': 200,
'body': json.dumps('First run, skipping...')
}
oldContent = getTempFile(filename)
result = False
if (oldContent == webpageContent):
print("nothing changed...")
else:
result = True
print("Webpage changed!")
saveTempFile(webpageContent,filename)
print("Ergebnis: ", result)
if result:
sendNotification(result,webpageContent)
return {
'statusCode': 200,
'body': json.dumps('Webpage has changed!')
}
else:
return {
'statusCode': 200,
'body': json.dumps('Webpage has not changed!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment