Skip to content

Commit

Permalink
Merge pull request #1 from nietoga/release-2
Browse files Browse the repository at this point in the history
Release 2
  • Loading branch information
nietoga authored Dec 4, 2024
2 parents 569b379 + 1925372 commit d907663
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 72 deletions.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dotenv import dotenv_values
import project_resource
import file_utils

_config = dotenv_values(project_resource.get_path(".env"))
_config = dotenv_values(file_utils.get_absolute_path(".env"))


def get(key: str) -> str | None:
Expand Down
11 changes: 11 additions & 0 deletions external_services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import Type

from mail import MailService
from user_data import UserDataProvider
from tray import Tray


class ExternalServices:
mail_service: MailService
user_data_provider: UserDataProvider
Tray: Type[Tray]
25 changes: 25 additions & 0 deletions external_services/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from external_services import ExternalServices


def _configure_mail():
from .mail import mail_service

ExternalServices.mail_service = mail_service


def _configure_user_data():
from .user_data import user_data_provider

ExternalServices.user_data_provider = user_data_provider


def _configure_tray_class():
from .tray import TrayClass

ExternalServices.Tray = TrayClass


def configure():
_configure_mail()
_configure_user_data()
_configure_tray_class()
10 changes: 10 additions & 0 deletions external_services/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import config
from mail.mailjet import MailJetService

MAILJET_API_KEY = config.get("MAILJET_API_KEY") or ""
MAILJET_API_SECRET = config.get("MAILJET_API_SECRET") or ""

mail_service = MailJetService(
api_key=MAILJET_API_KEY,
api_secret=MAILJET_API_SECRET,
)
3 changes: 3 additions & 0 deletions external_services/tray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from tray.pysg_tray import SgTray

TrayClass = SgTray
3 changes: 3 additions & 0 deletions external_services/user_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from user_data.pysg_provider import SgUserDataProvider

user_data_provider = SgUserDataProvider("__lol_monitor__")
7 changes: 7 additions & 0 deletions file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))


def get_absolute_path(local_path: str) -> str:
return os.path.join(CURRENT_DIRECTORY, local_path)
15 changes: 14 additions & 1 deletion mail/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
from .mailjet import send_mail
from abc import ABC, abstractmethod


class MailService(ABC):
@abstractmethod
def send_mail(
self,
email_from: str,
email_to: str,
subject: str,
text_part: str,
html_part: str | None = None,
) -> bool:
pass
56 changes: 30 additions & 26 deletions mail/mailjet.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
from mailjet_rest import Client
import config

MAILJET_API_KEY = config.get("MAILJET_API_KEY")
MAILJET_API_SECRET = config.get("MAILJET_API_SECRET")
from mail import MailService

mailjet = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET), version="v3.1")

class MailJetService(MailService):
def __init__(self, api_key: str, api_secret: str, debug: bool = False) -> None:
super().__init__()
self._mailjet_client = Client(auth=(api_key, api_secret), version="v3.1")
self._debug = debug

def send_mail(
email_from: str,
email_to: str,
subject: str,
text_part: str,
html_part: str | None = None,
) -> bool:
data = {
"Messages": [
{
"From": {"Email": email_from},
"To": [{"Email": email_to}],
"Subject": subject,
"TextPart": text_part,
"HTMLPart": html_part,
}
]
}
def send_mail(
self,
email_from: str,
email_to: str,
subject: str,
text_part: str,
html_part: str | None = None,
) -> bool:
data = {
"Messages": [
{
"From": {"Email": email_from},
"To": [{"Email": email_to}],
"Subject": subject,
"TextPart": text_part,
"HTMLPart": html_part,
}
]
}

response = mailjet.send.create(data=data)
response = self._mailjet_client.send.create(data=data)

print(response.status_code)
print(response.json())
if self._debug:
print(response.status_code)
print(response.json())

return response.ok
return response.ok
73 changes: 46 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@
from time import sleep
import PySimpleGUIQt as sg

from mail import send_mail
from monitoring import is_lol_runing
import config

from userdata import get_user_data, set_user_data
import file_utils

import project_resource
from external_services import ExternalServices
import external_services.configuration as external_services_config

external_services_config.configure()
mail_service = ExternalServices.mail_service
user_data = ExternalServices.user_data_provider
Tray = ExternalServices.Tray

DEFAULT_MAIL_SENDER = config.get("DEFAULT_MAIL_SENDER")


def monitor_user():
while True:
if is_lol_runing():
subscriber_email = get_user_data("subscriber_email")
name = get_user_data("name", "Someone")
subscriber_email = user_data.get_user_data("subscriber_email")
name = user_data.get_user_data("name", "Someone")
email_sent = False

if subscriber_email:
email_sent = send_mail(
email_sent = mail_service.send_mail(
DEFAULT_MAIL_SENDER,
subscriber_email,
"LoL Monitor Warning",
Expand All @@ -43,55 +48,69 @@ def monitor_user():
def run_main_window():
layout = [
[sg.Text("Settings")],
[sg.Text("Your Name"), sg.InputText(get_user_data("name", ""), key="-NAME-")],
[
sg.Text("Your Name"),
sg.InputText(user_data.get_user_data("name", ""), key="-NAME-"),
],
[
sg.Text("Subscriber Email"),
sg.InputText(
get_user_data("subscriber_email", ""), key="-SUBSCRIBER_EMAIL-"
user_data.get_user_data("subscriber_email", ""),
key="-SUBSCRIBER_EMAIL-",
),
],
[sg.Button("Save"), sg.Button("Close")],
[sg.Button("Save"), sg.Button("Hide")],
]

window = sg.Window(
"LoL Monitor",
layout,
size=(300, 1),
icon=project_resource.get_path("rat.ico"),
icon=file_utils.get_absolute_path("rat.ico"),
)

while True:
event, values = window.read()

if event == "Save":
set_user_data("name", values["-NAME-"])
set_user_data("subscriber_email", values["-SUBSCRIBER_EMAIL-"])
elif event in [sg.WINDOW_CLOSED, "Close"]:
user_data.set_user_data("name", values["-NAME-"])
user_data.set_user_data("subscriber_email", values["-SUBSCRIBER_EMAIL-"])
elif event in [sg.WINDOW_CLOSED, "Hide"]:
break

window.close()


def run_system_tray():
tray = sg.SystemTray(
menu=["", ["Open", "Exit"]],
filename=project_resource.get_path("rat.ico"),
)
def close_tray(tray: Tray):
tray.close()

while True:
menu_item = tray.read()
def open_window(tray: Tray):
tray.set_visible(False)
run_main_window()
tray.set_visible(True)

if menu_item == "Exit":
break
elif menu_item in [sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED, "Open"]:
tray.hide()
run_main_window()
tray.un_hide()
tray = Tray(file_utils.get_absolute_path("rat.ico"), ["Open", "Exit"])
tray.attach_listener("Exit", close_tray)
tray.attach_listener("Open", open_window)
tray.attach_listener(Tray.DOUBLE_CLICK_EVENT, open_window)
tray.run()

tray.close()

def exit_application(signum, frame):
exit()


def main():
import signal

signal.signal(signal.SIGINT, exit_application)
signal.signal(signal.SIGTERM, exit_application)

if __name__ == "__main__":
lol_monitor_thread = threading.Thread(target=monitor_user, daemon=True)
lol_monitor_thread.start()
run_system_tray()


if __name__ == "__main__":
main()
7 changes: 0 additions & 7 deletions project_resource.py

This file was deleted.

31 changes: 31 additions & 0 deletions tray/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod


class Tray(ABC):
DOUBLE_CLICK_EVENT = "__DOUBLE_CLICK__"

def __init__(self, icon_path: str, menu_items: list[str]) -> None:
self._icon_path = icon_path
self._menu_items = menu_items
self._visible = True
self._closed = False
self._listeners: dict[str,] = {}

def set_visible(self, visible):
self._visible = visible

def is_visible(self):
return self._visible

def close(self):
self._closed = True

def is_closed(self):
return self._closed

def attach_listener(self, key, listener):
self._listeners[key] = listener

@abstractmethod
def run():
pass
34 changes: 34 additions & 0 deletions tray/pysg_tray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import PySimpleGUIQt as sg

from tray import Tray


class SgTray(Tray):
DOUBLE_CLICK_EVENT = sg.EVENT_SYSTEM_TRAY_ICON_DOUBLE_CLICKED

def __init__(self, icon_path: str, menu_items: list[str]) -> None:
super().__init__(icon_path, menu_items)

self._tray = sg.SystemTray(
menu=["", self._menu_items],
filename=self._icon_path,
)

def set_visible(self, visible):
super().set_visible(visible)

if self.is_visible():
self._tray.un_hide()
else:
self._tray.hide()

def close(self):
super().close()
self._tray.close()

def run(self):
while not self.is_closed():
menu_item = self._tray.read(0)

if menu_item in self._listeners:
self._listeners[menu_item](self)
11 changes: 11 additions & 0 deletions user_data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from abc import ABC, abstractmethod


class UserDataProvider(ABC):
@abstractmethod
def set_user_data(self, key: str, value: str) -> None:
pass

@abstractmethod
def get_user_data(self, key: str, default: str | None = None) -> str | None:
pass
15 changes: 15 additions & 0 deletions user_data/pysg_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import PySimpleGUIQt as sg

from user_data import UserDataProvider


class SgUserDataProvider(UserDataProvider):
def __init__(self, settings_prefix: str = "") -> None:
super().__init__()
self._settings_prefix = settings_prefix

def set_user_data(self, key: str, value: str) -> None:
sg.user_settings_set_entry(self._settings_prefix + key, value)

def get_user_data(self, key: str, default: str | None = None) -> str | None:
return sg.user_settings_get_entry(self._settings_prefix + key, default)
9 changes: 0 additions & 9 deletions userdata.py

This file was deleted.

0 comments on commit d907663

Please sign in to comment.