Skip to content

Commit b3f702c

Browse files
committed
save cookies to reduce impact of login bug
1 parent 18e9ccf commit b3f702c

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

pythonbits/tracker.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
import requests
32
import contextlib
43
import re
4+
import os
55
import time
6+
from http.cookiejar import MozillaCookieJar
7+
8+
import requests
9+
import appdirs
610

711
from . import __version__ as version, __title__ as title
812
from .config import config
@@ -24,7 +28,7 @@ class Tracker():
2428
headers = {'User-Agent': '{}/{}'.format(title, version)}
2529

2630
def _login(self, session, _tries=0):
27-
maxtries = 10
31+
maxtries = 30
2832

2933
domain = config.get('Tracker', 'domain')
3034
login_url = "https://{}/login.php".format(domain)
@@ -38,7 +42,7 @@ def _login(self, session, _tries=0):
3842

3943
if 'href="login.php"' in resp.text:
4044
if 'id="loginform"' in resp.text:
41-
raise TrackerException("Login failed")
45+
raise TrackerException("Login failed (wrong credentials?)")
4246
elif 'You are banned' in resp.text:
4347
raise TrackerException(
4448
"Login failed (login attempts exceeded)")
@@ -47,16 +51,17 @@ def _login(self, session, _tries=0):
4751
# doesn't contain the login form) without logging you in
4852
# todo: convert this to a retry decorator
4953
if _tries < maxtries:
50-
backoff = min(10**_tries/1000, 16.)
51-
log.debug('Encountered login bug; trying again after '
54+
backoff = min(2**_tries/100, 5.)
55+
log.info('Encountered login bug; trying again after '
5256
'{}s back-off'.format(backoff))
57+
#breakpoint()
5358
time.sleep(backoff)
5459
self._login(session, _tries=_tries+1)
5560
else:
56-
log.debug('Encountered login bug; '
57-
'giving up after '
58-
'{} login attempts'.format(_tries))
59-
raise TrackerException("Login failed")
61+
log.notice('Encountered login bug; '
62+
'giving up after '
63+
'{} login attempts'.format(_tries))
64+
raise TrackerException("Login failed (server login bug)")
6065
elif 'logout.php' in resp.text:
6166
# Login successful, find and remember logout URL
6267
match = re.search(r"logout\.php\?auth=[0-9a-f]{32}", resp.text)
@@ -84,11 +89,19 @@ def login(self):
8489
log.notice("Logging in {} to {}",
8590
config.get('Tracker', 'username'),
8691
config.get('Tracker', 'domain'))
92+
cj_path = os.path.join(appdirs.user_cache_dir(title.lower()),
93+
'tracker_cookies.txt')
8794
with requests.Session() as s:
95+
s.cookies = MozillaCookieJar(cj_path)
96+
try:
97+
s.cookies.load()
98+
except FileNotFoundError:
99+
s.cookies.save()
88100
s.headers.update(self.headers)
89101
self._login(s)
90102
yield s
91103
self._logout(s)
104+
s.cookies.save()
92105
log.notice("Logged out {} of ",
93106
config.get('Tracker', 'username'),
94107
config.get('Tracker', 'domain'))

0 commit comments

Comments
 (0)