Added connection reestablishment every 10 minutes

This commit is contained in:
j4nk 2024-07-28 17:52:17 -04:00
parent 197e30bdf7
commit afd5e895c2
1 changed files with 11 additions and 4 deletions

15
bot.py
View File

@ -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
@ -143,8 +152,6 @@ class QBittorrentAPICaller():
return "Could not add torrent, please double check the magnet link (hash=" + magnet_hash + ")"
class QBBot(slixmpp.ClientXMPP):
def __init__(self, jid, password, room, nick, api_url, api_username, api_password):
@ -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()