Skip to content

Commit 7265e59

Browse files
authored
Merge pull request #48 from XanderStrike/retry-fl-tokens
retry on fl token failure
2 parents 87fdf8c + 00df958 commit 7265e59

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

lib/torrent.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ def download_all():
3030
discord.send('Downloaded ' + t[1] + ' - ' + t[2] + ' / ' + t[3] + ' / ' + t[4])
3131

3232
def download_torrent(torrent_id):
33-
download_link = wat.download_link(torrent_id)
3433
download_path = settings.get('torrent')[1]
3534
save_to = download_path + torrent_id + ".torrent"
3635

3736
opener = urllib.request.build_opener()
3837
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
39-
torrent = opener.open(download_link).read()
38+
39+
# First attempt with token (if enabled in settings)
40+
download_url = wat.download_link(torrent_id)
41+
torrent = opener.open(download_url).read()
42+
43+
# Check if we ran out of tokens, retry without token
44+
if b'You do not have any freeleech tokens left' in torrent:
45+
print("No freeleech tokens left, retrying without token")
46+
download_url = wat.download_link(torrent_id, use_token=False)
47+
torrent = opener.open(download_url).read()
4048

4149
output = open(save_to,'wb')
4250
output.write(torrent)

lib/wat.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ def get_group(group_id):
7979
except:
8080
return "no data"
8181

82-
def download_link(torrent_id):
82+
def download_link(torrent_id, use_token=None):
8383
domain = settings.get('domain')[1]
84-
use_tokens_setting = settings.get('use_tokens')
85-
token_count = use_tokens_setting[2] if use_tokens_setting[2] else '0'
86-
use_tokens = use_tokens_setting[1] == 'true' and int(token_count) > 0
84+
85+
# If use_token is explicitly specified, use that; otherwise check settings
86+
if use_token is None:
87+
use_tokens_setting = settings.get('use_tokens')
88+
token_count = use_tokens_setting[2] if use_tokens_setting[2] else '0'
89+
use_token = use_tokens_setting[1] == 'true' and int(token_count) > 0
8790

8891
url = domain + '/torrents.php?action=download&id=' + torrent_id + '&authkey=' + handle().authkey + '&torrent_pass=' + handle().passkey
89-
if use_tokens:
92+
if use_token:
9093
url += '&usetoken=1'
9194

9295
print(url)

0 commit comments

Comments
 (0)