-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (54 loc) · 1.62 KB
/
main.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
from rewe import ReweClient
from grocy import GrocyClient
import json
import logging
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
logging.basicConfig(level=logging.INFO)
rewe = ReweClient(
auth_token = config['REWE']['auth_token'],
base_url= 'https://shop.rewe.de/api'
)
grocy = GrocyClient(
api_key = config['Grocy']['api_key'],
base_url = config['Grocy']['base_url']
)
def get_state() -> dict:
try:
f = open('state.json', 'r')
state = json.load(f)
except:
state = {
'parsed_receipts': []
}
return state
def save_state(state: dict):
f = open('state.json', 'w')
json.dump(state, f)
def parse_receipt(receipt: dict):
logging.info(f'Parsing receipt {receipt["receiptId"]}')
for article in receipt['articles']:
if ('productName' not in article):
continue
product_id = grocy.find_or_create_product(
rewe_id=article['nan'],
name=article['productName']
)
logging.info(f'Adding {article["quantity"]} of {product_id} ({article["productName"]}) to stock')
grocy.add_stock(
product_id=product_id,
amount=article['quantity'],
price=article['unitPrice']
)
def main():
state = get_state()
receipts = rewe.get_receipts()
for r in receipts:
if r['receiptId'] not in state['parsed_receipts']:
receipt = rewe.get_receipt(r['receiptId'])
parse_receipt(receipt)
state['parsed_receipts'].append(r['receiptId'])
save_state(state)
if __name__ == '__main__':
main()