initial commit - aka shitty version

This commit is contained in:
helican 2023-12-10 22:18:48 +01:00
commit c27f6cc464

146
auerbot.py Executable file
View file

@ -0,0 +1,146 @@
#!/bin/env python
import requests
import argparse
import sys
import json
import pprint
from bs4 import BeautifulSoup
# Currently only "Eurobehälter-geschlossen" are supported
base_url = "https://www.auer-packaging.com/de/de/Eurobeh%C3%A4lter-geschlossen"
url_base = "https://www.auer-packaging.com/de/de"
product_map = {
"EG": "Eurobehälter-geschlossen",
"SD": "Scharnierdeckel-für-Eurobehälter"
}
s = requests.Session()
pp = pprint.PrettyPrinter(indent=4)
ppe = pprint.PrettyPrinter(indent=4,stream=sys.stderr)
# get available items for a specific config entry
# Needs a config entry and current used dict
# Returns a dict of { item: count }
def auer_bware_getcurrent(entry,used):
print(f'{entry["name"]}: {entry["qty"]} of {entry["alts"][0]},...',file=sys.stderr)
got = 0
getting = {}
if not entry.get("bware"):
return None
# Test every alternative and
for item in entry["alts"]:
bitem = f"B-{item}"
if got >= entry["qty"]:
break
available = auer_bware_getcount(base_url,item)
u = used.get(f"{bitem}",0)
g = getting.get(f"{bitem}",0)
if available is not None:
if available - u > 0:
totake = entry["qty"] if available - u > entry["qty"] else available - u
used[f"{bitem}"] = u + totake
getting[f"{bitem}"] = g + totake
got += totake
print(f'\treserving {totake} of {bitem}',file=sys.stderr)
else:
print(f"permutation {bitem} not available",file=sys.stderr)
return getting
#used[f'{entry["alts"][0]}'] = used.get(f'{entry["alts"][0]}',0) + entry["qty"]
# Returns integer for available items
def auer_bware_getcount(base_url,item):
try:
ret = s.get(auer_tourl(f'B-{item}'))
if ret.status_code == 200:
content = BeautifulSoup(ret.text,features="lxml")
available = int(content.find_all("div", class_="hinweis-cell product-availability")[0].contents[1])
print(f"\t{item}: {available-u}/{available}",file=sys.stderr)
return available
else:
return None
except Exception as e:
print(f"Accessing item {item} failed with: {e}",file=sys.stderr)
return None
def auer_tourl(item,skip_b=True):
pcode = item.split("-")[0]
if pcode == "B" and skip_b:
pcode = item.split("-")[1]
if pcode in product_map.keys():
return f'{url_base}/{product_map[pcode]}/{item}.html'
else:
return None
def main():
used = {}
needed = 0
parser = argparse.ArgumentParser()
parser.add_argument("-c", help="Filename for json config instead of stdin")
args = parser.parse_args()
# parse command line
if args.c is not None:
inf = open(args.c)
else:
inf = sys.stdin
conf = json.load(inf)
ppe.pprint(conf)
for entry in conf:
print(f'{entry["name"]}: {entry["qty"]} of {entry["alts"][0]},...',file=sys.stderr)
needed += entry["qty"]
got = 0
if entry.get("bware"):
for base in entry["alts"]:
perms = []
if entry.get("reinforced"):
perms.append('-WB')
else:
perms.append('')
newperms = []
if entry.get("handles--"):
for perm in perms:
newperms.append(f"-HG{perm}")
else:
if entry.get("handles++"):
for perm in perms:
newperms.append(f"-HO{perm}")
else:
for perm in perms:
newperms.append(f"-HO{perm}")
newperms.append(f"{perm}")
perms = newperms
for perm in perms:
ret = s.get(auer_tourl(f'B-{base}{perm}'))
u = used.get(f"B-{base}{perm}",0)
if ret.status_code == 200:
content = BeautifulSoup(ret.text,features="lxml")
available = int(content.find_all("div", class_="hinweis-cell product-availability")[0].contents[1])
print(f"\t{base}{perm}: {available-u}/{available}",file=sys.stderr)
if available - u > 0 and got < entry["qty"]:
totake = entry["qty"] if available - u > entry["qty"] else available - u
used[f"B-{base}{perm}"] = u + totake
print(f'\treserving {totake} of {base}{perm}',file=sys.stderr)
break
else:
print(f"permutation {base}{perm} not available",file=sys.stderr)
else:
continue
break
else:
used[f'{entry["alts"][0]}'] = used.get(f'{entry["alts"][0]}',0) + entry["qty"]
unum = 0
for k,v in used.items():
unum += v
print(f"needed = {needed}")
print(f"available = {unum}")
pp.pprint(used)
print(f"{float(unum)/float(needed)*100:.2f}% available")
if inf is not sys.stdin:
inf.close()
if __name__ == "__main__":
main()