Skip to content

Commit

Permalink
feat: add path filter for Transmission (#145)
Browse files Browse the repository at this point in the history
Closes #103. Closes #105. Ref #21.
  • Loading branch information
zhangnew authored Jul 26, 2022
1 parent dba0f79 commit 69901e0
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 212 deletions.
2 changes: 2 additions & 0 deletions autoremovetorrents/client/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def torrent_properties(self, torrent_hash):
'downloadedEver',
'secondsDownloading',
'percentDone',
'downloadDir',
]}
)
if len(result['torrents']) == 0: # No such torrent
Expand All @@ -135,6 +136,7 @@ def torrent_properties(self, torrent_hash):
torrent_obj.ratio = torrent['uploadRatio']
torrent_obj.uploaded = torrent['uploadedEver']
torrent_obj.downloaded = torrent['downloadedEver']
torrent_obj.download_dir = torrent['downloadDir']
torrent_obj.create_time = torrent['addedDate']
torrent_obj.seeding_time = torrent['secondsSeeding']
torrent_obj.downloading_time = torrent['secondsDownloading']
Expand Down
34 changes: 34 additions & 0 deletions autoremovetorrents/filter/path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#-*- coding:utf-8 -*-

from .filter import Filter

class PathFilter(Filter):
def __init__(self, all_paths, ac, re):
'''
filter torrents by path (download directory)
Parameters:
all_paths (boolean): whether accept all paths
ac (list): accept paths
re (list): reject paths
'''
Filter.__init__(self, all_paths, ac, re)

def apply(self, torrents):
# Pick accepted torrents
accepts = set()
if self._all: # Accept all torrents (all_paths)
accepts = set(torrents)
elif len(self._accept) > 0: # Accept specific path torrents (paths)
for torrent in torrents:
path = torrent.download_dir
if any(map(path.startswith, self._accept)) or path == '':
accepts.add(torrent)
# Pick rejected torrents
rejects = set()
if len(self._reject) > 0: # Reject specific path torrents (excluded_paths)
for torrent in accepts:
path = torrent.download_dir
if any(map(path.startswith, self._reject)):
rejects.add(torrent)
return accepts.difference(rejects) # Return their difference
14 changes: 9 additions & 5 deletions autoremovetorrents/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .filter.tag import TagFilter
from .filter.status import StatusFilter
from .filter.tracker import TrackerFilter
from .filter.path import PathFilter

class Strategy(object):
def __init__(self, name, conf):
Expand All @@ -47,13 +48,15 @@ def __init__(self, name, conf):

# Filter ALL
self._all_categories = conf['all_categories'] if 'all_categories' in conf \
else not 'categories' in conf
else 'categories' not in conf
self._all_tags = conf['all_tags'] if 'all_tags' in conf \
else not 'tags' in conf
else 'tags' not in conf
self._all_trackers = conf['all_trackers'] if 'all_trackers' in conf \
else not 'trackers' in conf
else 'trackers' not in conf
self._all_status = conf['all_status'] if 'all_status' in conf \
else not 'status' in conf
else 'status' not in conf
self._all_paths = conf['all_paths'] if 'all_paths' in conf \
else 'paths' not in conf

# Print debug log
self._logger.debug("Configuration of strategy '%s':" % self._name)
Expand All @@ -66,8 +69,9 @@ def _apply_filters(self):
{'all':self._all_tags, 'ac':'tags', 're':'excluded_tags'}, # Tag filter
{'all':self._all_status, 'ac':'status', 're':'excluded_status'}, # Status filter
{'all':self._all_trackers, 'ac':'trackers', 're':'excluded_trackers'}, # Tracker filter
{'all':self._all_paths, 'ac':'paths', 're':'excluded_paths'}, # Path filter
]
filter_obj = [CategoryFilter, TagFilter, StatusFilter, TrackerFilter]
filter_obj = [CategoryFilter, TagFilter, StatusFilter, TrackerFilter, PathFilter]

for i in range(0, len(filter_conf)):
# Initialize all of the filter arguments
Expand Down
3 changes: 2 additions & 1 deletion autoremovetorrents/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def disp(prop, converter = None):
"\tSeeder(connected/total):%d/%d\tLeecher(connected/total):%d/%d\tStatus:%s\n" +
"\tDownload Speed:%s(Avg.:%s)\tUpload Speed:%s(Avg.:%s)\n" +
"\tCreate Time:%s\tSeeding Time:%s\tDownloading Time:%s\tLast Activity:%s\n" +
"\tCategory:%s\tTags:%s\tTracker:%s") % \
"\tCategory:%s\tTags:%s\tTracker:%s\tDownload Dir:%s") % \
(
disp('name'),
disp('progress', lambda x: x*100),
Expand All @@ -57,4 +57,5 @@ def disp(prop, converter = None):
[urlparse_(x).hostname if urlparse_(x).hostname is not None else x for x in t]
)
),
disp('download_dir'),
)
5 changes: 4 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ pytest-cov
codacy-coverage
requests_mock
pytest-mock
setuptools
setuptools
sphinx
sphinx_rtd_theme
sphinx-intl
15 changes: 10 additions & 5 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ Just name your strategy like the task name.
Part II: Filters
++++++++++++++++

The removing condtions are only available for the torrents you chosen. There are 9 filters available.
The removing condtions are only available for the torrents you chosen. There are 12 filters available.

* ``all_trackers``/``all_categories``/``all_status``: Choose all the trackers/categories/status.
* ``all_trackers``/``all_categories``/``all_status``/``all_paths``: Choose all the trackers/categories/status/paths.
* ``categories``: Choose torrents in these categories.
* ``excluded_categories``: Don't choose torrents in these categories.
* ``trackers``: Choose torrents in these trackers.
Expand Down Expand Up @@ -148,18 +148,20 @@ The removing condtions are only available for the torrents you chosen. There are
- μTorrent doesn't have this status.

* ``excluded_status``: Don't choose these torrents in these status. Available status is shown in the table above.
* ``paths``: Choose torrents in these paths.
* ``excluded_paths``: Don't choose torrents in these paths.

The result of each filter is a set of torrents.

.. note::

When two or three of ``categories``, ``trackers`` and ``status`` filter are specificed, the program will take the intersection of these sets, and subtracts set ``excluded_categories``, ``excluded_trackers`` and ``excluded_status``.
When more than one of ``categories``, ``trackers``, ``status`` and ``paths`` filter are specified, the program will take the intersection of these sets, and subtracts set ``excluded_categories``, ``excluded_trackers``, ``excluded_status`` and ``excluded_paths``.


.. note::

1. Don't write sockets in ``trackers``. The ``trackers`` field only needs hostname, for example, just fill ``tracker.site1.com`` for ``https://tracker.site1.com``.
2. In 1.4.4 and later version, if there's only one item in ``categories``, ``trackers`` or ``status``, it's not necessary to use list structure. A single-line text is enough, for example:
2. In 1.4.4 and later version, if there's only one item in ``categories``, ``trackers``, ``status`` or ``paths``, it's not necessary to use list structure. A single-line text is enough, for example:

.. code-block:: yaml
Expand All @@ -172,6 +174,7 @@ The result of each filter is a set of torrents.
3. The ``StalledUp`` and ``StalledDown`` is the new status in version 1.4.5. In this program, ``Uploading`` inlcudes the torrents in ``StalledUpload`` status, and ``Downloading`` includes the torrents in ``StalledDownload`` status.
4. The ``paths`` and ``excluded_paths`` is only support the Transmission client (for now), and the path need end with ``/``.

Let's see some examples. Select those torrents whose categories are Movies or Games:

Expand Down Expand Up @@ -227,7 +230,7 @@ Select torrents whose categories are Movies or Games, but exclude those torrents
# Removing conditions are here
# ...
Select those torrents whose categories is Movies and status is uploading:
Select those torrents whose categories is Movies and status is uploading and path is /volume1/tv/:

.. code-block:: yaml
Expand All @@ -242,6 +245,8 @@ Select those torrents whose categories is Movies and status is uploading:
- Movies
status:
- Uploading
paths:
- /volume1/tv/
# Removing conditions are here
# ...
Expand Down
Loading

0 comments on commit 69901e0

Please sign in to comment.