2024-06-02 21:16:11 +00:00
|
|
|
import enum
|
2024-06-02 21:14:19 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from frozendict import frozendict
|
|
|
|
|
|
|
|
from . import mailgun
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2024-06-02 21:31:01 +00:00
|
|
|
SOUND_PATH = 'resource/sound/57808__guitarguy1985__carterattack.mp3'
|
2024-06-06 21:50:29 +00:00
|
|
|
# SOUND_PATH = 'resource/sound/516855__matrixxx__wake-up-01.wav'
|
|
|
|
|
2024-06-02 21:16:11 +00:00
|
|
|
|
2024-06-02 21:14:19 +00:00
|
|
|
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:
|
2024-06-02 21:31:01 +00:00
|
|
|
import playsound3
|
2024-06-06 21:50:29 +00:00
|
|
|
|
2024-06-02 21:31:01 +00:00
|
|
|
playsound3.playsound(SOUND_PATH, block=False)
|
2024-06-02 21:16:11 +00:00
|
|
|
|
2024-06-06 21:50:29 +00:00
|
|
|
|
2024-06-02 21:14:19 +00:00
|
|
|
NOTIFICATION_TYPE_TO_NOTIFIER = {
|
2024-06-02 21:16:11 +00:00
|
|
|
NotificationType.EMAIL: send_email_notification,
|
|
|
|
NotificationType.LOUD_SOUND: play_loud_and_annoying_sound,
|
2024-06-02 21:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def send_notifications(
|
|
|
|
session: requests.Session,
|
|
|
|
scraper_name: str,
|
|
|
|
latest_dict: frozendict,
|
|
|
|
notification_types: set[NotificationType],
|
|
|
|
) -> None:
|
|
|
|
for notification_type in notification_types:
|
2024-06-02 21:16:11 +00:00
|
|
|
NOTIFICATION_TYPE_TO_NOTIFIER[notification_type](
|
2024-06-06 21:50:29 +00:00
|
|
|
session,
|
|
|
|
scraper_name,
|
|
|
|
latest_dict,
|
2024-06-02 21:16:11 +00:00
|
|
|
)
|