from datetime import datetime, timedelta
def make_modification(name, states, dose1, dose2):
datasets = [ { "data": dose1, "label": "Dose #1" }, { "data": dose2, "label": "Dose #2" } ]
url = "https://raw.githubusercontent.com/CITF-Malaysia/citf-public/main/vaccination/vax_state.csv"
# Get yesterday's date because data is only available up to yesterday
kl_timezone = pytz.timezone('Asia/Kuala_Lumpur')
kl_time_now = datetime.now(kl_timezone)
kl_time_yesterday = kl_time_now - timedelta(days=1)
yesterday = kl_time_yesterday.strftime("%Y-%m-%d")
# read the CSV from the URL
# filter the data only for the date we are interested in
rows = df.loc[df["date"] == yesterday]
# Only get the data that we want to trend
states = rows["state"].tolist()
daily_dose1 = rows["dose1_daily"].tolist()
daily_dose2 = rows["dose2_daily"].tolist()
cumul_dose1 = rows["dose1_cumul"].tolist()
cumul_dose2 = rows["dose2_cumul"].tolist()
# Generate modificatiosn that fit Stencil's API format
daily_graph = make_modification("bar_graph_daily", states, daily_dose1, daily_dose2)
cumul_graph = make_modification("bar_graph_cumul", states, cumul_dose1, cumul_dose2)
modifications = [text_date, daily_graph, cumul_graph]
# Set our Zapier webhook to call once image is generatd
"template": "3bc487ef-33ad-46b3-a18c-0d57c3825723",
"modifications": modifications,
"webhook_url": "your-zapier-webhook"
"Authorization": "Bearer your-project-api-key",
"Content-Type": "application/json"
# Call Stencil's API to generate the image asynchronously
req.post("https://api.usestencil.com/v1/images", json=payload, headers=headers)
def lambda_handler(event, context):
'body': json.dumps('Request sent')