Skip to content

Commit

Permalink
added better logging and gotify notifications on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboardmedicNL committed Jun 10, 2024
1 parent 8e1d59e commit 461686d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
6 changes: 5 additions & 1 deletion config/example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"use_remote_post": "true or false",
"remote_http_server_url": "http://google.com",
"bot_name": "NAME_OF_YOUR_BOT_FOR_REMOTE_POST",
"post_interval": "time in minutes to post to remote post"
"post_interval": "time in minutes to post to remote post",
"ping_id": "user or group id from discord to ping when an error occurs",
"use_gotify": "true or false",
"gotifyurl": "your gotify server url",
"verbose:": "0, 1 or 2 (0 is basic logging, 1 is extensive logging, 2 is extensive logging with remote logging responses)"
}
52 changes: 37 additions & 15 deletions youtubelivebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# functions used in script

# formats embed for discord webhook and posts to url
def discord_log(title,color,description):
def discord_log(title,color,description,ping):
if use_discord_logs.lower() == "true":
if color == "blue":
color = 1523940
Expand All @@ -25,14 +25,30 @@ def discord_log(title,color,description):
color = 703235
elif color == "purple":
color = 10622948
data_for_log_hook = {"embeds": [
elif color == "gray" or color == "grey":
color = 1776669
if ping:
ping_string = f"<@{ping_id}>"
else:
ping_string = ""
data_for_log_hook = {"content": ping_string, "embeds": [
{
"title": title,
"color": color,
"description": description
}
]}
rl = requests.post(discord_log_webhook, json=data_for_log_hook)
if verbose:
print(f"sending message to discord remote log webhook with title: {title} Color: {color} Description: {description} and ping: {ping_string}")
time.sleep(1)

# gotify notification
def gotify(title,message,priority):
if use_gotify.lower() == "true":
gr = requests.post(gotifyurl, data={"title": title, "message": message, "priority":priority})
if verbose >= 2:
print(f"sending notification to gotify with title: {title} message: {message} priority: {priority}")
time.sleep(1)

#pulls data from config
Expand All @@ -47,10 +63,14 @@ def discord_log(title,color,description):
use_web_server = config_json["use_web_server"]
use_remote_post = config_json["use_remote_post"]
use_discord_logs = config_json["use_discord_logs"]
use_gotify = config_json["use_gotify"]
gotifyurl = config_json["gotifyurl"]
ping_id = config_json["ping_id"]
verbose = int(config_json["verbose"])
if use_discord_logs.lower() == "true":
discord_log_webhook = config_json["discord_remote_log_url"]
print("succesfully loaded config")
discord_log("Youtubelivebot","blue","succesfully loaded config")
discord_log("Youtubelivebot","blue","succesfully loaded config",False)

#webserver for monitoring purposes
if use_web_server.lower() == "true":
Expand All @@ -59,7 +79,7 @@ def thread_second():
process_thread = threading.Thread(target=thread_second)
process_thread.start()
print("starting webserver for local monitoring")
discord_log("Youtubelivebot","blue","starting webserver for local monitoring")
discord_log("Youtubelivebot","purple","starting webserver for local monitoring",False)

#post process to talk to remote monitor
if use_remote_post.lower() == "true":
Expand All @@ -68,7 +88,7 @@ def thread_third():
process_thread = threading.Thread(target=thread_third)
process_thread.start()
print("starting post server for remote monitoring")
discord_log("Youtubelivebot","blue","starting post server for remote monitoring")
discord_log("Youtubelivebot","purple","starting post server for remote monitoring",False)

# checks if name of video contains pursuit and if so posts video to webhook
while True:
Expand All @@ -77,17 +97,17 @@ def thread_third():
time_to_poll= math.ceil(1440 * len(channels) / 100)
time_to_sleep = time_to_poll * 60
print(f"calculated time between polls is {str(time_to_poll)} minutes")
discord_log("Youtubelivebot","yellow",f"calculated time between polls is {str(time_to_poll)} minutes")
discord_log("Youtubelivebot","yellow",f"calculated time between polls is {str(time_to_poll)} minutes",False)
# loop checks all channels in config and searches for video titles matching defined keywords in config
for channel in channels:
print(f"polling channel {channel}")
discord_log("Youtubelivebot","green",f"polling channel {channel}")
discord_log("Youtubelivebot","green",f"polling channel {channel}",False)
r = requests.get('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channel, '&eventType=live&type=video&key=' + youtube_api_key)
request = r
print(f"requested information for channel {channel} with response: {str(request)}")
# checks if get request was succesfull
if "200" in str(request):
discord_log("Youtubelivebot","green",f"requested information for channel {channel} with response: {str(request)}")
discord_log("Youtubelivebot","green",f"requested information for channel {channel} with response: {str(request)}",False)
yt_api_json = request.json()
try:
item_count = len(yt_api_json["items"])
Expand All @@ -101,20 +121,22 @@ def thread_third():
if video_id_to_send not in l:
l.append(video_id_to_send)
print(f"found video matching criteria, posting video with id: {video_id_to_send}")
discord_log("Youtubelivebot","green",f"found video matching criteria, posting video with id: {video_id_to_send}")
discord_log("Youtubelivebot","green",f"found video matching criteria, posting video with id: {video_id_to_send}",False)
r = requests.post(discord_webhook_url, data={"content": notification_message + "https://www.youtube.com/watch?v=" + video_id_to_send,})
else:
print(f"Live video found but it did not match the criteria for {channel}")
discord_log("Youtubelivebot","yellow",f"Live video found but none matching the criteria {channel}")
discord_log("Youtubelivebot","yellow",f"Live video found but none matching the criteria {channel}",False)
else:
print(f"no live videos found for channel {channel}")
discord_log("Youtubelivebot","yellow",f"no live videos found for channel {channel}")
discord_log("Youtubelivebot","yellow",f"no live videos found for channel {channel}",False)
else:
discord_log("Youtubelivebot","red",f"requested information for channel {channel} with response: {str(request)}")
print(f"waiting for {str(time_to_poll)} minutes")
discord_log("Youtubelivebot","yellow",f"waiting for {str(time_to_poll)} minutes")
discord_log("Youtubelivebot","red",f"requested information for channel {channel} with response: {str(request)}",True)
gotify("Clipbot",f"requested information for channel {channel} with response: {str(request)}","5")
print(f"main loop finished, waiting for {str(time_to_poll)} minutes")
discord_log("Youtubelivebot","gray",f"main loop finished, waiting for {str(time_to_poll)} minutes",False)
time.sleep(time_to_sleep)
except Exception as e:
print(f"An exception occurred in main loop: {str(e)} waiting for 1 minute")
discord_log("youtubelivebot","red",f"An exception occurred in main loop: {str(e)} waiting for 1 minute")
discord_log("youtubelivebot","red",f"An exception occurred in main loop: {str(e)} waiting for 1 minute",True)
gotify("Clipbot",f"An exception occurred in main loop: {str(e)} waiting for 1 minute","5")
time.sleep(60)

0 comments on commit 461686d

Please sign in to comment.