A simple scraper that sends a message on Slack when iPhones are available in Portland.
The default channel on Slack I used was #technology, but feel free to change that.
I put the iphone.txt template in a templates directory, too.
| # -*- coding: utf-8 -*- | |
| """ | |
| Are there new iPhones available in Portland? | |
| """ | |
| import requests as req | |
| from jinja2 import Environment, FileSystemLoader | |
| # --- | |
| # Jinja | |
| # --- | |
| THIS_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| TEMPLATES_DIR = os.path.join(THIS_DIR, "templates") | |
| env = Environment(loader=FileSystemLoader(TEMPLATES_DIR)) | |
| # --- | |
| # URLs | |
| # --- | |
| HEADERS = { | |
| "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11", | |
| "Referer": "http://www.istocknow.com/live/", | |
| "X-Requested-With": "XMLHttpRequest", | |
| } | |
| IPHONE = "http://www.istocknow.com/live/live.php?type=7Plus&operator=att&color=Silver&model=nosmall&ajax=1&nobb=false¬arget=false&noradioshack=false&nostock=true" | |
| # --- | |
| # Slack | |
| # --- | |
| SLACK_HOOK = "https://hooks.slack.com/services/ABC123/XYZ456/SLACKB0T789" | |
| SLACK_TEXT = env.get_template("iphone.txt") | |
| data = req.get(IPHONE, headers=HEADERS).json() | |
| stores = [] | |
| if "48" in data["dataz"]: | |
| stores.append("Bridgeport Village") | |
| if "47" in data["dataz"]: | |
| stores.append("Washington Square") | |
| if "46" in data["dataz"]: | |
| stores.append("Portland - Pioneer Place") | |
| if len(stores): | |
| text = SLACK_TEXT.render(stores=stores).encode("utf8") | |
| print text | |
| req.post(SLACK_HOOK, text) |
| { | |
| "channel": "#technology", | |
| "mrkdwn": true, | |
| "text": ":iphone: iPhone 7+ available at {{ stores | count }} stores", | |
| "attachments": [{ | |
| "mrkdwn_in": ["text"], | |
| "text": "{% for store in stores %}{{ store }}\n{% endfor %}", | |
| "footer": "iPhone 7+ | Silver | 128GB or 256GB | AT&T" | |
| }] | |
| } |