-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_web.py
104 lines (81 loc) · 3.58 KB
/
discord_web.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
import requests
#import json
import sys
from discord_webhook import DiscordWebhook, DiscordEmbed
from getConfig import ConfigHandler
getConfig = ConfigHandler()
def send_webhook(url, id="Unknown", status="error", message=None):
if url is None:
return
if message is not None:
message = remove_ansi_escape_sequences(message)
webhook = DiscordWebhook(url, rate_limit_retry=True, timeout=30)
if(status == "starting"):
title="Starting"
color="fc8803"
embed = DiscordEmbed(title, description="Starting holo-downloader", color=color)
embed.set_timestamp()
webhook.add_embed(embed)
webhook_response = webhook.execute()
return
elif(status=="membership-error"):
title = "Membership error"
color="ff0000"
embed = DiscordEmbed(title, description=("Error checking membership streams for [{0}](https://www.youtube.com/channel/{0}). \nCheck cookies!".format(id)), color=color)
if message:
embed.add_embed_field(name="Error Message: {0}".format(id), value=message)
embed.set_footer(text='Error Logger')
embed.set_timestamp()
#embed.set_author(name='{0}'.format(id), url='www.youtube.com/channel/{0}'.format(id))
#embed.set_thumbnail(url=data.get('thumbnail_url'))
webhook.add_embed(embed)
webhook_response = webhook.execute()
return
embed_error = None
try:
response = requests.get("https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v={0}".format(id), timeout=30)
except Exception as e:
embed_error = str(e)
color="03b2f8"
# create embed object for webhook
# you can set the color as a decimal (color=242424) or hex (color="03b2f8") number
match status:
case "recording":
title = "Recording"
color="0011ff"
case "waiting":
title = "Waiting"
color="ffff00"
case "error":
title = "Error"
color="ff0000"
case "done":
title = "Done"
color="00ff00"
# Check if the request was successful (status code 200)
if embed_error is None and response.status_code == 200:
# Parse the JSON data from the response
data = response.json()
embed = DiscordEmbed(title, description="[{0}](https://youtu.be/{1})".format(data.get('title'),id), color=color)
embed.set_author(name=data.get('author_name'), url=data.get('author_url'))
embed.set_thumbnail(url=data.get('thumbnail_url'))
elif embed_error:
embed = DiscordEmbed(title, description="Video https://youtu.be/{0} is not accessible due to error: {1}".format(id, embed_error[:250]), color=color)
else:
embed = DiscordEmbed(title, description="Video https://youtu.be/{0} is not accessible, perhaps it has been privated".format(id), color=color)
if message:
embed.add_embed_field(name="Message: {0}".format(id), value=message)
embed.set_timestamp()
# add embed object to webhook
webhook.add_embed(embed)
webhook_response = webhook.execute()
def remove_ansi_escape_sequences(text):
import re
# This regular expression matches ANSI escape sequences
ansi_escape = re.compile(r'\x1B[@-_][0-?]*[ -/]*[@-~]')
return ansi_escape.sub('', text)
def main(id, status, message = None):
if getConfig.get_discord_webhook() is not None:
send_webhook(getConfig.get_discord_webhook(), id, status, message)
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])