From eb32384a4ebebc02982bc6fe78a1c1c281782bbe Mon Sep 17 00:00:00 2001 From: publicmatt Date: Mon, 18 Dec 2023 18:03:22 -0800 Subject: [PATCH] add python for updating chat bubble. --- .gitignore | 2 + Makefile | 16 ++ build-svg.py | 96 ++++++++++++ chat.svg | 20 +-- requirements.txt | 4 + template.svg | 381 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 509 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 build-svg.py create mode 100644 requirements.txt create mode 100644 template.svg diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85c55eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.venv diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2e098f5 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +PYTHON=.venv/bin/python + +.PHONY: build install + +install: requirements.txt + $(PYTHON) -m pip install -r $^ + +build: build-svg.py template.svg + $(PYTHON) $< + git add '*.svg' && \ + GIT_AUTHOR_NAME='publicmatt' GIT_AUTHOR_EMAIL='git@publicmatt.com' \ + GIT_COMMITTER_NAME='publicmatt' GIT_COMMITTER_EMAIL='git@publicmatt.com' \ + git commit -m "Auto-updating git readme" + git push publicmatt + git push github + diff --git a/build-svg.py b/build-svg.py new file mode 100644 index 0000000..3f3817d --- /dev/null +++ b/build-svg.py @@ -0,0 +1,96 @@ +import os +import requests +import pendulum +import json +from dotenv import load_dotenv +import sys +from pathlib import Path + +# load env files +load_dotenv() + +# part of current dir +app_path = Path(__file__).parent + +# Environment variable for API key +WEATHER_API_KEY = os.getenv("WEATHER_API_KEY") +WEATHER_DOMAIN = "http://dataservice.accuweather.com" + +# emojis that accuweather uses +emojis = { + 1: "β˜€οΈ", + 2: "β˜€οΈ", + 3: "🌀", + 4: "🌀", + 5: "🌀", + 6: "πŸŒ₯", + 7: "☁️", + 8: "☁️", + 11: "🌫", + 12: "🌧", + 13: "🌦", + 14: "🌦", + 15: "β›ˆ", + 16: "β›ˆ", + 17: "🌦", + 18: "🌧", + 19: "🌨", + 20: "🌨", + 21: "🌨", + 22: "❄️", + 23: "❄️", + 24: "🌧", + 25: "🌧", + 26: "🌧", + 29: "🌧", + 30: "πŸ₯΅", + 31: "πŸ₯Ά", + 32: "πŸ’¨", +} + +dayBubbleWidths = { + "Monday": 235, + "Tuesday": 235, + "Wednesday": 260, + "Thursday": 245, + "Friday": 220, + "Saturday": 245, + "Sunday": 230, +} + +# Time since graduating +today = pendulum.now() +todayDay = today.format("dddd") + +psTime = pendulum.instance(today).diff_for_humans( + pendulum.datetime(2023, 12, 14), absolute=True +) + +# Today's weather +locationKey = "331416" # Bellingham +url = f"forecasts/v1/daily/1day/{locationKey}?apikey={WEATHER_API_KEY}" + +try: + response = requests.get(f"{WEATHER_DOMAIN}/{url}") + response.raise_for_status() + json_data = response.json() + + degF = round(json_data["DailyForecasts"][0]["Temperature"]["Maximum"]["Value"]) + degC = round((degF - 32) * 5 / 9) + icon = json_data["DailyForecasts"][0]["Day"]["Icon"] + + with open(app_path / "template.svg", "r", encoding="utf-8") as file: + data = file.read() + + data = data.replace("{degF}", str(degF)) + data = data.replace("{degC}", str(degC)) + data = data.replace("{weatherEmoji}", emojis[icon]) + data = data.replace("{psTime}", psTime) + data = data.replace("{todayDay}", todayDay) + data = data.replace("{dayBubbleWidth}", str(dayBubbleWidths[todayDay])) + + with open(app_path / "chat.svg", "w", encoding="utf-8") as file: + file.write(data) + +except requests.exceptions.RequestException as err: + print(err, file=sys.stderr) diff --git a/chat.svg b/chat.svg index fb68a03..a97e3f4 100644 --- a/chat.svg +++ b/chat.svg @@ -248,10 +248,10 @@ - + - Hi, I'm Jason + Hi, I'm Matt @@ -272,11 +272,11 @@ - + - I live in Columbus, Ohio where it’s supposed to be - 49Β° F (9Β° C) and 🌧 today. + I live in Bellingham, Washington where it’s supposed to be + 46Β° F (8Β° C) and 🌦 today. @@ -300,8 +300,8 @@ - I’m a product designer. I used to work at GitHub, - but I’ve been at PlanetScale for almost 3 years now. + I write software. I work in web, data and machine learning, + and graduated from Western Washington University with a MS in Computer Science 5 days ago. @@ -322,12 +322,12 @@ - + - You can find me on Twitter at - https://twitter.com/jasonlong + You can find me online at + https://www.publicmatt.com/ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b3cdf78 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +requests +pendulum +quantiphy +python-dotenv diff --git a/template.svg b/template.svg new file mode 100644 index 0000000..ea44ba5 --- /dev/null +++ b/template.svg @@ -0,0 +1,381 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hi, I'm Matt + + + + + + + + + + + + + + + + + + + + + + + + I live in Bellingham, Washington where it’s supposed to be + {degF}Β° F ({degC}Β° C) and {weatherEmoji} today. + + + + + + + + + + + + + + + + + + + + + + + + I write software. I work with web tech, data and machine learning, + and graduated from Western Washington University with a MS in Computer Science {psTime} ago. + + + + + + + + + + + + + + + + + + + + + + + + + You can find me online at + https://www.publicmatt.com/ + + + + + + + + + + + + + + + + + + + + + + + + + Thanks for stopping by + + + + + + + + + + + + + + + + + + + + + + + + Have a great {todayDay}! πŸ‘‹ + +