-
-
Save stealthbomber10/b7f87d8d8f68953d1cd4715a28d490dc 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
#!/usr/bin/env python | |
from bs4 import BeautifulSoup | |
from twilio.rest import TwilioRestClient | |
import json | |
import os | |
import re | |
import requests | |
url = 'https://postmates.com/st-louis' | |
def sendText(body): | |
account_sid = os.environ['TWILIO_ACCOUNT_SID'] | |
auth_token = os.environ['TWILIO_AUTH_TOKEN'] | |
twilio_number = os.environ['TWILIO_PHONE_NUMBER'] | |
number_file = os.environ['POSTMATES_NUMBERS'] | |
#add optional file of other people's numbers | |
#client = TwilioRestClient(account_sid, auth_token) | |
with open(number_file, 'r') as numbers: | |
for number in numbers: | |
client.messages.create(body=body, to=number, from_=twilio_number) | |
def extractFreeFoodFromSoup(soup): | |
regex = re.compile('free') | |
strings = list(soup.stripped_strings) | |
freeFood = [s for s in strings if regex.match(s.lower())] | |
return freeFood | |
def hasNewFreeFood(freeFood): | |
try: | |
foodFile = open('/tmp/freefood', 'r+') | |
previousFreeFood = json.load(foodFile) | |
except (FileNotFoundError, json.decoder.JSONDecodeError): | |
foodFile = open('/tmp/freefood', 'w+') | |
previousFreeFood = [] | |
foodFile.seek(0) | |
foodFile.truncate() | |
json.dump(freeFood, foodFile) | |
foodFile.close() | |
return freeFood != previousFreeFood | |
def main(): | |
postmatesPage = requests.get(url) | |
soup = BeautifulSoup(postmatesPage.text, 'html.parser') | |
freeFood = extractFreeFoodFromSoup(soup) | |
if hasNewFreeFood(freeFood): | |
sendText('Free Postmates!\n\n' + '\n'.join(freeFood)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment