diff --git a/src/twfy_tools/utils/contact_io.py b/src/twfy_tools/utils/contact_io.py index bd4f9de37a..953d17c727 100644 --- a/src/twfy_tools/utils/contact_io.py +++ b/src/twfy_tools/utils/contact_io.py @@ -3,6 +3,7 @@ """ from datetime import date, timedelta +from functools import lru_cache from typer import Typer @@ -14,9 +15,15 @@ ) from twfy_tools.db.models import OptinValues, User +app = Typer() + mailing_list_name = "mySociety Newsletters" -app = Typer() + +@lru_cache +def get_mailing_list_internal_id(): + client = MailChimpHandler(config.MAILCHIMP_API_KEY) + return client.list_name_to_unique_id(mailing_list_name) def get_internal_optin_id( @@ -25,7 +32,7 @@ def get_internal_optin_id( client = MailChimpHandler(config.MAILCHIMP_API_KEY) # get internal id for the mailing list - mailing_list_id = client.list_name_to_unique_id(mailing_list_name) + mailing_list_id = get_mailing_list_internal_id() interest_group_items = client.get_interest_group(mailing_list_id, interest_group) return interest_group_items.interest_name_to_id[interest_name] @@ -62,7 +69,7 @@ def upload_contacts(start_date: date, end_date: date): members_and_values.append(MemberAndInterests(user.email, internal_ids)) client = MailChimpHandler(config.MAILCHIMP_API_KEY) - mailing_list_id = client.list_name_to_unique_id(mailing_list_name) + mailing_list_id = get_mailing_list_internal_id() client.batch_add_to_different_interest_groups(mailing_list_id, members_and_values)