-
Notifications
You must be signed in to change notification settings - Fork 2
/
ikea-stock-checker.py
60 lines (50 loc) · 2.01 KB
/
ikea-stock-checker.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
from __future__ import print_function
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
from requests import get
from xml.etree import ElementTree
from yagmail import SMTP
import os
items_wanted = {
"Outdoor sectional": "S59028752"
}
local_stores = {
"PA, Conshohocken": 211,
"PA, South Philadelphia": 215,
"NJ, Elizabeth": 154,
"MD, Baltimore": 152
}
region = 'us'
locale = 'en'
availability_url = 'http://www.ikea.com/{region}/{locale}/iows/catalog/availability/{item_id}'
def query():
available = False
output = []
gmail_user = os.environ.get('GMAIL_USER')
gmail_password = os.environ.get('GMAIL_PASSWORD')
for item_name, item_id in items_wanted.items():
availability_data = get(availability_url.format(region=region, locale=locale,
item_id=item_id))
for city, code in local_stores.items():
available_qty = int(ElementTree.fromstring(availability_data.content)
.find(".//localStore[@buCode='{code}']".format(code=code))
.find('.//availableStock').text)
if available_qty > 0:
available = True
message = "{} available {} ({}) {} @ {}".format(
available_qty, item_name, item_id, city, datetime.now())
output.append(message)
print(message)
if available and gmail_user and gmail_password:
SMTP(gmail_user, gmail_password).send(to=gmail_user, subject='Ikea Availability',
contents='\n'.join(output))
else:
print('No items available @ {}'.format(datetime.now()))
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(query, 'interval', hours=1)
print('Press Ctrl+{} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
print('See ya!')