Created
February 23, 2022 08:08
-
-
Save alexgarel/0098218b04e2afb97d33d6f40e485063 to your computer and use it in GitHub Desktop.
Control robotoff auto-applied category insight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Get csv from postgresql: | |
``` | |
docker-compose exec postgres psql -W -U postgres postgres | |
postgres# \copy (select barcode, STRING_AGG(value_tag, ' / ') from product_insight where automatic_processing = 't' and timestamp > '2022-01-20' and type = 'category' group by barcode order by barcode) to '/tmp/auto-cat.csv' with csv; | |
COPY 13910 | |
``` | |
Run this below: | |
``` | |
docker-compose run -v /tmp/:/tmp/auto-cat/ --rm workers poetry run python | |
``` | |
""" | |
import csv | |
data = list(csv.reader(open("/tmp/auto-cat/auto-cat.csv"))) | |
from robotoff.mongo import MONGO_CLIENT_CACHE | |
collection = MONGO_CLIENT_CACHE.get().off.products | |
img_url = "https://images.openfoodfacts.org/images/products" | |
def complete_data(code, cats): | |
infos = collection.find_one({"code": code}, ["product_name", "categories_tags", "ecoscore_grades_tags", "nutrition_grades_tags", "images"]) | |
if infos is None: | |
infos = {} | |
if len(code) >= 9: | |
product_url = (code[:3] + "/" + code[3:6] + "/" + code[6:9] + "/" + code[9:]).rstrip("/") | |
else: | |
product_url = code | |
try: | |
img = next(f"{img_url}/{product_url}/{img_name}.{img_props.get('rev', '')}.200.jpg" for img_name, img_props in infos.get("images", {}).items() if img_name.startswith("front")) | |
except StopIteration: | |
img = None | |
return [ | |
code, | |
cats, | |
infos.get("product_name", ""), | |
"/".join(infos.get("categories_tags", [])), | |
"/".join(infos.get("ecoscore_grades_tags", [])), | |
"/".join(infos.get("nutrition_grades_tags", [])), | |
img | |
] | |
data2 = [complete_data(*d) for d in data] | |
csv.writer(open("/tmp/auto-cat/auto-cat2.csv", "w")).writerows(data2) | |
""" | |
I Then reformat in libreoffice | |
and use https://extensions.libreoffice.org/en/extensions/show/links-to-images | |
to download images. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment