Skip to content

Commit

Permalink
Merge pull request #203 from ManiMatter/refresh-qbit-cookie
Browse files Browse the repository at this point in the history
Added Cookie Refresh on every run
  • Loading branch information
ManiMatter authored Dec 6, 2024
2 parents 6cc8e58 + 86eb3ce commit 568fd69
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 101 deletions.
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ async def main(settingsDict):
# Start Cleaning
while True:
logger.verbose("-" * 50)

# Refresh qBit Cookie
if settingsDict["QBITTORRENT_URL"]:
await qBitRefreshCookie(settingsDict)
if not settingsDict["QBIT_COOKIE"]:
logger.error("Cookie Refresh failed - exiting decluttarr")
exit()

# Cache protected (via Tag) and private torrents
protectedDownloadIDs, privateDowloadIDs = await getProtectedAndPrivateFromQbit(
settingsDict
Expand Down
14 changes: 3 additions & 11 deletions src/utils/loadScripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dateutil.relativedelta import relativedelta as rd
import requests
from src.utils.rest import rest_get, rest_post #
from src.utils.shared import qBitRefreshCookie
import asyncio
from packaging import version

Expand Down Expand Up @@ -189,18 +190,9 @@ async def instanceChecks(settingsDict):
# Check Bittorrent
if settingsDict['QBITTORRENT_URL']:
# Checking if qbit can be reached, and checking if version is OK
try:
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.post(settingsDict['QBITTORRENT_URL']+'/auth/login', data={'username': settingsDict['QBITTORRENT_USERNAME'], 'password': settingsDict['QBITTORRENT_PASSWORD']}, headers={'content-type': 'application/x-www-form-urlencoded'}, verify=settingsDict['SSL_VERIFICATION']))
if response.text == 'Fails.':
raise ConnectionError('Login failed.')
response.raise_for_status()
settingsDict['QBIT_COOKIE'] = {'SID': response.cookies['SID']}
except Exception as error:
await qBitRefreshCookie(settingsDict)
if not settingsDict['QBIT_COOKIE']:
error_occured = True
logger.error('!! %s Error: !!', 'qBittorrent')
logger.error('> %s', error)
logger.error('> Details:')
logger.error(response.text)

if not error_occured:
qbit_version = await rest_get(settingsDict['QBITTORRENT_URL']+'/app/version',cookies=settingsDict['QBIT_COOKIE'])
Expand Down
89 changes: 0 additions & 89 deletions src/utils/main.py

This file was deleted.

18 changes: 17 additions & 1 deletion src/utils/shared.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Shared Functions
import logging, verboselogs

import asyncio
import requests
logger = verboselogs.VerboseLogger(__name__)
from src.utils.rest import rest_get, rest_delete, rest_post
from src.utils.nest_functions import add_keys_nested_dict, nested_get
Expand Down Expand Up @@ -398,3 +399,18 @@ async def qBitOffline(settingsDict, failType, NAME):
)
return True
return False

async def qBitRefreshCookie(settingsDict):
try:
response = await asyncio.get_event_loop().run_in_executor(None, lambda: requests.post(settingsDict['QBITTORRENT_URL']+'/auth/login', data={'username': settingsDict['QBITTORRENT_USERNAME'], 'password': settingsDict['QBITTORRENT_PASSWORD']}, headers={'content-type': 'application/x-www-form-urlencoded'}, verify=settingsDict['SSL_VERIFICATION']))
if response.text == 'Fails.':
raise ConnectionError('Login failed.')
response.raise_for_status()
settingsDict['QBIT_COOKIE'] = {'SID': response.cookies['SID']}
logger.debug('qBit cookie refreshed!')
except Exception as error:
logger.error('!! %s Error: !!', 'qBittorrent')
logger.error('> %s', error)
logger.error('> Details:')
logger.error(response.text)
settingsDict['QBIT_COOKIE'] = {}

0 comments on commit 568fd69

Please sign in to comment.