Ejemplo n.º 1
0
    def from_dict(cls, data: JSONType) -> 'Taxonomy':
        taxonomy = Taxonomy()

        for key, key_data in data.items():
            if key not in taxonomy:
                node = TaxonomyNode(identifier=key,
                                    names=key_data.get('name', {}))
                taxonomy.add(key, node)

        for key, key_data in data.items():
            node = taxonomy[key]
            parents = [taxonomy[ref] for ref in key_data.get('parents', [])]
            node.add_parents(parents)

        return taxonomy
Ejemplo n.º 2
0
    def from_dict(cls, data: JSONType) -> "Taxonomy":
        taxonomy = Taxonomy()

        for key, key_data in data.items():
            if key not in taxonomy:
                node = TaxonomyNode(
                    identifier=key,
                    names=key_data.get("name", {}),
                    synonyms=key_data.get("synonyms", None),
                )
                taxonomy.add(key, node)

        for key, key_data in data.items():
            node = taxonomy[key]
            parents = [taxonomy[ref] for ref in key_data.get("parents", [])]
            node.add_parents(parents)

        return taxonomy
Ejemplo n.º 3
0
def is_selected_image(product_images: JSONType, image_id: str) -> bool:
    for key_prefix in ("nutrition", "front", "ingredients"):
        for key, image in product_images.items():
            if key.startswith(key_prefix):
                if image["imgid"] == image_id:
                    logger.debug("Image {} is a selected image for "
                                 "'{}'".format(image_id, key_prefix))
                    return True

    return False
Ejemplo n.º 4
0
def print_generic_insight(insight: JSONType) -> None:
    for key, value in insight.items():
        click.echo("{}: {}".format(key, str(value)))

    click.echo("url: {}/product/{}".format(settings.BaseURLProvider().get(),
                                           insight["barcode"]))

    if "source" in insight:
        click.echo("image: {}{}".format(settings.OFF_IMAGE_BASE_URL,
                                        insight["source"]))
    click.echo("")
Ejemplo n.º 5
0
def print_generic_insight(insight: JSONType) -> None:
    for key, value in insight.items():
        click.echo("{}: {}".format(key, str(value)))

    click.echo("url: {}".format("https://fr.openfoodfacts.org/produit/"
                                "{}".format(insight["barcode"])))

    if "source" in insight:
        click.echo("image: {}{}".format(STATIC_IMAGE_DIR_URL,
                                        insight["source"]))
    click.echo("")
Ejemplo n.º 6
0
def find_nutrition_image_nutrient_languages(
    mentions: JSONType, ) -> Dict[str, Dict[str, int]]:
    languages: Dict[str, Dict[str, int]] = {}
    for nutrient, matches in mentions.items():
        seen_lang: Set[str] = set()

        for match in matches:
            for lang in match.get("languages", []):
                if lang not in seen_lang:
                    languages.setdefault(nutrient, {})
                    nutrient_languages = languages[nutrient]
                    nutrient_languages.setdefault(lang, 0)
                    nutrient_languages[lang] += 1
                    seen_lang.add(lang)

    return languages
Ejemplo n.º 7
0
def is_recent_image(product_images: JSONType, image_id: str,
                    max_timedelta: datetime.timedelta) -> bool:
    upload_datetimes = []
    insight_image_upload_datetime: Optional[datetime.datetime] = None

    for key, image_meta in product_images.items():
        if not key.isdigit():
            continue

        upload_datetime = datetime.datetime.utcfromtimestamp(
            int(image_meta["uploaded_t"]))
        if key == image_id:
            insight_image_upload_datetime = upload_datetime
        else:
            upload_datetimes.append(upload_datetime)

    if not upload_datetimes:
        logger.debug("No other images")
        return True

    if insight_image_upload_datetime is None:
        raise ValueError("Image with ID {} not found".format(image_id))

    else:
        for upload_datetime in upload_datetimes:
            if upload_datetime - insight_image_upload_datetime > max_timedelta:
                logger.debug("More recent image: {} > {}".format(
                    upload_datetime, insight_image_upload_datetime))
                return False

        sorted_datetimes = [
            str(x) for x in sorted(set(x.date() for x in upload_datetimes),
                                   reverse=True)
        ]
        logger.debug(
            "All images were uploaded the same day or before the target "
            "image:\n{} >= {}".format(insight_image_upload_datetime.date(),
                                      ", ".join(sorted_datetimes)))
        return True

    logger.debug("More recent images: {} < {}".format(
        insight_image_upload_datetime.date(),
        max(x.date() for x in upload_datetimes),
    ))
    return False
Ejemplo n.º 8
0
def is_special_image(images: JSONType,
                     image_path: str,
                     image_type: str,
                     lang: Optional[str] = None) -> bool:
    if not is_valid_image(images, image_path):
        return False

    image_id = pathlib.Path(image_path).stem

    for image_key, image_data in images.items():
        if (image_key.startswith(image_type)
                and str(image_data.get("imgid")) == image_id):
            if lang is None:
                return True

            elif image_key.endswith("_{}".format(lang)):
                return True

    return False