def test_process_individual_county_greene(data_clean):
    county_data = process_individual_county(data_clean,
                                            DATA_INDEX,
                                            county_name="Greene")

    df_deaths = county_data["deaths"]
    print("\n", df_deaths)
def test_process_individual_county_washington(data_clean):
    county_data = process_individual_county(data_clean,
                                            DATA_INDEX,
                                            county_name="Washington")

    df_deaths = county_data["deaths"]
    print("\n", df_deaths)
Example #3
0
def greene_county_data(greene_county_dict, data_clean) -> Dict[str, pd.DataFrame]:
    """
    Creates county_data, ie. a dict of pandas Dataframes with processed cases, deaths, test
    data for Greene county
    """
    county_name_clean = greene_county_dict["42059"]["name"].replace(" County", "")
    return process_individual_county(
        data_clean, DATA_INDEX, county_name=county_name_clean
    )
Example #4
0
def dauphin_county_data(dauphin_info, data_clean) -> Dict[str, pd.DataFrame]:
    """
    Creates county_data, ie. a dict of pandas Dataframes with processed cases, deaths, test
    data for Dauphin county
    """
    county_name = dauphin_info["name"]
    county_name_clean = county_name.replace(" County", "")
    return process_individual_county(
        data_clean, DATA_INDEX, county_name=county_name_clean
    )
def test_process_individual_county_total(data_clean):
    state_data = process_individual_county(data_clean,
                                           DATA_INDEX,
                                           county_name="Total")
    df_cases = state_data["cases"]
    df_cases = df_cases.set_index("date")

    # assertions
    cases_total_july_27 = df_cases.at["2020-07-27", "total"]
    cases_moving_avg_july_27 = df_cases.at["2020-07-27", "moving_avg"]
    assert cases_total_july_27 == 108264
    assert int(cases_moving_avg_july_27) == 932
def test_process_individual_county_dauphin(data_clean):
    county_data = process_individual_county(data_clean,
                                            DATA_INDEX,
                                            county_name="Dauphin")
    df_cases = county_data["cases"]
    df_cases = df_cases.set_index("date")

    # assertions
    cases_total_july_27 = df_cases.at["2020-07-27", "total"]
    cases_total_per_capita_july_27 = df_cases.at["2020-07-27",
                                                 "total_per_capita"]
    assert cases_total_july_27 == 2559
    assert int(round(cases_total_per_capita_july_27)) == 920
def main(
    counties: Dict[str, Dict],
    email_send: bool = True,
    custom_subject_line: str = None,
    condense_email: bool = True,
    aws_bucket: str = AWS_BUCKET,
    aws_dir: str = AWS_DIR,
) -> None:
    """
    Generates a unique newsletter based on COVID-19 data and emails it to selected counties.
    
    Args:
        counties (Dict[str, Dict]): Dict of dicts representing county names and sendgrid email ID,
        email_send (bool, optional): Whether to send emails. Defaults to True. Useful for testing program without
            sending emails.
        custom_subject_line (str, optional): A custom subject line that overwrites the programmatically generated
            subject line. Useful for testing purposes. Disabled by default.
        condense_email (bool, optional): Condenses multiple white spaces in email HTML into single spaces before
            sending. Defaults to True.
        aws_bucket (optional, str): AWS bucket where charts will be uploaded to. Defaults to value stored in
            definitions.py
        aws_dir (optional, str): Directory within AWS bucket where charts will be uploaded. Defaults to value stored in
            definitions.py

    Returns:
        None.
    """

    # set up basic settings
    init_program()

    # fetch COVID-19 data + Spotlight stories for promo purposes
    data_raw = fetch_data(FETCH_DIR_URL, DATA_INDEX)
    stories_raw = fetch_stories(3)

    # clean, filter, process data
    stories_clean = process_stories(stories_raw)
    data_clean = process_clean(data_raw)
    gdf_raw = process_geo(PATH_PA_GEOJSON)
    gdf_processed = merge_geo(gdf_raw, data_clean)

    # loop over dict of counties and generate charts and chatter
    for fips, county_dict in counties.items():
        # skip county if there are no subscribers to email list
        county_name = county_dict["name"]
        email_list_id = county_dict["id"]
        subscriber_count = count_subscribers(email_list_id)
        if subscriber_count == 0:
            logging.info(
                f"No subscribers in {county_name} email list, moving on to next county..."
            )
            continue
        logging.info(
            f"Creating newsletter payload for {county_name}, which has {subscriber_count} subscribers."
        )

        county_name_clean = county_name.replace(" County", "")
        county_data = process_individual_county(
            data_clean, DATA_INDEX, county_name=county_name_clean
        )

        # create email payload
        county_payload = gen_county_payload(
            county_name_clean=county_name_clean,
            data_clean=data_clean,
            county_data=county_data,
            gdf=gdf_processed,
            aws_bucket=aws_bucket,
            aws_dir=aws_dir,
        )

        # Generate HTML
        newsletter_filename = f"newsletter_{county_name_clean}_{est_now_iso()}.html"
        newsletter_local_path = DIR_OUTPUT / newsletter_filename
        newsletter_browser_link = (
            f"https://{aws_bucket}/{aws_dir}/{newsletter_filename}"
        )
        newsletter_vars = gen_jinja_vars(
            county_name=county_name,
            county_payload=county_payload,
            newsletter_browser_link=newsletter_browser_link,
            story_promo=stories_clean,
        )
        html = gen_html(templates_path=DIR_TEMPLATES, template_vars=newsletter_vars)

        # Upload copy of HTML to s3
        with open(newsletter_local_path, "w") as fout:
            fout.writelines(html)
        copy_to_s3(newsletter_local_path, aws_bucket, aws_dir, content_type="text/html")

        # Send email
        if email_send:
            html = html if not condense_email else condense_whitespace(html)
            subject = (
                f"COVID-19 Update: {county_name} ({est_now_ap_brief()})"
                if not custom_subject_line
                else custom_subject_line
            )
            logging.info(f"Sending email for {county_name}...")
            logging.info(f"Subject line: {subject}")
            send_email_list(html, email_list_id, subject=subject)
        else:
            logging.info("No email has been sent because 'email_send' option is False")