Created
March 13, 2023 19:59
-
-
Save hwayne/cb1dbbed66a1d1957d2ae6de5436da3a to your computer and use it in GitHub Desktop.
Script that notifies me when a cubs game is happening
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is running on an aws lambda instance with a CloudWatch trigger: | |
# Trigger is cron(0 17 * * ? *) | |
# CSV is from https://www.mlb.com/cubs/schedule/downloadable-schedule | |
import json | |
from urllib.request import urlopen, Request | |
from urllib.parse import urlencode | |
from os import getenv | |
from csv import DictReader | |
from datetime import datetime | |
from pathlib import Path | |
import boto3 | |
def lambda_handler(event, context): | |
# TODO have separate code paths for daily (pushover) and on-demand (http response) | |
# https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html | |
today = datetime.today().strftime(r"%m/%d/%y") | |
csv = DictReader(Path('./GameTicketPromotionPrice.csv').open()) | |
for row in csv: | |
if row['START DATE'] == today: | |
start, end = row['START TIME'], row['END TIME'] | |
message = f"Cubs Game: {start}-{end}" | |
url = "https://api.pushover.net/1/messages.json" | |
data = urlencode({'token': getenv('PUSHOVER_API'), | |
'user': getenv('PUSHOVER_USER'), | |
'message': message, | |
'title': "Game warning"}) | |
data = data.encode('ascii') | |
with urlopen(url, data=data) as f: | |
pass | |
return { | |
'statusCode': 200, | |
'body': json.dumps(f.status) | |
} | |
return { | |
'statusCode': 200, | |
'body': "No game detected" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment