Skip to content

Instantly share code, notes, and snippets.

@zachwill
Created September 26, 2016 16:20
Show Gist options
  • Select an option

  • Save zachwill/7a9833758decfe748d8ed72152fcc1fc to your computer and use it in GitHub Desktop.

Select an option

Save zachwill/7a9833758decfe748d8ed72152fcc1fc to your computer and use it in GitHub Desktop.

Portland iPhone 7+ Scraper with Slack

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&notarget=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"
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment