import re


def extract_img_and_tag(entry_main, entry_tag):
    """
    Extracts features about an article's main image (3) and tags (1) from the API dictionary
    and returns them: 4 variables returned.
    ------
    ARGUMENTS:
    The API dictionary entries for the main image ('result->field->main') and tags ('result->tags')
    """

    img_path = re.findall(r"<img src=\"(.+?)\"", entry_main)
    if len(img_path) != 0:
        img_url = img_path[0]
    else:
        img_url = "NULL"

    img_descr = re.findall(r"<span class=\"element-image__caption\">(.+?)<", entry_main)
    if len(img_descr) != 0:
        img_capt = img_descr[0]
    else:
        img_capt = "NULL"

    img_credits = re.findall(r"<span class=\"element-image__credit\">(.+?)<", entry_main)
    if len(img_credits) != 0:
        img_cred = img_credits[0]
    else:
        img_cred = "NULL"

    tags = None
    for el in entry_tag:
        if el["type"] == "keyword":
            if tags is None:
                tags = el["webTitle"].lower()
            else:
                tags = f'{tags},{el["webTitle"].lower()}'

    return img_url, img_capt, img_cred, tags