From d22bf9d35729794f8c248dabccc8a09a9d2a6d90 Mon Sep 17 00:00:00 2001 From: Billy Date: Mon, 2 Dec 2024 18:05:05 +0530 Subject: [PATCH] feat: Add cross platform notifications --- .github/workflows/builds.yml | 56 ++++++++++++++++++++++++++++++++++++ notifier/AoC.spec | 2 +- notifier/app.py | 40 ++++++++++++++++++-------- notifier/pyproject.toml | 17 +++++++++++ 4 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/builds.yml create mode 100644 notifier/pyproject.toml diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml new file mode 100644 index 0000000..a66fed3 --- /dev/null +++ b/.github/workflows/builds.yml @@ -0,0 +1,56 @@ +name: "AoC Notifier Builds" +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + +jobs: + build: + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + include: + - os: ubuntu-latest + artifact-name: AoC-notifier-linux + file-separator: "/" + - os: windows-latest + artifact-name: AoC-notifier-windows + file-separator: "\\" + - os: macos-latest + artifact-name: AoC-notifier-macos + file-separator: "/" + + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Set up Python 3.12 + uses: actions/setup-python@v3 + with: + python-version: "3.12" + + - name: Install requirements + run: | + cd notifier + python -m pip install --upgrade pip + python -m pip install . + python -m pip install pyinstaller + + - name: Build notifier (Windows) + if: runner.os == 'Windows' + run: | + cd notifier + pyinstaller -F -w -i "aoc.ico" --clean -n "AoC" --add-data "aoc.ico;." --add-data "aoc.png;." app.py + + - name: Build notifier (macOS/Linux) + if: runner.os != 'Windows' + run: | + cd notifier + pyinstaller -F -w -i "aoc.ico" --clean -n "AoC" --add-data "aoc.ico:." --add-data "aoc.png:." app.py + + - name: Upload artifacts + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.artifact-name }} + path: "notifier/dist/" diff --git a/notifier/AoC.spec b/notifier/AoC.spec index 11c3f99..785f5a6 100644 --- a/notifier/AoC.spec +++ b/notifier/AoC.spec @@ -5,7 +5,7 @@ a = Analysis( ['app.py'], pathex=[], binaries=[], - datas=[('aoc.png', '.')], + datas=[('aoc.ico', '.'), ('aoc.png', '.')], hiddenimports=[], hookspath=[], hooksconfig={}, diff --git a/notifier/app.py b/notifier/app.py index bab9205..d3a60bd 100644 --- a/notifier/app.py +++ b/notifier/app.py @@ -4,27 +4,43 @@ """ import datetime +import platform import time from pathlib import Path import pytz -from win11toast import toast -EST_TZ = pytz.timezone("US/Eastern") +if platform.system() == "Windows": + from win11toast import toast +else: + from plyer import notification + + toast = notification.notify +EST_TZ = pytz.timezone("US/Eastern") image = str(Path("aoc.png").absolute()) def notify(current_time_est: datetime.datetime): - toast( - "Time for Advent of Code 🎄!", - "Open day {} challenge".format(current_time_est.day), - icon=image, - on_click="https://adventofcode.com/{}/day/{}".format( - current_time_est.year, current_time_est.day - ), - audio="ms-winsoundevent:Notification.Looping.Call7", - ) + if platform.system() == "Windows": + toast( + "Time for Advent of Code 🎄!", + "Open day {} challenge".format(current_time_est.day), + icon=image, + on_click="https://adventofcode.com/{}/day/{}".format( + current_time_est.year, current_time_est.day + ), + audio="ms-winsoundevent:Notification.Looping.Call7", + ) + else: + toast( + title="Time for Advent of Code 🎄", + message="Day {} challenge is out now!".format(current_time_est.day), + app_name="AoC", + app_icon=str(Path("aoc.ico").absolute()), + timeout=10, + ticker="AoC", + ) def main(): @@ -50,4 +66,4 @@ def main(): main() -# pyinstaller -F -w -i "aoc.ico" --clean -n "AoC" --add-data "aoc.png;." app.py +# pyinstaller -F -w -i "aoc.ico" --clean -n "AoC" --add-data "aoc.ico;." --add-data "aoc.png;." app.py diff --git a/notifier/pyproject.toml b/notifier/pyproject.toml new file mode 100644 index 0000000..c4a9ff7 --- /dev/null +++ b/notifier/pyproject.toml @@ -0,0 +1,17 @@ +[tool.poetry] +name = "aoc-notifier" +version = "0.1.0" +description = "" +authors = ["Billy "] +license = "MIT" +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.12" +plyer = "^2.1.0" +win11toast = { version = "^0.35", platform = "win32" } +pytz = "^2024.2" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api"