-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.py
124 lines (106 loc) · 3.8 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
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
124
# this is a script to send webhook to discord
import requests
import json
import logging
from wotd.main import extract_data, download_word_audio
# Load configuration from config.json
with open("config.json", "r") as file:
config = json.load(file)
webhook_url = config["webhook_url"]
error_webhook_url = config["error_webhook_url"]
# Setup logging
logging.basicConfig(
filename="discord_webhook.log",
level=logging.INFO,
format="%(asctime)s - %(message)s",
)
def send_to_discord(data, *audio_files):
global webhook_url # This ensures we're using the global variable
content = (
f"> New <@&937579201551290419>! for {data['date']}\n"
"\n"
f"> # **Word:** {data['word']}\n"
f"> **Yomigana:** {data['yomigana']}\n"
f"> **Romaji:** {data['romaji']}\n"
f"> **Meaning:** {data['words_meaning']}\n"
f"> **Part of the speech:** {data['part_of_speech']}\n"
"\n"
f"> ## Example 1:\n"
f"> <a:Arrow:939136140622069791> {data['example1']}\n"
f"> **Yomigana:** {data['example1_yomigana']}\n"
f"> **Romaji:** {data['example1_romaji']}\n"
f"> **Meaning:** {data['example1_meaning']}\n"
)
if data["example2"] is not None:
content += (
"\n"
f"> ## Example 2:\n"
f"> <a:Arrow:939136140622069791> {data['example2']}\n"
f"> **Yomigana:** {data['example2_yomigana']}\n"
f"> **Romaji:** {data['example2_romaji']}\n"
f"> **Meaning:** {data['example2_meaning']}\n"
)
files = [(f.split("/")[-1], open(f, "rb")) for f in audio_files if f]
response = requests.post(
webhook_url,
data={"content": content},
files={f"file{i+1}": file for i, file in enumerate(files)},
)
# Close the file objects
for _, file_obj in files:
file_obj.close()
if response.status_code != 204:
logging.error(f"Failed to send data to Discord: {response.text}")
print(f"Failed to send data to Discord: {response.text}")
if __name__ == "__main__":
data = extract_data()
if data:
(
word,
main_audio,
date,
yomigana,
romaji,
words_meaning,
part_of_speech,
example1,
example1_yomigana,
example1_romaji,
example1_meaning,
example1_mp3,
example2,
example2_yomigana,
example2_romaji,
example2_meaning,
example2_mp3,
) = data
audio_paths = []
audio_main_path = download_word_audio(main_audio, "word_of_the_day.mp3")
if audio_main_path:
audio_paths.append(audio_main_path)
if example1_mp3:
audio_example1_path = download_word_audio(example1_mp3, "example_1.mp3")
if audio_example1_path:
audio_paths.append(audio_example1_path)
if example2_mp3:
audio_example2_path = download_word_audio(example2_mp3, "example_2.mp3")
if audio_example2_path:
audio_paths.append(audio_example2_path)
output_data = {
"word": word,
"date": date,
"yomigana": yomigana,
"romaji": romaji,
"words_meaning": words_meaning,
"example1": example1,
"example1_yomigana": example1_yomigana,
"example1_romaji": example1_romaji,
"example1_meaning": example1_meaning,
"example2": example2,
"example2_yomigana": example2_yomigana,
"example2_romaji": example2_romaji,
"example2_meaning": example2_meaning,
}
if part_of_speech:
output_data["part_of_speech"] = part_of_speech
send_to_discord(output_data, *audio_paths)