flexget.plugins.modify_trackers
Covered: 55 lines
Missed: 4 lines
Skipped 21 lines
Percent: 93 %
 1
import re
 2
import logging
 3
from flexget.plugin import priority, register_plugin
 5
log = logging.getLogger('modify_trackers')
 8
class AddTrackers(object):
10
    """
11
        Adds tracker URL to torrent files.
13
        Configuration example:
15
        add_trackers:
16
          - uri://tracker_address:port/
18
        This will add all tracker URL uri://tracker_address:port/.
19
        TIP: You can use global section in configuration to make this enabled on all feeds.
20
    """
22
    def validator(self):
23
        from flexget import validator
24
        trackers = validator.factory('list')
25
        trackers.accept('url', protocols=['udp', 'http'])
26
        return trackers
28
    @priority(127)
29
    def on_feed_modify(self, feed, config):
30
        for entry in feed.entries:
31
            if 'torrent' in entry:
32
                for url in config:
33
                    if not url in entry['torrent'].get_multitrackers():
34
                        entry['torrent'].add_multitracker(url)
35
                        log.info('Added %s tracker to %s' % (url, entry['title']))
36
            if entry['url'].startswith('magnet:'):
37
                entry['url'] += ''.join(['&tr=' + url for url in config])
40
class RemoveTrackers(object):
42
    """
43
        Removes trackers from torrent files using regexp matching.
45
        Configuration example:
47
        remove_trackers:
48
          - moviex
50
        This will remove all trackers that contain text moviex in their url.
51
        TIP: You can use global section in configuration to make this enabled on all feeds.
52
    """
54
    def validator(self):
55
        from flexget import validator
56
        trackers = validator.factory('list')
57
        trackers.accept('regexp')
58
        return trackers
60
    @priority(127)
61
    def on_feed_modify(self, feed, config):
62
        for entry in feed.entries:
63
            if 'torrent' in entry:
64
                trackers = entry['torrent'].get_multitrackers()
65
                for tracker in trackers:
66
                    for regexp in config or []:
67
                        if re.search(regexp, tracker, re.IGNORECASE | re.UNICODE):
68
                            log.debug('remove_trackers removing %s because of %s' % (tracker, regexp))
70
                            entry['torrent'].remove_multitracker(tracker)
71
                            log.info('Removed %s' % tracker)
72
            if entry['url'].startswith('magnet:'):
73
                for regexp in config:
75
                    tr_search = r'&tr=([^&]*%s[^&]*)' % regexp
76
                    entry['url'] = re.sub(tr_search, '', entry['url'], re.IGNORECASE | re.UNICODE)
78
register_plugin(AddTrackers, 'add_trackers', api_ver=2)
79
register_plugin(RemoveTrackers, 'remove_trackers', api_ver=2)