from multiprocessing import get_logger
import logging
import json
import requests

from bs4 import BeautifulSoup


def logger(level=logging.INFO) -> logging.getLogger:
    log = get_logger()
    log.setLevel(level)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(
        '%(levelname)s: %(asctime)s - %(process)s - %(message)s'
    ))
    log.addHandler(handler)
    return log


def get_ld_json(response: requests.Response):
    soup = BeautifulSoup(response.content, 'html.parser')
    lds = soup.findAll('script', {'type': 'application/ld+json'})
    if lds:
        for ld in lds:
            if '"Product"' in ld.text:
                return json.loads(ld.text)
    log.info(f'status_code: {response.status_code} ld+json not found for {response.url}')
    return None


log = logger()
