Created
September 5, 2018 00:06
-
-
Save cjwinchester/2ec77b376b0311cffd9dfd24f355fc49 to your computer and use it in GitHub Desktop.
Get alerted, via Slack, to items of interest on free craigslist
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 os | |
import json | |
import requests | |
from bs4 import BeautifulSoup | |
target_keywords = ['bookshelf', 'book shelf', 'desk'] | |
url = 'https://cosprings.craigslist.org/search/zip' | |
r = requests.get(url) | |
hit = any([x in r.text.lower() for x in keywords]) | |
if hit: | |
soup = BeautifulSoup(r.text, 'html.parser') | |
links = soup.find_all('a', {'class': 'result-title'}) | |
targets = [] | |
for kw in target_keywords: | |
for l in links: | |
if kw in l.text.lower(): | |
targets.append(l) | |
slack_hook = os.environ.get('MY_COOL_SLACK_HOOK') | |
if slack_hook: | |
fmt = ' 👉 <{}|{}>' | |
msg = '\n'.join([fmt.format(x['href'], x.text.strip()) | |
for x in targets]) | |
payload = { | |
'channel': '@cjwinchester', | |
'username': 'Craigslist bot', | |
'icon_emoji': ':roast-beef:', | |
'text': '*New on Craigslist*\n' + msg | |
} | |
payload_as_json = json.dumps(payload) | |
requests.post(slack_hook, data=payload_as_json) | |
else: | |
print('You need the "MY_COOL_SLACK_HOOK" variable.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment