Exemplo n.º 1
0
def get_linked_data_md(art):
    data = None
    for y in art.microdata:
        props = y.get("properties")
        if props is None:
            continue
        tp = str(y.get("type", ""))
        if tp.endswith("/Offer") or tp.endswith("/AggregateOffer"):
            price = props.get("price") or props.get("lowPrice")
            price = parse_price(price)
            if price is None or price == 0:
                continue
            currency = props.get("priceCurrency")
            if currency is None:
                continue
            return {
                "currency": currency,
                "price": price,
                "name": " ".join(art.title.split()),
                "origin": art.url,
                "image": getter(y, "image"),
                "brand": None,
                "description": y.get("description"),
                "source": "md",
            }

        if tp.endswith("/Product"):
            data = deeper_price_md(art)
            if data is not None:
                return data
            continue
        offers = props.get("offers", [])
        if isinstance(offers, dict):
            offers = [offers]
        brand = props.get("brand", {})
        if isinstance(brand, dict):
            brand = brand.get("properties", {}).get("name")
        for offer in offers:
            offer_properties = offer.get("properties")
            if offer_properties is None:
                continue
            currency = offer_properties.get("priceCurrency")
            if currency is None:
                continue
            price = offer_properties.get("price") or offer_properties.get(
                "lowPrice")
            price = parse_price(price)
            if price is None or price == 0:
                continue
            data = {
                "currency": currency,
                "price": price,
                "name": "".join(props.get("name", "")),
                "origin": art.url,
                "image": getter(props, "image"),
                "brand": brand,
                "description": getter(props, "description"),
                "source": "md",
            }
            return data
Exemplo n.º 2
0
def deeper_price_md(art):
    data = None
    for y in art.microdata:
        tp = str(y.get("type", ""))
        if not tp.endswith("/Product"):
            continue
        props = y.get("properties")
        if props is None:
            continue
        name = y.get("properties", {}).get("name") or " ".join(art.title.split())
        if isinstance(name, list):
            name = name[0]
        offers = y.get("properties", {}).get("offers", {})
        offers = props.get("offers", [])
        if isinstance(offers, dict):
            offers = [offers]
        for offer in offers:
            offer = offer.get("properties", offer)
            price = offer.get("price") or offer.get("lowPrice")
            price = parse_price(price)
            if price is None or price == 0:
                continue
            currency = offer.get("priceCurrency")
            if currency is None:
                continue
            brand = y.get("brand") or props.get("brand") or offer.get("brand")
            if isinstance(brand, dict):
                brand = brand.get("properties", {}).get("name")
            image = y.get("image") or props.get("image") or offer.get("image")
            if isinstance(image, list):
                image = image[0]
            if isinstance(image, dict):
                prop = image.get("properties", image)
                image = [v for k, v in prop.items() if "url" in k.lower()]
                if image:
                    image = image[0]
            description = (
                y.get("description") or props.get("description") or offer.get("description")
            )
            if isinstance(description, list):
                description = description[0]
            elif isinstance(description, dict):
                description = json.dumps(description)
            return {
                "currency": currency,
                "price": price,
                "name": name,
                "origin": art.url,
                "image": urljoin(art.url, image),
                "brand": brand,
                "description": description,
                "source": "md",
            }
Exemplo n.º 3
0
def get_linked_data_jd(art):
    data = None
    try:
        jdata = art.jsonld
    except json.JSONDecodeError:
        return None
    for y in jdata:
        if not y:
            continue
        if isinstance(y, list):
            y = y[0]
        if y.get("@type") != "Product":
            continue
        offers = y.get("offers", [])
        if isinstance(offers, dict):
            offers = [offers]
        brand = y.get("brand", {})
        if isinstance(brand, dict):
            brand = brand.get("name")
        for offer in offers:
            currency = offer.get('priceCurrency')
            if currency is None:
                continue
            price = offer.get("price") or offer.get("lowPrice")
            price = parse_price(price)
            if price is None or price == 0:
                continue
            name = getter(y, "name")
            if isinstance(name, list):
                name = "".join(name[:1])
            data = {
                "currency": currency,
                "price": price,
                "name": name,
                "origin": art.url,
                "image": getter(y, "image"),
                "brand": brand,
                "description": getter(y, "description"),
                "source": "jsonld",
            }
            return data