Skip to content

Commit

Permalink
Add last activity condition
Browse files Browse the repository at this point in the history
Reference to #21, closes #39, #42.
  • Loading branch information
jerrymakesjelly committed Sep 22, 2019
1 parent 1fab863 commit 5ff1de2
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion autoremovetorrents/client/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def torrent_properties(self, torrent_hash):
# Get other information
properties = self._torrent_generic_properties(torrent_hash)
trackers = self._torrent_trackers(torrent_hash)
# For qBittorrent 3.x, the last activity field doesn't exist.
# We use current timestamp to avoid erroneous deletion.
last_activity = int(time.time())
if 'last_activity' in torrent:
last_activity = torrent['last_activity']
return Torrent(
torrent['hash'], torrent['name'],
torrent['category'] if 'category' in torrent else torrent['label'],
Expand All @@ -77,7 +82,7 @@ def torrent_properties(self, torrent_hash):
torrent['state'] == 'stalledUP' or torrent['state'] == 'stalledDL',
torrent['size'], torrent['ratio'],
properties['total_uploaded'], properties['addition_date'],
properties['seeding_time'])
properties['seeding_time'], last_activity)

# Judge Torrent Status (qBittorrent doesn't have stopped status)
@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions autoremovetorrents/client/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def torrents_list(self):
def torrent_properties(self, torrent_hash):
result = self._make_transmission_request('torrent-get',
{'ids': [torrent_hash],
'fields': ['hashString', 'name', 'trackers', 'status', 'totalSize', 'uploadRatio', 'uploadedEver', 'addedDate', 'secondsSeeding', 'isStalled']}
'fields': ['hashString', 'name', 'trackers', 'status', 'totalSize', 'uploadRatio', 'uploadedEver', 'addedDate', 'secondsSeeding', 'isStalled', 'activityDate']}
)
if len(result['torrents']) == 0: # No such torrent
raise NoSuchClient("No such torrent of hash '%s'." % torrent_hash)
Expand All @@ -82,7 +82,7 @@ def torrent_properties(self, torrent_hash):
Transmission._judge_status(torrent['status']),
torrent['isStalled'],
torrent['totalSize'], torrent['uploadRatio'],
torrent['uploadedEver'], torrent['addedDate'], torrent['secondsSeeding'])
torrent['uploadedEver'], torrent['addedDate'], torrent['secondsSeeding'], torrent['activityDate'])

# Judge Torrent Status
@staticmethod
Expand Down
4 changes: 3 additions & 1 deletion autoremovetorrents/client/utorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def torrent_properties(self, torrent_hash):
torrent[0], torrent[2], torrent[11], trackers, uTorrent._judge_status(torrent[1], torrent[4]),
False, # uTorrent never has stall status
torrent[3], torrent[7]/1000,
torrent[6], sys.maxsize, -1)
torrent[6], sys.maxsize, -1,
int(time.time()) # uTorrent doesn't have last activity time field
)
# Not Found
raise NoSuchTorrent('No such torrent.')

Expand Down
16 changes: 16 additions & 0 deletions autoremovetorrents/condition/lastactivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import time
from .base import Comparer
from .base import Condition

class LastActivityCondition(Condition):
def __init__(self, la, comp = Comparer.GT):
Condition.__init__(self)
self._last_activity = la
self._comparer = comp

def apply(self, torrents):
for torrent in torrents:
if self.compare(time.time() - torrent.last_activity, self._last_activity, self._comparer):
self.remove.add(torrent)
else:
self.remain.add(torrent)
4 changes: 3 additions & 1 deletion autoremovetorrents/conditionparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .condition.createtime import CreateTimeCondition
from .condition.ratio import RatioCondition
from .condition.seedingtime import SeedingTimeCondition
from .condition.lastactivity import LastActivityCondition
from .exception.nosuchcondition import NoSuchCondition
from .exception.syntaxerror import ConditionSyntaxError

Expand All @@ -13,7 +14,8 @@ class ConditionParser(object):
_condition_map = {
'create_time': CreateTimeCondition,
'ratio': RatioCondition,
'seeding_time': SeedingTimeCondition
'seeding_time': SeedingTimeCondition,
'last_activity': LastActivityCondition
}

# Condition expression
Expand Down
2 changes: 2 additions & 0 deletions autoremovetorrents/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .condition.ratio import RatioCondition
from .condition.torrentsize import TorrentSizeCondition
from .condition.torrentnumber import TorrentNumberCondition
from .condition.lastactivity import LastActivityCondition
from .condition.donothing import EmptyCondition
from .conditionparser import ConditionParser

Expand Down Expand Up @@ -66,6 +67,7 @@ def _apply_conditions(self):
'ratio': RatioCondition,
'seed_size': TorrentSizeCondition,
'maximum_number': TorrentNumberCondition,
'last_activity': LastActivityCondition,
'nothing': EmptyCondition
}
for conf in self._conf:
Expand Down
3 changes: 2 additions & 1 deletion autoremovetorrents/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Torrent(object):
def __init__(self, hash_value, name, category, tracker, status, stalled, size, ratio,
uploaded, create_time, seeding_time):
uploaded, create_time, seeding_time, last_activity):
# Save Properties
self.hash = hash_value
self.name = name
Expand All @@ -20,6 +20,7 @@ def __init__(self, hash_value, name, category, tracker, status, stalled, size, r
self.uploaded = uploaded
self.create_time = create_time
self.seeding_time = seeding_time
self.last_activity = last_activity

# Format torrent info
def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test:
last_activity: 700000
remove:
- Torrent - 4
- Torrent - 9
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
remove: last_activity > 800000
remove:
- Torrent - 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
remove: last_activity < 100000
remove:
- Torrent - 13
- Torrent - 14
- Torrent - 15
- Torrent - 16
3 changes: 2 additions & 1 deletion pytest/test_strategies/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def test_data():
torrent['ratio'],
torrent['uploaded'],
torrent['added_on'],
torrent['seeding_time']
torrent['seeding_time'],
torrent['last_activity']
))

return input_torrents
Expand Down

0 comments on commit 5ff1de2

Please sign in to comment.