Add some more structuring

This commit is contained in:
polyfloyd 2024-01-06 23:39:19 +01:00
parent 5f6f9c52a7
commit ee9a00eb47
3 changed files with 66 additions and 31 deletions

View file

@ -5,12 +5,23 @@ import re
import subprocess
class Product:
def __init__(self, name, price, ean, units):
self.name = name
self.price = price
self.ean = ean
self.units = units
def __str__(self):
return self.name
def get(url):
compl = subprocess.run(['links', '-source', url], capture_output=True)
return compl.stdout
def ah_get_by_sku(ah_sku):
def ah_get_by_sku(ah_sku, units):
assert re.match('^wi\d+$', ah_sku)
html_src = get(f'https://www.ah.nl/producten/product/{ah_sku}')
@ -24,14 +35,9 @@ def ah_get_by_sku(ah_sku):
else:
raise Exception(f'ah.nl returned no JSON metadata for SKU {ah_sku}')
name = schema['name']
ean = schema['gtin13']
sku = schema['sku']
price = Decimal(schema['offers']['price'])
return {
'name': name,
'price': price,
'ean': ean,
'sku': sku,
}
return Product(
name=schema['name'],
price=Decimal(schema['offers']['price']),
ean=schema['gtin13'],
units=units,
)