-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpricewatch.py
executable file
·206 lines (160 loc) · 5.4 KB
/
pricewatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import requests
import configparser
from bs4 import BeautifulSoup
from time import gmtime, strftime
import smtplib
from email.mime.text import MIMEText
from amazon.api import AmazonAPI
import os, sys, getopt
import socket, getpass
def provider_saturn(section):
"""Provider for Saturn.de
"""
try:
url_request = requests.get(section['url'])
soup = BeautifulSoup(url_request.text, "lxml")
result = soup.find(property="product:price:amount")
if not (result is None):
return result.get('content')
else:
return None
except:
return None
def provider_amazon_de(section):
"""Provider for Amazon
"""
try:
amazon = AmazonAPI(section['amazon_access_key'], section['amazon_secret_key'], section['amazon_assoc_tag'], region="DE")
product = amazon.lookup(ItemId=section['asin'])
return product.price_and_currency[0]
except:
return None
def write_to_db(dbcache, section, items, price):
"""Update price entry in DB cache file
"""
if not dbcache.has_section(section):
dbcache.add_section(section)
dbcache.set(section, "currentprice", str(price))
dbcache.set(section, "lastchecked", strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
return
def check_price(dbcache, section, items, price):
"""Check for price change and set notify flags accordingly.
"""
# price must have been previously set
try:
lastprice = float(dbcache.get(section, "currentprice"))
except:
lastprice = None
# price: str to float
try:
price = float(price)
except:
price = None
# sanity check
if not 'notify' in items:
print("Notify method not set in config file!", file=sys.stderr)
exit(-1)
# price available now, but not before
if (not (price is None)) and (lastprice is None):
return {'Notify': True, 'Reason': 'Now available', 'Lastprice': lastprice, 'Price': price}
# price available before, but not now (error?)
if (not (lastprice is None)) and (price is None):
return {'Notify': True, 'Reason': 'Not available anymore', 'Lastprice': lastprice, 'Price': price}
# check if price has changed
if price != lastprice:
if items["notify"] == "price_changed":
return {'Notify': True, 'Reason': 'Price has changed', 'Lastprice': lastprice, 'Price': price}
elif items["notify"] == "price_lower":
if price < lastprice:
return {'Notify': True, 'Reason': 'Price is lower', 'Lastprice': lastprice, 'Price': price}
else:
print("Unknown notify method: " + items["notify"], file=sys.stderr)
exit(-1)
# no change
return {'Notify': False, 'Reason': 'Price not changed', 'Lastprice': lastprice, 'Price': price}
def notify_price(email_to, section, check_price_result):
message = section + ": " + check_price_result['Reason'] + ", new price: " + str(check_price_result['Price']) + ", old price: " + str(check_price_result['Lastprice'])
print(message)
# send email
if not (email_to is None):
email_msg = MIMEText(message)
email_msg['Subject'] = "pricewatch update: " + section
email_msg['From'] = getpass.getuser() + "@" + socket.getfqdn()
email_msg['To'] = email_to
s = smtplib.SMTP('localhost')
s.send_message(email_msg)
s.quit()
def iterate_sections(config):
Success = True
# open cache file
try:
dbcachefilename = config.defaults()['dbfile']
except:
print("Could not find entry dbfile in settings", file=sys.stderr)
exit(-1)
dbconfig = configparser.ConfigParser()
dbconfig.read(dbcachefilename)
for section in config.sections():
retries = config.getint(section, "retries", fallback=1)
retry = 0
price = None
while retry < retries and price is None:
# tuple-list --> dict
dict_section = dict(config.items(section))
provider = config.get(section, "provider")
if provider == "saturn.de":
price = provider_saturn(dict(config.items(section)))
elif provider == "amazon.de":
price = provider_amazon_de(dict(config.items(section)))
else:
print("Provider " + provider + " not found", file=sys.stderr)
exit(-1)
retry += 1
if not (price is None):
print(section + ": provided price " + str(price))
else:
print(section + ": Could not get price!", file=sys.stderr)
Success = False
# check price
check_price_result = check_price(dbconfig, section, dict_section, price)
# make the notification
if check_price_result['Notify'] == True:
notify_price(config.get(section, "email"), section, check_price_result)
# update cache entry in db file
write_to_db(dbconfig, section, dict_section, price)
# write db file to disk
with open(dbcachefilename, 'w') as dbfile:
dbconfig.write(dbfile)
return Success
def main(argv):
# open config file
config = configparser.ConfigParser()
# get script path
source_dir = os.path.dirname(os.path.abspath(__file__))
# default filename of config
config_filename = source_dir + '/pricewatch.ini'
# parse command line options
try:
opts, args = getopt.getopt(argv,"hc:",["config="])
except getopt.GetoptError:
print('pricewatch.py [-h] [-c <config file>]')
exit(2)
for opt, arg in opts:
if opt in ("-c", "--config"):
config_filename = arg
elif opt == '-h':
print('pricewatch.py [-h] [-c <config file>]')
exit(0)
if config.read(config_filename) == []:
print("configuration file not found", file=sys.stderr)
exit(-1)
# iterate over all sections
Success = iterate_sections(config)
print("Success: " + str(Success))
if Success == True:
exit(0)
else:
print("Could not get price for at least one section", file=sys.stderr)
exit(-1)
if __name__ == "__main__":
main(sys.argv[1:])