Package: kbg
PyKbgPyKbgis a Python wrapper aroundKelbongoo’s website.Kelbongoo is asocial economycompany operating in the East of Paris,
France. It sells products from small local producers (mostly located in thePicardy), and is the only intermediary between the producer and the
consumer (short food supply chain).InstallUsageAPI DocsExamplesCompatibilityNotesInstallpip3installkbgThis requires Python ≥3.5.UsageUse theKbgclass to initiate a connection:fromkbgimportKbgk=Kbg(your_email,your_password)print(k.logged_in())# TrueSome general endpoints are available without connection:fromkbgimportUnauthenticatedKbgk=UnauthenticatedKbg()print(k.logged_in())# FalseAPI DocsKbgTheKbgconstructor takes an email and a password. It raises an exception on
failed login.Kbghas all the endpointsUnauthenticatedKbghas, plus the following ones:logged_in()Return a boolean indicating if the object is successfully connected.get_customer_information()Get some information as adictabout the consumer, including first and last
name, email, phone, email settings.get_customer_orders(page=1)Get all the customer’s orders. This is a paginated endpoint. It returns adictwith anorderskey as well as acount,pageandnext_pageones that you can use to get the next pages, if any.get_all_customer_orders(full=False)Yield all the customer’s orders. This is a useful wrapper aroundget_customer_order.Iffull=Trueis passed, callget_customer_orderon each order to yield its
full information.Note that if all you want is the products’ full names, useget_store_offer_dictsas a lookup map instead offull=Trueto save
unnecessary requests.get_customer_order(order_id)Get more information about a specific order (dict).UnauthenticatedKbgTheUnauthenticatedKbgconstructor doesn’t take any argument.get_stores()Get the list of stores (listofdicts).get_store(store_id)Get a single store (dict).get_store_availabilities(store_id)Get product availabilities at the given store for the current command window,
as a map of product ids to units count.get_store_offer(store_id, force=False)Get the offer at a the given store (dict). This includes all products along
with their producers, categories, and families (subcategories).Note this method is cached; useforce=Trueto force the API call.get_store_offer_dicts(store_id, force=False)Equivalent ofget_store_offerthat returns lookupdicts rather than lists
of items.get_store_status(store_id)Return adictdescribing a store’s status.is_active(bool): is the store active, i.e. are we in the timeframe for
orders?is_full(bool): is the store full, i.e. it can’t take anymore orders?full_tags(strlist): what is full? Possible values:"ORDERS","SEC","FRAIS"ExamplesCreate a simple connection:fromkbgimportKbgk=Kbg("your@email.com","yourpassword")Compute your total spendingtotal_spent=0fororderink.get_all_customer_orders():forproductinorder["products"]:total_spent+=product["consumer_price"]# get a price in euros rather than centstotal_spent/=100print("You spent a total of%.2f€ at Kelbongoo!"%total_spent)Print your most-bought productsfromcollectionsimportCountermy_store="BOR"top_products=Counter()top_producers=Counter()store_products=k.get_store_offer_dicts(my_store)["products"]fororderink.get_all_customer_orders():forproductinorder["products"]:product_id=product["id"]ifproduct_idinstore_products:product=store_products[product_id]top_products[product["product_name"]]+=1top_producers[product["producer_name"]]+=1print("Top products:")forproduct,nintop_products.most_common(5):print("%3d-%s"%(n,product))print("\nTop producers:")forproducer,nintop_producers.most_common(5):print("%3d-%s"%(n,producer))Find your beers coveragemy_store="BOR"offer=k.get_store_offer(my_store)# Get the id of the family 'Bières' ("Beers")beer_family_id=Noneforfamilyinoffer["families"]:iffamily["name"]=="Bières":beer_family_id=family["id"]breakelse:raiseException("Can't find the beer family! :(")# Collect all products in that familybeers={}forproductinoffer["products"]:ifproduct["family_id"]==beer_family_id:beers[product["id"]]="%-40s(%s)"%(product["product_name"],product["producer_name"])known_beers=set()# Collect all *bought* products in that familyfororderink.get_all_customer_orders():forproductinorder["products"]:product_id=product["id"]ifproduct_idinbeers:known_beers.add(product_id)print("You have tasted%dbeers out of%d."%(len(known_beers),len(beers)))iflen(known_beers)!=len(beers):print("Other beers you might want to try:")forbeer_id,beerinbeers.items():ifbeer_idnotinknown_beers:print("*",beer)CompatibilityThis library uses undocumented API endpoints, so it may break at any time.NotesDon’t confuse KBG (Kelbongoo) with theKGB.The Kelbongoo API refers to stores as “locales”, using the first tree letters
in upper-case as a primary key:BORis Borrégo andBICis Bichat, for
example.Prices are given in euro cents; you need to divide them by 100 to get the
price in euro:"consumer_price": 221means it’s something that costs €2.21.Please throttle your requests to respect Kelbongoo’s servers.