From afd5e895c23e5cb854b3551f14919604c318a17d Mon Sep 17 00:00:00 2001 From: j4nk Date: Sun, 28 Jul 2024 17:52:17 -0400 Subject: [PATCH] Added connection reestablishment every 10 minutes --- bot.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 4c1b7ef..b53b92e 100644 --- a/bot.py +++ b/bot.py @@ -7,6 +7,7 @@ import datetime import time import configparser import os +import threading def json_to_key_value_string(json_data): # Parse JSON data @@ -38,7 +39,7 @@ class QBittorrentAPICaller(): self.username = username self.password = password self.session = requests.Session() - self.login() + self.reestablish_session() def __del__(self): self.logout() @@ -56,6 +57,14 @@ class QBittorrentAPICaller(): return to_ret + # We need to reestablish a connection to the server periodically, this does that + def reestablish_session(self): + # Every 10 minutes, run this + threading.Timer(600, self.reestablish_session).start() + self.logout() + self.login() + logging.info("Reestablished connection to QBittorrent server") + def truncate_string(self, s, max_length=25): return s[:max_length] + '...' if len(s) > max_length else s @@ -141,8 +150,6 @@ class QBittorrentAPICaller(): return "Successfully added " + p["name"] return "Could not add torrent, please double check the magnet link (hash=" + magnet_hash + ")" - - class QBBot(slixmpp.ClientXMPP): @@ -286,7 +293,7 @@ if __name__ == '__main__': # Ideally use optparse or argparse to get JID, # password, and log level. - logging.basicConfig(level=logging.ERROR, + logging.basicConfig(level=logging.INFO, format='%(levelname)-8s %(message)s') config = configparser.ConfigParser()