diff --git a/post.py b/post.py index b7fe0d9..1768113 100644 --- a/post.py +++ b/post.py @@ -5,8 +5,18 @@ from datetime import datetime, timezone, timedelta # formats embed for discord webhook and posts to url -def discord_remote_log(title,color,description): +def discord_remote_log(title,color,description): if use_discord_logs.lower() == "true": + if color == "blue": + color = 1523940 + elif color == "yellow": + color = 14081792 + elif color == "red": + color = 10159108 + elif color == "green": + color = 703235 + elif color == "purple": + color = 10622948 data_for_log_hook = {"embeds": [ { "title": title, @@ -15,6 +25,7 @@ def discord_remote_log(title,color,description): } ]} rl = requests.post(discord_remote_log_url, json=data_for_log_hook) + time.sleep(1) # loads needed data from config to variables with open("config/config.json") as config: # opens config and stores data in variables @@ -26,7 +37,7 @@ def discord_remote_log(title,color,description): if use_discord_logs.lower() == "true": discord_remote_log_url = str(config_json["discord_remote_log_url"]) print(" Succesfully loaded config") - discord_remote_log("Goinglivebot/post",14081792,"succesfully loaded config") + discord_remote_log("Goinglivebot/post","blue","succesfully loaded config") # main loop while True: @@ -36,8 +47,8 @@ def discord_remote_log(title,color,description): try: x = requests.post( remote_http_server_url, json = myobj) # sends post request print(" webhook response is: " + x.text) # log message - discord_remote_log("Goinglivebot/post",14081792,f"webhook response is: {x.text}") + discord_remote_log("Goinglivebot/post","purple",f"webhook response is: {x.text}") except Exception as e: # catches exception print(f"An exception occurred in main loop: {str(e)}") - discord_remote_log("Goinglivebot/post",10159108,f"An exception occurred in main loop: {str(e)}") + discord_remote_log("Goinglivebot/post","red",f"An exception occurred in main loop: {str(e)}") time.sleep(post_interval) \ No newline at end of file diff --git a/webserver.py b/webserver.py index c7bbe2e..e2546db 100644 --- a/webserver.py +++ b/webserver.py @@ -5,8 +5,18 @@ import requests # formats embed for discord webhook and posts to url -def discord_remote_log(title,color,description): +def discord_remote_log(title,color,description): if use_discord_logs.lower() == "true": + if color == "blue": + color = 1523940 + elif color == "yellow": + color = 14081792 + elif color == "red": + color = 10159108 + elif color == "green": + color = 703235 + elif color == "purple": + color = 10622948 data_for_log_hook = {"embeds": [ { "title": title, @@ -15,6 +25,7 @@ def discord_remote_log(title,color,description): } ]} rl = requests.post(discord_remote_log_url, json=data_for_log_hook) + time.sleep(1) # loads config into variables for use in script with open("config/config.json") as config: # opens config and stores data in variables @@ -25,22 +36,26 @@ def discord_remote_log(title,color,description): if use_discord_logs.lower() == "true": discord_remote_log_url = str(config_json["discord_remote_log_url"]) print(" Succesfully loaded config") - discord_remote_log("Goinglivebot/webserver",14081792,"succesfully loaded config") + discord_remote_log("Goinglivebot/webserver","blue","succesfully loaded config") # start webserver -class MyServer(BaseHTTPRequestHandler): - def do_GET(self): - self.send_response(200) - self.send_header("Content-type", "text/html") - self.end_headers() - self.wfile.write(bytes("https://pythonbasics.org", "utf-8")) - self.wfile.write(bytes("

Request: %s

" % self.path, "utf-8")) - self.wfile.write(bytes("", "utf-8")) - self.wfile.write(bytes("

Hello, i am a webserver.

", "utf-8")) - self.wfile.write(bytes("", "utf-8")) +try: + class MyServer(BaseHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write(bytes("https://pythonbasics.org", "utf-8")) + self.wfile.write(bytes("

Request: %s

" % self.path, "utf-8")) + self.wfile.write(bytes("", "utf-8")) + self.wfile.write(bytes("

Hello, i am a webserver.

", "utf-8")) + self.wfile.write(bytes("", "utf-8")) -if __name__ == "__main__": - webServer = HTTPServer((web_server_url, web_server_port), MyServer) - print(" Server started http://%s:%s" % (web_server_url, web_server_port)) - discord_remote_log("Goinglivebot/webserver",703235,"Server started http://%s:%s" % (web_server_url, web_server_port)) - webServer.serve_forever() \ No newline at end of file + if __name__ == "__main__": + webServer = HTTPServer((web_server_url, web_server_port), MyServer) + print(" Server started http://%s:%s" % (web_server_url, web_server_port)) + discord_remote_log("Goinglivebot/webserver","green","Server started http://%s:%s" % (web_server_url, web_server_port)) + webServer.serve_forever() +except Exception as e: + print(f"An exception occurred in main loop: {str(e)}") + discord_remote_log("Goinglivebot/webserver","red",f"An exception occurred in main loop: {str(e)}") \ No newline at end of file diff --git a/youtubelivebot.py b/youtubelivebot.py index 5b691f9..00b9564 100644 --- a/youtubelivebot.py +++ b/youtubelivebot.py @@ -15,22 +15,33 @@ # formats embed for discord webhook and posts to url def discord_log(title,color,description): if use_discord_logs.lower() == "true": - data = {"embeds": [ + if color == "blue": + color = 1523940 + elif color == "yellow": + color = 14081792 + elif color == "red": + color = 10159108 + elif color == "green": + color = 703235 + elif color == "purple": + color = 10622948 + data_for_log_hook = {"embeds": [ { "title": title, "color": color, "description": description } ]} - rl = requests.post(discord_log_webhook, json=data) + rl = requests.post(discord_log_webhook, json=data_for_log_hook) time.sleep(1) #pulls data from config -with open("config/config.json") as config: # opens config and stores values in variables +with open("config/config.json") as config: config_json = json.load(config) youtube_api_key = config_json["youtube_api_key"] discord_webhook_url = config_json["discord_webhook_url"] word_list = config_json["word_list"] + ignore_list = config_json["ignore_list"] notification_message = config_json["notification_message"] channels = config_json["channels"] use_web_server = config_json["use_web_server"] @@ -39,7 +50,7 @@ def discord_log(title,color,description): if use_discord_logs.lower() == "true": discord_log_webhook = config_json["discord_remote_log_url"] print("succesfully loaded config") -discord_log("Youtubelivebot",14081792,"succesfully loaded config") +discord_log("Youtubelivebot","blue","succesfully loaded config") #webserver for monitoring purposes if use_web_server.lower() == "true": @@ -48,7 +59,7 @@ def thread_second(): process_thread = threading.Thread(target=thread_second) process_thread.start() print("starting webserver for local monitoring") - discord_log("Youtubelivebot",14081792,"starting webserver for local monitoring") + discord_log("Youtubelivebot","blue","starting webserver for local monitoring") #post process to talk to remote monitor if use_remote_post.lower() == "true": @@ -57,54 +68,53 @@ def thread_third(): process_thread = threading.Thread(target=thread_third) process_thread.start() print("starting post server for remote monitoring") - discord_log("Youtubelivebot",14081792,"starting post server for remote monitoring") + discord_log("Youtubelivebot","blue","starting post server for remote monitoring") # checks if name of video contains pursuit and if so posts video to webhook while True: try: # calculates minimum time between api calls to avoid rate limiting - channel_count= 0 - for channel in channels: - channel_count= channel_count + 1 - time_to_poll= 1440 / channel_count / 100 - time_to_poll= math.ceil(time_to_poll) + time_to_poll= math.ceil(1440 * len(channels) / 100) time_to_sleep = time_to_poll * 60 - print(f"time between polls is {str(time_to_poll)} minutes") - discord_log("Youtubelivebot",14081792,f"time between polls is {str(time_to_poll)} minutes") + 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") # 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",14081792,f"polling channel {channel}") + discord_log("Youtubelivebot","green",f"polling channel {channel}") 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"request response is {str(request)}") - discord_log("Youtubelivebot",14081792,f"request response is {str(request)}") + 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)}") yt_api_json = request.json() - item_count= 0 - for item in yt_api_json["items"]: - item_count = item_count + 1 + try: + item_count = len(yt_api_json["items"]) + except: + item_count = 0 if item_count > 0: for item in yt_api_json["items"]: - # checks if videos found contain keywords - if any(s in (item["snippet"]["title"].lower()) for s in word_list): + # checks if videos found contain keywords and do not contain ignored words + if any(s in (item["snippet"]["title"].lower()) for s in word_list) and all(s not in (item["snippet"]["title"].lower()) for s in ignore_list): video_id_to_send = item["id"]["videoId"] if video_id_to_send not in l: l.append(video_id_to_send) - print(f"posting video with id: {video_id_to_send}") - discord_log("Youtubelivebot",703235,f"posting video with id: {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}") 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 no pursuit on channel {channel}") - discord_log("Youtubelivebot",14081792,f"Live video found but no pursuit on channel {channel}") + 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}") else: - print(f" no live videos found for channel {channel}") - discord_log("Youtubelivebot",14081792,f"no live videos found for channel {channel}") - print(f" waiting for {str(time_to_poll)} minutes") - discord_log("Youtubelivebot",14081792,f"waiting for {str(time_to_poll)} minutes") + print(f"no live videos found for channel {channel}") + discord_log("Youtubelivebot","yellow",f"no live videos found for channel {channel}") + 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") time.sleep(time_to_sleep) - except Exception as e: # catches exception + except Exception as e: print(f"An exception occurred in main loop: {str(e)} waiting for 1 minute") - discord_log("youtubelivebot",10159108,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") time.sleep(60) \ No newline at end of file