|
| 1 | +""" |
| 2 | +Push Notifications via Discord |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import List |
| 7 | + |
| 8 | +import requests |
| 9 | + |
| 10 | +from camply.config import DiscordConfig |
| 11 | +from camply.containers import AvailableCampsite |
| 12 | +from camply.notifications.base_notifications import BaseNotifications |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class DiscordNotifications(BaseNotifications): |
| 18 | + """ |
| 19 | + Push Notifications via Discord |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + super().__init__() |
| 24 | + self.session.headers.update({"Content-Type": "application/json"}) |
| 25 | + if any([DiscordConfig.DISCORD_WEBHOOK is None, DiscordConfig.DISCORD_WEBHOOK == ""]): |
| 26 | + warning_message = ( |
| 27 | + "Discord is not configured properly. To send Discord messages " |
| 28 | + "make sure to run `camply configure` or set the " |
| 29 | + "proper environment variable: `DISCORD_WEBHOOK`." |
| 30 | + ) |
| 31 | + logger.error(warning_message) |
| 32 | + raise EnvironmentError(warning_message) |
| 33 | + |
| 34 | + def send_message(self, message: str, **kwargs) -> requests.Response: |
| 35 | + """ |
| 36 | + Send a message via Discord - if environment variables are configured. |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + message: str |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + requests.Response |
| 45 | + """ |
| 46 | + message_json = kwargs |
| 47 | + if message: |
| 48 | + message_json["content"] = message |
| 49 | + |
| 50 | + logger.debug(message_json) |
| 51 | + response = self.session.post( |
| 52 | + url=DiscordConfig.DISCORD_WEBHOOK, |
| 53 | + json=message_json, |
| 54 | + ) |
| 55 | + try: |
| 56 | + response.raise_for_status() |
| 57 | + except requests.HTTPError as he: |
| 58 | + logger.warning( |
| 59 | + "Notifications weren't able to be sent to Discord. " |
| 60 | + "Your configuration might be incorrect." |
| 61 | + ) |
| 62 | + raise ConnectionError(response.text) from he |
| 63 | + return response |
| 64 | + |
| 65 | + def block_for_campsite(self, campsite: AvailableCampsite): |
| 66 | + message_title, formatted_dict = self.format_standard_campsites( |
| 67 | + campsite=campsite, |
| 68 | + ) |
| 69 | + |
| 70 | + # Remove items that will be templated as part of the embed. |
| 71 | + del formatted_dict["Recreation Area"] |
| 72 | + del formatted_dict["Booking Date"] |
| 73 | + del formatted_dict["Booking End Date"] |
| 74 | + del formatted_dict["Facility Name"] |
| 75 | + del formatted_dict["Booking Link"] |
| 76 | + del formatted_dict["Campsite Site Name"] |
| 77 | + del formatted_dict["Campsite Loop Name"] |
| 78 | + |
| 79 | + return { |
| 80 | + "author": { |
| 81 | + "name": f"🏕 {campsite.recreation_area}" |
| 82 | + }, |
| 83 | + "title": f"{campsite.facility_name} {campsite.campsite_loop_name} #{campsite.campsite_site_name}", |
| 84 | + "description": f"{campsite.booking_date} to {campsite.booking_end_date}", |
| 85 | + "url": campsite.booking_url, |
| 86 | + "color": 2375436, |
| 87 | + "fields": [ |
| 88 | + { |
| 89 | + "name": key, |
| 90 | + "value": str(value) |
| 91 | + } for key, value in formatted_dict.items() |
| 92 | + ], |
| 93 | + "footer": { |
| 94 | + "text": "camply, the campsite finder ⛺️" |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + def send_campsites(self, campsites: List[AvailableCampsite], **kwargs): |
| 99 | + """ |
| 100 | + Send a message with a campsite object |
| 101 | +
|
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + campsites: AvailableCampsite |
| 105 | + """ |
| 106 | + self.send_message( |
| 107 | + message="", |
| 108 | + embeds=[self.block_for_campsite(campsite) for campsite in campsites], |
| 109 | + ) |
0 commit comments