Skip to content

Instantly share code, notes, and snippets.

@jamesbvaughan
Created March 7, 2017 08:29
Show Gist options
  • Select an option

  • Save jamesbvaughan/4c501fc99acb75852756a4d1dfc8ca3d to your computer and use it in GitHub Desktop.

Select an option

Save jamesbvaughan/4c501fc99acb75852756a4d1dfc8ca3d to your computer and use it in GitHub Desktop.
#!/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/los-angeles'
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']
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()
@ckllr

ckllr commented Mar 8, 2017

Copy link
Copy Markdown

Are you running this script with Python 2.7+?

@andrewelkins

Copy link
Copy Markdown

It's probably over kill for this, but if you want to play around with a nosql store instead of a file, try tinydb https://tinydb.readthedocs.io/en/latest/

@stealthbomber10

Copy link
Copy Markdown

What does 'POSTMATES_NUMBERS' represent?

@timkofu

timkofu commented Mar 14, 2017

Copy link
Copy Markdown

SQLite is perfect for logging hits, and you'll have data workable via SQL, for something in the future you absolutely hadn't thought of right now.

@jamesbvaughan

Copy link
Copy Markdown
Author

@ckllr I'm running it with Python 3+

@jamesbvaughan

Copy link
Copy Markdown
Author

@andrewelkins Oh that looks cool, I'll try it out. Thanks!

@jamesbvaughan

Copy link
Copy Markdown
Author

@trentandraka It's the path to a file that contains a list of the phone numbers of some friends of mine who wanted to get the same text updates I was getting

@jamesbvaughan

Copy link
Copy Markdown
Author

@timkofu That's a good point. I'm sure that I could think of some cool project to do with the data once I've collected a bunch of it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment