-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_new.py
42 lines (33 loc) · 1.25 KB
/
discord_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import requests
from dateutil import parser as date_parser
import locale
class DiscordNews:
def __init__(self, webhook, username, avatar_url, feed):
self.webhook = webhook
self.username = username
self.avatar_url = avatar_url
self.feed = feed
def prepare_and_notify(self):
latest_entry = self.feed.entries[0] #get the last entry
self.__notify_to_discord_channel(latest_entry)
def notify(self, data):
self.__notify_to_discord_channel(data)
# this is where you can customize the message that will be sent on you server
def __notify_to_discord_channel(self, data):
headers = {"Content-Type": "application/json"}
published_date = date_parser.parse(data.published)
locale.setlocale(locale.LC_TIME, "fr_FR.UTF-8")
format_date = published_date.strftime("%A %d %B %Y")
content = f'''
# 📰📢 Nouvelle publication disponible ! 📢📰
**{data.title}**
🗓 {format_date}
{data.link}
{data.description}
'''
payload = {
"username": self.username,
"content": content,
"avatar_url": self.avatar_url
}
return requests.post(url=self.webhook, headers=headers, json=payload)