import json


def write_to_json(**context):
    """
    Write keywords generated by NLP to local JSON file

    Parameters
    ----------
    context : dict
        Context object that contains kwargs and Xcom data

    Returns
    -------
    status : boolean
        Status of writing sentiment to disk
    """
    data_directory = context['directory']
    file_name = context['filename']
    keywords = context['task_instance'].xcom_pull(task_ids='scrape_articles')
    file_names = list()
    for k in keywords:
        # Strip leading 'https://' and trailing '.com'
        domain = k.replace("https://","")
        domain = domain.replace(".com", "")
        new_fname = f"{data_directory}/{domain}_{file_name}"
        file_names.append(new_fname)
        with open(new_fname, 'w') as f:
            json.dump(
                keywords[k],
                f,
                ensure_ascii=False,
                indent=2,
                sort_keys=True
            )
    return file_names