-
-
Save anonymous/20ea5c7077ae905ca079 to your computer and use it in GitHub Desktop.
scrapes truman boots site for new releases
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 | |
import requests | |
import os | |
import time | |
import sys | |
import signal | |
from urlparse import urljoin | |
import re | |
from bs4 import BeautifulSoup | |
# from IPython import embed | |
ROOT_URL = 'http://www.trumanboot.com/' | |
THE_URL = 'http://www.trumanboot.com/?post_type=product' | |
def anything_new(links): | |
all_urls = ''.join(links) | |
return hash(all_urls) != -817609967207271490 | |
def is_available(): | |
r = requests.get(THE_URL) | |
soup = BeautifulSoup(r.content, 'html.parser') | |
anchors = soup.select('li > div > h3 > a:nth-of-type(1)') | |
links = map(lambda lnk: urljoin(ROOT_URL, lnk.get('href')), anchors) | |
return not anything_new(links) | |
def send_email(): | |
os.system('echo "New Truman boots in stock" | mail -s "Alert" 5551234567@tmomail.net') | |
os.system('echo "New Truman in stock" | mail -s "Alert" example@example.com') | |
def main(): | |
while True: | |
if is_available(): | |
send_email() | |
return 0 | |
else: | |
time.sleep(600) | |
def daemonize(): | |
# remove process from session | |
if os.fork() > 0: | |
sys.exit(0) | |
os.setsid() | |
os.umask(0) | |
if os.fork() > 0: | |
sys.exit(0) | |
# redirect stdin, stdout, and stderr to /dev/null | |
fd = os.open('/dev/null', os.O_RDWR) | |
os.dup2(fd, 0) | |
os.dup2(fd, 1) | |
os.dup2(fd, 2) | |
# ignore SIGHUP | |
signal.signal(signal.SIGHUP, signal.SIG_IGN) | |
with open('truman.pid', 'w') as f: | |
f.write(str(os.getpid()) + "\n") | |
if __name__ == '__main__': | |
daemonize() | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment