-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (78 loc) · 3.38 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
from bs4 import BeautifulSoup
import time
from discord_webhook import DiscordWebhook, DiscordEmbed
url = "https://thunderstore.io/c/lethal-company/?ordering=last-updated"
webhook_url = "https://discord.com/api/webhooks/{channel_id}/{token}"
def find_new_dicts(old_list, new_list):
"""
Find new dictionaries in new_list that are not present in old_list.
Parameters:
- old_list: List of dictionaries
- new_list: List of dictionaries
Returns:
- List of dictionaries containing new entries in new_list
"""
names = [i.get("name") for i in old_list]
new_dicts = [new_dict for new_dict in new_list if new_dict.get("name") not in names]
return new_dicts
def fetch_items():
"""
Webscrapes the thunderstore website for updates
Returns:
- List of dictionaries containing all the mods
"""
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
listitems = soup.find("div", class_="package-list")
out = []
for item in listitems.children:
if item.find("h5") == -1:
continue
name = item.find("h5").text
children = list(item.children)
img = children[1].find("img")["src"]
post_url = "https://thunderstore.io" + children[1].find("a")["href"]
description = children[5].text.strip()
tags = [i.text.strip() for i in children[7] if i.text.strip() != ""]
likebar = children[3]
downlaods = list(list(likebar.children)[1].children)[1].text.strip()
likes = list(list(likebar.children)[1].children)[3].text.strip()
author = list(likebar.children)[-2].find("a").text.strip()
author_url = "https://thunderstore.io" + list(likebar.children)[-2].find("a")["href"]
out.append({
"name": name,
"url": post_url,
"img": img,
"description": description,
"tags": tags,
"likes": likes,
"downlaods": downlaods,
"author": author,
"author_url": author_url
})
return out
items = fetch_items()
#items.remove(items[2])
while True:
new_items = fetch_items()
diff = find_new_dicts(items, new_items)
items = new_items
print("Diff:", [i.get("name") for i in diff])
for mod in diff:
# Create the embed and send it to the server
webhook = DiscordWebhook(url=webhook_url)
embed = DiscordEmbed(title=mod.get("name"), description=mod.get("description"), color="df3432", url=mod.get("url"))
embed.set_thumbnail(url=mod.get("img"))
embed.add_embed_field(name="Downloads", value="`"+mod.get("downlaods")+"` <:LethalPoint:1187900986300309566>", inline=True)
embed.add_embed_field(name="Likes", value="`"+mod.get("likes")+"` <:LethalUwU:1188231578674004030>", inline=True)
embed.add_embed_field(name="Tags", value="```"+", ".join(mod.get("tags"))+"```", inline=False)
embed.set_author(name=mod.get("author"), url=mod.get("author_url"))
embed.set_footer(text="Cosmic Collectors", icon_url="https://cdn.discordapp.com/icons/1178990094301007942/a_7bb927e6b20b7257069d540216be9a26.webp")
embed.set_timestamp()
webhook.add_embed(embed)
resp = webhook.execute()
# Check every five minutes
for i in range(30):
print(f"\r[{'.'*i}{' '*(29-i)}] ", end="", flush=True)
time.sleep(10)