Skip to content

Commit

Permalink
feat: support "reannounce"
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoux committed Mar 16, 2022
1 parent 7b2b95b commit a578b90
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
18 changes: 18 additions & 0 deletions autoremovetorrents/client/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def delete_torrents(self, torrent_hash_list):
def delete_torrents_and_data(self, torrent_hash_list):
return self._session.get(self._host+'/api/v2/torrents/delete', params={'hashes':'|'.join(torrent_hash_list), 'deleteFiles': True})

# Batch Reannounce torrents
def reannounce_torrents(self, torrent_hash_list):
return self._session.get(self._host+'/api/v2/torrents/reannounce', params={'hashes':'|'.join(torrent_hash_list)})


def __init__(self, host):
# Logger
self._logger = logger.Logger.register(__name__)
Expand Down Expand Up @@ -282,3 +287,16 @@ def remove_torrents(self, torrent_hash_list, remove_data):
# Some of them may fail but we can't judge them,
# So we consider all of them as successful.
return (torrent_hash_list, [])

# Batch Reannounce Torrents
# Return values: (success_hash_list, failed_list -> {hash: reason, ...})
def reannounce_torrents(self, torrent_hash_list):
request = self._request_handler.reannounce_torrents(torrent_hash_list)
if request.status_code != 200:
return ([], [{
'hash': torrent,
'reason': 'The server responses HTTP %d.' % request.status_code,
} for torrent in torrent_hash_list])
# Some of them may fail but we can't judge them,
# So we consider all of them as successful.
return (torrent_hash_list, [])
15 changes: 14 additions & 1 deletion autoremovetorrents/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(self, name, conf):
# Results
self.remain_list = set()
self.remove_list = set()
self.reannounce_list = set()

# Filter ALL
self._all_categories = conf['all_categories'] if 'all_categories' in conf \
Expand Down Expand Up @@ -129,6 +130,7 @@ def _apply_conditions(self, client_status):
'seeding_time': SeedingTimeCondition,
'max_size': SizeCondition,
'upload_ratio': UploadRatioCondition,
'reannounce' : ConditionParser,
}
for conf in self._conf:
if conf in conditions:
Expand All @@ -150,7 +152,11 @@ def _apply_conditions(self, client_status):

# Output
self.remain_list = cond.remain
self.remove_list.update(cond.remove)
if conf == 'reannounce':
self.reannounce_list.update(cond.remove)
else:
self.remove_list.update(cond.remove)


# Print updated list to debug log
self._logger.debug('OUTPUT: %d torrent(s) to be reserved after applying the condition.' % len(self.remain_list))
Expand All @@ -159,6 +165,9 @@ def _apply_conditions(self, client_status):
self._logger.debug('OUTPUT: %d torrent(s) to be removed after applying the condition.' % len(self.remove_list))
for torrent in self.remove_list:
self._logger.debug(torrent)
self._logger.info('OUTPUT: %d torrent(s) to be reannounced after applying the condition.' % len(self.reannounce_list))
for torrent in self.reannounce_list:
self._logger.debug(torrent)

# Execute this strategy
def execute(self, client_status, torrents):
Expand All @@ -174,4 +183,8 @@ def execute(self, client_status, torrents):
if len(self.remove_list) > 0:
self._logger.info('To be deleted:')
for torrent in self.remove_list:
self._logger.info(torrent)
if len(self.reannounce_list) > 0:
self._logger.info('To be reannounced:')
for torrent in self.reannounce_list:
self._logger.info(torrent)
24 changes: 23 additions & 1 deletion autoremovetorrents/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, name, conf, remove_torrents = True):
# Torrents
self._torrents = set()
self._remove = set()
self._reannounce = set()

# Client status
self._client_status = None
Expand Down Expand Up @@ -111,6 +112,7 @@ def _apply_strategies(self):
strategy = Strategy(strategy_name, self._strategies[strategy_name])
strategy.execute(self._client_status, self._torrents)
self._remove.update(strategy.remove_list)
self._reannounce.update(strategy.reannounce_list)

# Remove torrents
def _remove_torrents(self):
Expand All @@ -133,14 +135,34 @@ def _remove_torrents(self):
delete_list[torrent['hash']], torrent['reason']
)

# Reannounce torrents
def _reannounce_torrents(self):
# Bulid a dict to store torrent hashes and names which to be deleted
reannounce_list = {}
for torrent in self._reannounce:
reannounce_list[torrent.hash] = torrent.name
# Run deletion
success, failed = self._client.reannounce_torrents([hash_ for hash_ in reannounce_list])
# Output logs
for hash_ in success:
self._logger.info(
'The torrent %s have been reannounced.' ,
reannounce_list[hash_]
)
for torrent in failed:
self._logger.error('The torrent %s cannot be reannounced. Reason: %s' ,
reannounce_list[torrent['hash']], torrent['reason']
)

# Execute
def execute(self):
self._logger.info("Running task '%s'..." % self._name)
self._logger.info(" task '%s'..." % self._name)
self._login()
self._get_torrents()
self._apply_strategies()
if self._enabled_remove:
self._remove_torrents()
self._reannounce_torrents()

# Get remaining torrents (for tester)
def get_remaining_torrents(self):
Expand Down

0 comments on commit a578b90

Please sign in to comment.