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 time
|
||||||
import configparser
|
import configparser
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
def json_to_key_value_string(json_data):
|
def json_to_key_value_string(json_data):
|
||||||
# Parse JSON data
|
# Parse JSON data
|
||||||
|
@ -38,7 +39,7 @@ class QBittorrentAPICaller():
|
||||||
self.username = username
|
self.username = username
|
||||||
self.password = password
|
self.password = password
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.login()
|
self.reestablish_session()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.logout()
|
self.logout()
|
||||||
|
@ -56,6 +57,14 @@ class QBittorrentAPICaller():
|
||||||
|
|
||||||
return to_ret
|
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):
|
def truncate_string(self, s, max_length=25):
|
||||||
return s[:max_length] + '...' if len(s) > max_length else s
|
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 + ")"
|
return "Could not add torrent, please double check the magnet link (hash=" + magnet_hash + ")"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class QBBot(slixmpp.ClientXMPP):
|
class QBBot(slixmpp.ClientXMPP):
|
||||||
|
|
||||||
def __init__(self, jid, password, room, nick, api_url, api_username, api_password):
|
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,
|
# Ideally use optparse or argparse to get JID,
|
||||||
# password, and log level.
|
# password, and log level.
|
||||||
|
|
||||||
logging.basicConfig(level=logging.ERROR,
|
logging.basicConfig(level=logging.INFO,
|
||||||
format='%(levelname)-8s %(message)s')
|
format='%(levelname)-8s %(message)s')
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
|
|
Loading…
Reference in New Issue