-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
67 lines (50 loc) · 2.15 KB
/
webhook.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import time
from discord_webhook import DiscordEmbed, DiscordWebhook
from configparser import ConfigParser
from room import Room
config = ConfigParser()
def update_config():
config.read_file(open(r'config.cfg'))
def notify_added(rooms: [Room], content="@everyone new flat available!"):
update_config()
for room in rooms:
for url in config['webhook_urls'].values():
webhook = DiscordWebhook(url, content=content)
embed = create_embed(room)
webhook.add_embed(embed)
response = webhook.execute()
time.sleep(0.1)
def notify_removed(rooms: [Room], content="@everyone old flat was taken down!"):
update_config()
for room in rooms:
for url in config['webhook_urls'].values():
webhook = DiscordWebhook(url, content=content)
embed = create_embed(room)
embed.title = "❌ ~~" + embed.title + "~~"
embed.description = "~~" + embed.description + "~~"
embed.set_color(0)
embed.set_footer()
embed.set_thumbnail()
webhook.add_embed(embed)
response = webhook.execute()
time.sleep(0.1)
def notify_back_online(old_rooms: [Room], new_rooms: [Room]):
config.read_file(open(r'config.cfg'))
added_rooms = new_rooms.difference(old_rooms)
removed_rooms = old_rooms.difference(new_rooms)
for url in config['webhook_urls'].values():
message = f"Good morning! During downtime `{len(added_rooms)}` rooms became available. " \
f"`{len(removed_rooms)}` rooms were taken down."
webhook = DiscordWebhook(url, content=message)
response = webhook.execute()
def create_embed(room):
embed = DiscordEmbed()
embed.set_color("ea4c6c")
embed.set_url(room.url())
embed.set_title(room.title)
embed.set_description(room.description)
embed.add_embed_field(name="Address", value=room.address, inline=True)
embed.add_embed_field(name="Rent", value=room.price_fmt(), inline=True)
embed.set_footer(text="Uploaded at " + str(room.submitted))
embed.set_thumbnail(url="https://www.woko.ch/images/logos/woko-logo.png")
return embed