-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (109 loc) · 3.75 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import xml.etree.ElementTree as ET
from requests import get
import time
from discord_webhook import DiscordWebhook, DiscordEmbed
import shutil
from dotenv import load_dotenv
def main():
load_dotenv()
rss = mc_rss_to_list()
with open("last-edited.txt") as log:
previous_epoch = float(log.read())
newest_epoch = previous_epoch
post_types = [
"A Minecraft Java Snapshot",
"A Minecraft Java Pre-Release",
"A Minecraft Java Release Candidate",
"A Minecraft Java Release",
]
update_log = False
mc_url_pre = "https://www.minecraft.net"
images_url = os.environ['images_url']
web_hook_pfp = f"{images_url}/pfp.webp"
for item in rss:
publish_epoch = time_convert(item['pubDate'])
if publish_epoch > newest_epoch:
newest_epoch = publish_epoch
if previous_epoch >= publish_epoch:
print("No (more) updated posts")
break
update_log = False
if item["description"] not in post_types:
print("Post not related to minecraft updates.")
else:
image_file_name = item['imageURL'].split('/')[6]
download_image(f"{mc_url_pre}/{item['imageURL']}", image_file_name)
webhook = DiscordWebhook(
url=os.environ['test_webhook_url'],
username=item['primaryTag'],
avatar_url=web_hook_pfp
)
embed = DiscordEmbed(title=item['title'], description=item['description'])
embed.set_url(item['link'])
embed.set_image(f"{images_url}/{image_file_name}")
embed.set_footer(text=item['pubDate'])
webhook.add_embed(embed)
webhook.execute()
if update_log:
with open("last-edited.txt", "w") as log:
log.write(str(newest_epoch))
def time_convert(datetime):
pub_date = datetime.split()
p_weekday = pub_date[0][:-1]
p_day = pub_date[1]
p_mon = pub_date[2]
p_year = pub_date[3]
p_time = pub_date[4]
p_date_form = time.strptime(f"{p_weekday} {p_mon} {p_day} {p_time} {p_year}")
p_epoch = time.mktime(p_date_form)
return p_epoch
def mc_rss_to_list():
rss_url = "https://www.minecraft.net/en-us/feeds/community-content/rss"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/107.0.0.0 Safari/537.36"
}
proxies = {
'http': 'http://bamajoe411.com:3128',
}
req = get(url=rss_url, headers=headers, proxies=proxies, auth=(os.environ['proxy_user'], os.environ['proxy_pass']))
root = ET.fromstring(req.content)
rss_list = []
for item in root.iter('item'):
rss_item = {
item[0].tag: item[0].text,
item[1].tag: item[1].text,
item[2].tag: item[2].text,
item[3].tag: item[3].text,
item[4].tag: item[4].text,
item[5].tag: item[5].text,
}
rss_list.append(rss_item)
return rss_list
def download_image(url, file_name):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/107.0.0.0 Safari/537.36"
}
proxies = {
"http": "http://bamajoe411.com:3128",
}
res = get(
url,
stream=True,
headers=headers,
proxies=proxies,
auth=(os.environ['proxy_user'], os.environ['proxy_pass'])
)
if res.status_code == 200:
with open(f"images/{file_name}", "wb") as f:
shutil.copyfileobj(res.raw, f)
print("Image successfully Downloaded: ", file_name)
else:
print("Image Couldn't be retrieved")
if __name__ == "__main__":
main()
# 1677423600.0