Skip to content

Commit

Permalink
add tracker's status checking
Browse files Browse the repository at this point in the history
  • Loading branch information
konkere committed Jul 18, 2024
1 parent 265b68b commit 5989638
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
File renamed without changes
File renamed without changes.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Konkere
Copyright (c) 2024 Konkere

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Torrent updater](/img/TorrUpd.jpg)
![Torrent updater](/.github/img/TorrUpd.jpg)
Tool for automatically checking the relevance of torrents and updating them in the torrent client.

Supports trackers: RuTracker and NNM-Club (hash comparison in topics) and TeamHD (torrent size comparison in RSS, login problem due to reCaptcha).
Expand Down
64 changes: 34 additions & 30 deletions torrent_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

import logging
import requests
from config import Conf
from bencoder import bdecode
from urllib.parse import urljoin
Expand Down Expand Up @@ -39,38 +40,41 @@ def main():
rss_url = f'{rss_url}{config.auth[tracker]["passkey"]}'
config.tracker_ids[tracker] = rss_parser(rss_url, config.tracker_ids[tracker])
for tracker in config.tracker_ids.keys():
for topic_id in config.tracker_ids[tracker]:
current_torrent = config.client.get_torrent_by_topic(tracker, topic_id)
fresh_tracker = trackers[tracker]['incarnation'](
auth=config.auth[tracker],
topic_id=topic_id,
session=sessions[tracker]
)
current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower()
if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint:
new_torrent = fresh_tracker.download_torrent()
new_torrent_name = bdecode(new_torrent)[b'info'][b'name'].decode('UTF-8')
if not sessions[tracker] and fresh_tracker.session:
sessions[tracker] = fresh_tracker.session
data = {
'category': current_torrent['category'],
'tags': current_torrent['tags'],
'path': current_torrent['save_path'],
'state': current_torrent['state'],
'tracker': tracker,
'topic_id': topic_id,
}
config.client.remove_torrent(current_torrent['hash'])
logging.info(
f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. '
f'Updating the torrent — {current_torrent["name"]}'
response = requests.get(config.auth[tracker]['url'])
tracker_status = response.status_code
if tracker_status == 200:
for topic_id in config.tracker_ids[tracker]:
current_torrent = config.client.get_torrent_by_topic(tracker, topic_id)
fresh_tracker = trackers[tracker]['incarnation'](
auth=config.auth[tracker],
topic_id=topic_id,
session=sessions[tracker]
)
config.client.add_torrent(torrent=new_torrent, data=data)
if new_torrent_name != current_torrent['name']:
logging.warning(
f'The torrent name has changed: {current_torrent["name"]}{new_torrent_name}. '
f'Duplicate files may appear.'
current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower()
if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint:
new_torrent = fresh_tracker.download_torrent()
new_torrent_name = bdecode(new_torrent)[b'info'][b'name'].decode('UTF-8')
if not sessions[tracker] and fresh_tracker.session:
sessions[tracker] = fresh_tracker.session
data = {
'category': current_torrent['category'],
'tags': current_torrent['tags'],
'path': current_torrent['save_path'],
'state': current_torrent['state'],
'tracker': tracker,
'topic_id': topic_id,
}
config.client.remove_torrent(current_torrent['hash'])
logging.info(
f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. '
f'Updating the torrent — {current_torrent["name"]}'
)
config.client.add_torrent(torrent=new_torrent, data=data)
if new_torrent_name != current_torrent['name']:
logging.warning(
f'The torrent name has changed: {current_torrent["name"]}{new_torrent_name}. '
f'Duplicate files may appear.'
)


if __name__ == '__main__':
Expand Down

0 comments on commit 5989638

Please sign in to comment.