Skip to content

Instantly share code, notes, and snippets.

@loggerhead
Created October 16, 2017 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loggerhead/e8edebd8b59909d4efce8efec3ec538f to your computer and use it in GitHub Desktop.
Save loggerhead/e8edebd8b59909d4efce8efec3ec538f to your computer and use it in GitHub Desktop.
Notify you if there is update of stocks in https://bandwagonhost.com/cart.php
# -*- coding: utf-8 -*-
import os
import time
import requests
from datetime import datetime
from pyquery import PyQuery as pq
URL = 'http://bwh1.net/cart.php'
GAP = 3600
def alert(title, msg):
cmd = '''osascript -e 'display notification "%s" with title "%s"\'''' % (msg, title)
return os.system(cmd)
def alert_diff(diff):
item = sorted(diff)[0]
return alert(item[0], item[1])
def parse_page(html):
items = set()
d = pq(html)
divs = d('#order-web20cart .cartbox')
for div in divs.items():
item = []
for td in div('td').items():
texts = []
for e in td.contents():
if isinstance(e, str):
texts.append(e.strip())
elif e.text:
texts.append(e.text.strip())
item.append(tuple(filter(len, texts)))
item[0] = item[0][0]
item[1] = item[1][-1]
item[2] = ' '.join(item[2])
items.add(tuple(item))
return items
def gen_diff():
rr = [requests.get(URL), None]
while True:
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(GAP)
rr[-1] = requests.get(URL)
rr.reverse()
if all([r.status_code == 200 for r in rr]):
items1 = parse_page(rr[0].text)
items2 = parse_page(rr[1].text)
diff = items1.difference(items2)
print('v' * 120)
for item in sorted(items1):
print("%-60s\t%20s\t%s" % item)
print('^' * 120)
if len(diff):
return diff
else:
msg = "fetch url failed!"
print(msg)
alert("ERROR", msg)
if __name__ == '__main__':
diff = gen_diff()
alert_diff(diff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment