58 lines
1.3 KiB
Python
58 lines
1.3 KiB
Python
import enum
|
|
import logging
|
|
|
|
import requests
|
|
from frozendict import frozendict
|
|
|
|
from . import mailgun
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SOUND_PATH = 'resource/sound/57808__guitarguy1985__carterattack.mp3'
|
|
|
|
|
|
class NotificationType(enum.Enum):
|
|
EMAIL = 1
|
|
LOUD_SOUND = 2
|
|
|
|
|
|
def send_email_notification(
|
|
session: requests.Session,
|
|
scraper_name: str,
|
|
latest_dict: frozendict,
|
|
) -> None:
|
|
body = ['A new update has occured for ', scraper_name, '\n']
|
|
for k, v in latest_dict.items():
|
|
body.append(f'{k}: {v}\n')
|
|
mailgun.send_email(session, f'Updated {scraper_name}', ''.join(body))
|
|
|
|
|
|
def play_loud_and_annoying_sound(
|
|
_session: requests.Session,
|
|
_scraper_name: str,
|
|
_latest_dict: frozendict,
|
|
) -> None:
|
|
import playsound3
|
|
|
|
playsound3.playsound(SOUND_PATH, block=False)
|
|
|
|
|
|
NOTIFICATION_TYPE_TO_NOTIFIER = {
|
|
NotificationType.EMAIL: send_email_notification,
|
|
NotificationType.LOUD_SOUND: play_loud_and_annoying_sound,
|
|
}
|
|
|
|
|
|
def send_notifications(
|
|
session: requests.Session,
|
|
scraper_name: str,
|
|
latest_dict: frozendict,
|
|
notification_types: set[NotificationType],
|
|
) -> None:
|
|
for notification_type in notification_types:
|
|
NOTIFICATION_TYPE_TO_NOTIFIER[notification_type](
|
|
session,
|
|
scraper_name,
|
|
latest_dict,
|
|
)
|