Added connection reestablishment every 10 minutes
This commit is contained in:
parent
197e30bdf7
commit
afd5e895c2
15
bot.py
15
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()
|
||||
|
|
Loading…
Reference in New Issue