Add Sligro support
This commit is contained in:
parent
0e5abf9140
commit
544f902793
3 changed files with 151 additions and 15 deletions
|
@ -7,7 +7,8 @@ profit_margin = Decimal('1.3')
|
|||
|
||||
|
||||
class AutoUpdate:
|
||||
_meta_re = re.compile(r'#\s*(?P<vendor>ah):(?P<sku>\S+)\s+(?P<units>\d+)x$')
|
||||
_ah_meta_re = re.compile(r'#\s*ah:(?P<sku>\S+)\s+(?P<units>\d+)x$')
|
||||
_sligro_meta_re = re.compile(r'^(?P<gtin13>\d{13})[^#]+#\s*sligro$')
|
||||
|
||||
def __init__(self, vendor, sku, units):
|
||||
self.vendor = vendor
|
||||
|
@ -15,22 +16,36 @@ class AutoUpdate:
|
|||
self.units = units
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.vendor}:{self.sku} {self.units}x'
|
||||
if self.vendor == 'sligro':
|
||||
return f'{self.vendor}'
|
||||
if self.units:
|
||||
return f'{self.vendor}:{self.sku} {self.units}x'
|
||||
return f'{self.vendor}:{self.sku}'
|
||||
|
||||
@staticmethod
|
||||
def from_product_line(line):
|
||||
m = AutoUpdate._meta_re.search(line)
|
||||
if not m:
|
||||
raise Exception('no auto update directive found')
|
||||
return AutoUpdate(m['vendor'], m['sku'], int(m['units']))
|
||||
ah = AutoUpdate._ah_meta_re.search(line)
|
||||
if ah:
|
||||
return AutoUpdate('ah', ah['sku'], int(ah['units']))
|
||||
|
||||
sligro = AutoUpdate._sligro_meta_re.search(line)
|
||||
if sligro:
|
||||
return AutoUpdate('sligro', sligro['gtin13'], None)
|
||||
|
||||
raise Exception('no auto update directive found')
|
||||
|
||||
assert AutoUpdate.from_product_line('# ah:wi162664 8x')
|
||||
assert AutoUpdate.from_product_line('8711327538481,liuk 0.80 Ola Liuk # ah:wi162664 8x')
|
||||
assert AutoUpdate.from_product_line('5000112659184 # sligro')
|
||||
assert AutoUpdate.from_product_line('5000112659184 1.00 Cola Zero # sligro')
|
||||
assert AutoUpdate.from_product_line('5000112659184,colazero 1.00 Cola Zero # sligro')
|
||||
|
||||
|
||||
def find_product_details(auto_update):
|
||||
if auto_update.vendor == 'ah':
|
||||
return scrapers.ah_get_by_sku(auto_update.sku, auto_update.units)
|
||||
if auto_update.vendor == 'sligro':
|
||||
return scrapers.sligro_get_by_gtin(auto_update.sku)
|
||||
raise Exception(f'unknown vendor: {auto_update.vendor}')
|
||||
|
||||
|
||||
|
@ -51,16 +66,17 @@ def update_product_pricings(src):
|
|||
try:
|
||||
prod_info = find_product_details(auto_update)
|
||||
except Exception as err:
|
||||
logging.error('could not update %s %s: %s', auto_update, err)
|
||||
logging.error('could not update %s: %s', auto_update, err)
|
||||
lines_out.append(line)
|
||||
continue
|
||||
|
||||
product_aliases = set()
|
||||
if not line.startswith('#'):
|
||||
product_aliases = set(find_aliases.search(line)['aliases'].split(','))
|
||||
product_aliases.add(prod_info.gtin)
|
||||
|
||||
aliases = ','.join(sorted(product_aliases))
|
||||
human_aliases = set(find_aliases.search(line)['aliases'].split(','))
|
||||
human_aliases -= set([prod_info.gtin])
|
||||
human_aliases -= set(prod_info.aliases)
|
||||
human_aliases = sorted(human_aliases)
|
||||
scannables = ','.join([prod_info.gtin, *prod_info.aliases, *human_aliases])
|
||||
|
||||
# Apply profit margin and divide by the number of units per sold packaging.
|
||||
unit_price = prod_info.price * profit_margin / prod_info.units
|
||||
|
@ -68,7 +84,7 @@ def update_product_pricings(src):
|
|||
unit_price = (unit_price * 20).quantize(Decimal('1'), rounding=ROUND_UP) / 20
|
||||
|
||||
fmt_price = f'{unit_price:.2f}'
|
||||
lines_out.append(f'{aliases:<15} {fmt_price:<6} {prod_info.name:<32} # {auto_update}')
|
||||
lines_out.append(f'{scannables:<30} {fmt_price:<6} {prod_info.name:<60} # {auto_update}')
|
||||
|
||||
logging.debug(f'Found "{prod_info.name}", buy €{prod_info.price/prod_info.units:.2f}, sell €{fmt_price}')
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue