Last active
June 12, 2020 00:21
-
-
Save yixiaoyx/be5ea7638abf6461e15290b3a6c695c8 to your computer and use it in GitHub Desktop.
A script to detect price drop
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/python3 | |
# This script is supposed to be run on a linux machine with a mail server, i.e. can send emails via the `mail` command. | |
# Can use cron to set up a schedule to run the script. | |
import re | |
import requests | |
import subprocess | |
import time | |
# If items are too many, can store them in a csv or json and read it here. | |
items = [ | |
('https://xxxxx', 0) # replace with tuples of (product URL, price) | |
] | |
mail_user = '' # replace with mail user name | |
log_file = 'log.txt' # replace with local log file name | |
# If items are from multiple websites, can add the regex into the item list. | |
# If all items come from the same website, can use a pair of global variables as below. | |
regex_price = '"price":"([0-9\.]+)"' # replace with regex to match the price, different for each website | |
regex_name = '/([a-z\-]+)$' # replace with regex to match the product name in the URL, different for each website | |
f = open(log_file, 'a') | |
for (item, price) in items: | |
r = requests.get(item) | |
m = re.search(regex_price, r.text) | |
if m: | |
p = float(m.group(1)) | |
if p < price: | |
n = re.search(regex_name, item) | |
if n: | |
name = n.group(1) | |
email_subject = "Price drop detected: "+name | |
email_content = "original price: "+str(price)+"\ncurrent price: "+str(p)+"\n"+item+"\n" | |
email_process = subprocess.Popen(["mail", "-s", email_subject, mail_user], stdin=subprocess.PIPE) | |
email_process.stdin.write(email_content.encode()) | |
email_process.stdin.close() | |
if f: | |
f.write("\n["+time.ctime()+"] email sent for "+name+" "+str(price)) | |
if f: | |
f.write("\n["+time.ctime()+"] scan completed") | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment