|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 | 4 | import logging
|
| 5 | +import requests |
5 | 6 | from config import Conf
|
6 | 7 | from bencoder import bdecode
|
7 | 8 | from urllib.parse import urljoin
|
@@ -39,38 +40,41 @@ def main():
|
39 | 40 | rss_url = f'{rss_url}{config.auth[tracker]["passkey"]}'
|
40 | 41 | config.tracker_ids[tracker] = rss_parser(rss_url, config.tracker_ids[tracker])
|
41 | 42 | for tracker in config.tracker_ids.keys():
|
42 |
| - for topic_id in config.tracker_ids[tracker]: |
43 |
| - current_torrent = config.client.get_torrent_by_topic(tracker, topic_id) |
44 |
| - fresh_tracker = trackers[tracker]['incarnation']( |
45 |
| - auth=config.auth[tracker], |
46 |
| - topic_id=topic_id, |
47 |
| - session=sessions[tracker] |
48 |
| - ) |
49 |
| - current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower() |
50 |
| - if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint: |
51 |
| - new_torrent = fresh_tracker.download_torrent() |
52 |
| - new_torrent_name = bdecode(new_torrent)[b'info'][b'name'].decode('UTF-8') |
53 |
| - if not sessions[tracker] and fresh_tracker.session: |
54 |
| - sessions[tracker] = fresh_tracker.session |
55 |
| - data = { |
56 |
| - 'category': current_torrent['category'], |
57 |
| - 'tags': current_torrent['tags'], |
58 |
| - 'path': current_torrent['save_path'], |
59 |
| - 'state': current_torrent['state'], |
60 |
| - 'tracker': tracker, |
61 |
| - 'topic_id': topic_id, |
62 |
| - } |
63 |
| - config.client.remove_torrent(current_torrent['hash']) |
64 |
| - logging.info( |
65 |
| - f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. ' |
66 |
| - f'Updating the torrent — {current_torrent["name"]}' |
| 43 | + response = requests.get(config.auth[tracker]['url']) |
| 44 | + tracker_status = response.status_code |
| 45 | + if tracker_status == 200: |
| 46 | + for topic_id in config.tracker_ids[tracker]: |
| 47 | + current_torrent = config.client.get_torrent_by_topic(tracker, topic_id) |
| 48 | + fresh_tracker = trackers[tracker]['incarnation']( |
| 49 | + auth=config.auth[tracker], |
| 50 | + topic_id=topic_id, |
| 51 | + session=sessions[tracker] |
67 | 52 | )
|
68 |
| - config.client.add_torrent(torrent=new_torrent, data=data) |
69 |
| - if new_torrent_name != current_torrent['name']: |
70 |
| - logging.warning( |
71 |
| - f'The torrent name has changed: {current_torrent["name"]} → {new_torrent_name}. ' |
72 |
| - f'Duplicate files may appear.' |
| 53 | + current_fingerprint = str(current_torrent[trackers[tracker]['fingerprint']]).lower() |
| 54 | + if current_fingerprint != fresh_tracker.fingerprint.lower() and fresh_tracker.fingerprint: |
| 55 | + new_torrent = fresh_tracker.download_torrent() |
| 56 | + new_torrent_name = bdecode(new_torrent)[b'info'][b'name'].decode('UTF-8') |
| 57 | + if not sessions[tracker] and fresh_tracker.session: |
| 58 | + sessions[tracker] = fresh_tracker.session |
| 59 | + data = { |
| 60 | + 'category': current_torrent['category'], |
| 61 | + 'tags': current_torrent['tags'], |
| 62 | + 'path': current_torrent['save_path'], |
| 63 | + 'state': current_torrent['state'], |
| 64 | + 'tracker': tracker, |
| 65 | + 'topic_id': topic_id, |
| 66 | + } |
| 67 | + config.client.remove_torrent(current_torrent['hash']) |
| 68 | + logging.info( |
| 69 | + f'The torrent {trackers[tracker]["fingerprint"]} in topic {fresh_tracker.topic_url} has changed. ' |
| 70 | + f'Updating the torrent — {current_torrent["name"]}' |
73 | 71 | )
|
| 72 | + config.client.add_torrent(torrent=new_torrent, data=data) |
| 73 | + if new_torrent_name != current_torrent['name']: |
| 74 | + logging.warning( |
| 75 | + f'The torrent name has changed: {current_torrent["name"]} → {new_torrent_name}. ' |
| 76 | + f'Duplicate files may appear.' |
| 77 | + ) |
74 | 78 |
|
75 | 79 |
|
76 | 80 | if __name__ == '__main__':
|
|
0 commit comments