forked from JordanMartin/ikea-stock-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock_checker.py
73 lines (58 loc) · 2.56 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# coding: utf-8
import requests
import json
import cli_ui as cli
class StockChecker:
products = []
store_id: int
def __init__(self, api_url):
"""api_url: eg: https://iows.ikea.com/retail/iows/fr/fr/stores/%d/availability/ART"""
self.api_url = api_url
def get_availability(self, ref: str):
headers = {
'accept': 'application/vnd.ikea.iows+json;version=1.0',
'contract': '37249',
'consumer': 'MAMMUT#pip-range'
}
# xxx.xxx.xxx -> xxxxxxxxx
ref = ref.replace('.', '')
r = requests.get(self.api_url + '/' + ref, headers=headers)
return r.json()
def printAll(self):
rows = []
for product in self.products:
ref = product['ref']
name = product['name']
stock = product['StockAvailability']['RetailItemAvailability']['AvailableStock']['$']
location = product['StockAvailability']['RetailItemAvailability']['RecommendedSalesLocation']['$']
stock_forcast = product['StockAvailability']['AvailableStockForecastList']['AvailableStockForecast']
if 'RestockDateTime' in product['StockAvailability']['RetailItemAvailability']:
restock_date = product['StockAvailability']['RetailItemAvailability']['RestockDateTime']['$']
else:
restock_date = '-'
row = [
(cli.reset, ref),
(cli.reset, name),
(cli.green if stock != '0' else cli.red, stock),
(cli.reset, self.format_location(location)),
(cli.blue, restock_date)
]
for forcast in stock_forcast:
row.append((cli.reset, forcast['AvailableStock']['$']))
rows.append(row)
headers = ['Ref', 'Name', 'Stock', 'Location', 'Restock date', 'D+1', 'D+2', 'D+3', 'D+4']
cli.info_table(rows, headers=headers)
def format_location(self, rawPlace: str) -> str:
"""Format the place when located in the warehouse"""
if not isinstance(rawPlace, str):
return str(rawPlace)[0:2] + " " + str(rawPlace)[2:4]
return rawPlace
def ask_ref(self):
ref = cli.ask_string('Add a reference number (xxx.xxx.xxx)')
name = cli.ask_string('Display name (optionnal)')
self.add_ref(ref, name)
def add_ref(self, ref: str, name: str = None):
product = self.get_availability(ref)
product['ref'] = ref
product['name'] = name if name != None else '-'
self.products.append(product)