|
| 1 | +# |
| 2 | +# core.py |
| 3 | +# |
| 4 | +# Copyright (C) 2013 Stefan Talpalaru <[email protected]> |
| 5 | +# |
| 6 | +# Basic plugin template created by: |
| 7 | +# Copyright (C) 2008 Martijn Voncken <[email protected]> |
| 8 | +# Copyright (C) 2007-2009 Andrew Resch <[email protected]> |
| 9 | +# Copyright (C) 2009 Damien Churchill <[email protected]> |
| 10 | +# Copyright (C) 2010 Pedro Algarvio <[email protected]> |
| 11 | +# |
| 12 | +# Deluge is free software. |
| 13 | +# |
| 14 | +# You may redistribute it and/or modify it under the terms of the |
| 15 | +# GNU General Public License, as published by the Free Software |
| 16 | +# Foundation; either version 3 of the License, or (at your option) |
| 17 | +# any later version. |
| 18 | +# |
| 19 | +# deluge is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 22 | +# See the GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with deluge. If not, write to: |
| 26 | +# The Free Software Foundation, Inc., |
| 27 | +# 51 Franklin Street, Fifth Floor |
| 28 | +# Boston, MA 02110-1301, USA. |
| 29 | +# |
| 30 | +# In addition, as a special exception, the copyright holders give |
| 31 | +# permission to link the code of portions of this program with the OpenSSL |
| 32 | +# library. |
| 33 | +# You must obey the GNU General Public License in all respects for all of |
| 34 | +# the code used other than OpenSSL. If you modify file(s) with this |
| 35 | +# exception, you may extend this exception to your version of the file(s), |
| 36 | +# but you are not obligated to do so. If you do not wish to do so, delete |
| 37 | +# this exception statement from your version. If you delete this exception |
| 38 | +# statement from all source files in the program, then also delete it here. |
| 39 | +# |
| 40 | + |
| 41 | +import logging |
| 42 | +from deluge.plugins.pluginbase import CorePluginBase |
| 43 | +import deluge.component as component |
| 44 | +import deluge.configmanager |
| 45 | +from deluge.core.rpcserver import export |
| 46 | +from pprint import pprint |
| 47 | + |
| 48 | +DEFAULT_PREFS = { |
| 49 | + "trackers": [ |
| 50 | + #{"url": "test"}, |
| 51 | + ], |
| 52 | +} |
| 53 | + |
| 54 | +log = logging.getLogger(__name__) |
| 55 | + |
| 56 | +class Core(CorePluginBase): |
| 57 | + def enable(self): |
| 58 | + self.config = deluge.configmanager.ConfigManager("defaulttrackers.conf", DEFAULT_PREFS) |
| 59 | + component.get("EventManager").register_event_handler( |
| 60 | + "TorrentAddedEvent", self.on_torrent_added |
| 61 | + ) |
| 62 | + |
| 63 | + def disable(self): |
| 64 | + component.get("EventManager").deregister_event_handler( |
| 65 | + "TorrentAddedEvent", self.on_torrent_added |
| 66 | + ) |
| 67 | + |
| 68 | + def update(self): |
| 69 | + pass |
| 70 | + |
| 71 | + def on_torrent_added(self, torrent_id): |
| 72 | + torrent = component.get("TorrentManager")[torrent_id] |
| 73 | + trackers = torrent.get_status(["trackers"])["trackers"] |
| 74 | + existing_urls = [tracker["url"] for tracker in trackers] |
| 75 | + got_new_trackers = False |
| 76 | + for new_tracker in self.config["trackers"]: |
| 77 | + if new_tracker["url"] not in existing_urls: |
| 78 | + got_new_trackers = True |
| 79 | + trackers.append({ |
| 80 | + "tier": 0, |
| 81 | + "url": str(new_tracker["url"]), |
| 82 | + }) |
| 83 | + if got_new_trackers: |
| 84 | + torrent.set_trackers(trackers) |
| 85 | + log.debug("added new trackers for %s" % torrent.filename) |
| 86 | + |
| 87 | + @export |
| 88 | + def set_config(self, config): |
| 89 | + """Sets the config dictionary""" |
| 90 | + for key in config.keys(): |
| 91 | + self.config[key] = config[key] |
| 92 | + self.config.save() |
| 93 | + |
| 94 | + @export |
| 95 | + def get_config(self): |
| 96 | + """Returns the config dictionary""" |
| 97 | + return self.config.config |
| 98 | + |
0 commit comments