Last active
October 23, 2019 20:40
-
-
Save leah/7c5f8742febc9ca03c92aca820c87a87 to your computer and use it in GitHub Desktop.
A simple Django app to check grossdomesticproduct.com for updates.
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
from pytz import utc | |
from apscheduler.schedulers.blocking import BlockingScheduler | |
from pickup.send import send_text, make_call, check_changes | |
print('Setting up scheduler...') | |
sched = BlockingScheduler(timezone=utc) | |
@sched.scheduled_job('interval', minutes=1) | |
def timed_job(): | |
# This should probably be done in the background but whatever | |
print('This clock job is run every minute.') | |
changed, message = check_changes() | |
if changed: | |
send_text(message=message) | |
make_call() | |
@sched.scheduled_job('cron', hour=15) | |
def scheduled_job(): | |
print('This clock job is run every day at 3pm utc / 8 am pst.') | |
send_text(message='Good morning from the Banksy pickup app! ☀️ Checking for updates every minute.') | |
sched.start() |
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 requests | |
from twilio.rest import Client | |
account_sid = 'xxx' | |
auth_token = 'xxx' | |
from_number = '+15555555555' | |
def send_text(message): | |
client = Client(account_sid, auth_token) | |
# Send text to Leah | |
client.messages.create(body=message, from_=from_number, to='+14155555555') | |
# Send text to fnnch | |
client.messages.create(body=message, from_=from_number, to='+13145555555') | |
def make_call(): | |
client = Client(account_sid, auth_token) | |
# Call Leah | |
client.calls.create(url='http://demo.twilio.com/docs/voice.xml', to='+14155555555', from_=from_number) | |
# Call fnnch | |
client.calls.create(url='http://demo.twilio.com/docs/voice.xml', to='+13145555555', from_=from_number) | |
def check_changes(): | |
# HTML from website | |
req = requests.get('https://grossdomesticproduct.com') | |
web_text = req.text | |
# Remove the email obscuring code since it changes every time | |
start = web_text.find('<a href="/cdn-cgi/l/email-protection') | |
end_text = '[email protected]</span></a>' | |
end = web_text.find(end_text) | |
substring = web_text[start:end+len(end_text)] | |
web_text = web_text.replace(substring, '') | |
# Saved HTML (with email removed) | |
try: | |
file = open('grossdomesticproduct.html') | |
file_text = file.read() | |
file.close() | |
except FileNotFoundError: | |
file_text = '' # file doesn't exist yet, no problem | |
if web_text != file_text: | |
# Update file with changes | |
file = open('grossdomesticproduct.html','w+') | |
file.write(web_text) | |
file.close() | |
return True, 'BANKSY! ✨ grossdomesticproduct.com has changed!' | |
return False, 'No changes to grossdomesticproduct.com. 😪' |
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
from django.http import HttpResponse | |
from .send import send_text, make_call, check_changes | |
def index(request): | |
changed, message = check_changes() | |
send_text(message=message) | |
make_call() | |
return HttpResponse(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment