diff --git a/core/__init__.py b/core/__init__.py index 15d78bfd..c59daa92 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -46,12 +46,21 @@ from six.moves import reload_module from core import logger, main_db, version_check, databases, transcoder from core.configuration import config +from core.plugins.downloaders.configuration import ( + configure_nzbs, + configure_torrents, + configure_torrent_class, +) +from core.plugins.downloaders.utils import ( + pause_torrent, + remove_torrent, + resume_torrent, +) from core.utils import ( RunningProcess, category_search, clean_dir, copy_link, - create_torrent_class, extract_files, flatten, get_dirs, @@ -59,13 +68,10 @@ from core.utils import ( list_media_files, make_dir, parse_args, - pause_torrent, rchmod, remove_dir, remove_read_only, - remove_torrent, restart, - resume_torrent, sanitize_name, update_download_info_status, wake_up, @@ -403,26 +409,6 @@ def configure_wake_on_lan(): wake_up() -def configure_sabnzbd(): - global SABNZBD_HOST - global SABNZBD_PORT - global SABNZBD_APIKEY - - SABNZBD_HOST = CFG['Nzb']['sabnzbd_host'] - SABNZBD_PORT = int(CFG['Nzb']['sabnzbd_port'] or 8080) # defaults to accommodate NzbGet - SABNZBD_APIKEY = CFG['Nzb']['sabnzbd_apikey'] - - -def configure_nzbs(): - global NZB_CLIENT_AGENT - global NZB_DEFAULT_DIRECTORY - - NZB_CLIENT_AGENT = CFG['Nzb']['clientAgent'] # sabnzbd - NZB_DEFAULT_DIRECTORY = CFG['Nzb']['default_downloadDirectory'] - - configure_sabnzbd() - - def configure_groups(): global GROUPS @@ -435,114 +421,6 @@ def configure_groups(): GROUPS = None -def configure_utorrent(): - global UTORRENT_WEB_UI - global UTORRENT_USER - global UTORRENT_PASSWORD - - UTORRENT_WEB_UI = CFG['Torrent']['uTorrentWEBui'] # http://localhost:8090/gui/ - UTORRENT_USER = CFG['Torrent']['uTorrentUSR'] # mysecretusr - UTORRENT_PASSWORD = CFG['Torrent']['uTorrentPWD'] # mysecretpwr - - -def configure_transmission(): - global TRANSMISSION_HOST - global TRANSMISSION_PORT - global TRANSMISSION_USER - global TRANSMISSION_PASSWORD - - TRANSMISSION_HOST = CFG['Torrent']['TransmissionHost'] # localhost - TRANSMISSION_PORT = int(CFG['Torrent']['TransmissionPort']) - TRANSMISSION_USER = CFG['Torrent']['TransmissionUSR'] # mysecretusr - TRANSMISSION_PASSWORD = CFG['Torrent']['TransmissionPWD'] # mysecretpwr - - -def configure_deluge(): - global DELUGE_HOST - global DELUGE_PORT - global DELUGE_USER - global DELUGE_PASSWORD - - DELUGE_HOST = CFG['Torrent']['DelugeHost'] # localhost - DELUGE_PORT = int(CFG['Torrent']['DelugePort']) # 8084 - DELUGE_USER = CFG['Torrent']['DelugeUSR'] # mysecretusr - DELUGE_PASSWORD = CFG['Torrent']['DelugePWD'] # mysecretpwr - - -def configure_qbittorrent(): - global QBITTORRENT_HOST - global QBITTORRENT_PORT - global QBITTORRENT_USER - global QBITTORRENT_PASSWORD - - QBITTORRENT_HOST = CFG['Torrent']['qBittorrenHost'] # localhost - QBITTORRENT_PORT = int(CFG['Torrent']['qBittorrentPort']) # 8080 - QBITTORRENT_USER = CFG['Torrent']['qBittorrentUSR'] # mysecretusr - QBITTORRENT_PASSWORD = CFG['Torrent']['qBittorrentPWD'] # mysecretpwr - - -def configure_flattening(): - global NOFLATTEN - - NOFLATTEN = (CFG['Torrent']['noFlatten']) - if isinstance(NOFLATTEN, str): - NOFLATTEN = NOFLATTEN.split(',') - - -def configure_torrent_categories(): - global CATEGORIES - - CATEGORIES = (CFG['Torrent']['categories']) # music,music_videos,pictures,software - if isinstance(CATEGORIES, str): - CATEGORIES = CATEGORIES.split(',') - - -def configure_torrent_resuming(): - global TORRENT_RESUME - global TORRENT_RESUME_ON_FAILURE - - TORRENT_RESUME_ON_FAILURE = int(CFG['Torrent']['resumeOnFailure']) - TORRENT_RESUME = int(CFG['Torrent']['resume']) - - -def configure_torrent_permissions(): - global TORRENT_CHMOD_DIRECTORY - - TORRENT_CHMOD_DIRECTORY = int(str(CFG['Torrent']['chmodDirectory']), 8) - - -def configure_torrent_deltetion(): - global DELETE_ORIGINAL - - DELETE_ORIGINAL = int(CFG['Torrent']['deleteOriginal']) - - -def configure_torrent_linking(): - global USE_LINK - - USE_LINK = CFG['Torrent']['useLink'] # no | hard | sym - - -def configure_torrents(): - global TORRENT_CLIENT_AGENT - global OUTPUT_DIRECTORY - global TORRENT_DEFAULT_DIRECTORY - - TORRENT_CLIENT_AGENT = CFG['Torrent']['clientAgent'] # utorrent | deluge | transmission | rtorrent | vuze | qbittorrent |other - OUTPUT_DIRECTORY = CFG['Torrent']['outputDirectory'] # /abs/path/to/complete/ - TORRENT_DEFAULT_DIRECTORY = CFG['Torrent']['default_downloadDirectory'] - configure_torrent_linking() - configure_flattening() - configure_torrent_deltetion() - configure_torrent_categories() - configure_torrent_permissions() - configure_torrent_resuming() - configure_utorrent() - configure_transmission() - configure_deluge() - configure_qbittorrent() - - def configure_remote_paths(): global REMOTE_PATHS @@ -973,13 +851,6 @@ def configure_passwords_file(): PASSWORDS_FILE = CFG['passwords']['PassWordFile'] -def configure_torrent_class(): - global TORRENT_CLASS - - # create torrent class - TORRENT_CLASS = create_torrent_class(TORRENT_CLIENT_AGENT) - - def configure_sections(section): global SECTIONS global CATEGORIES @@ -1138,8 +1009,8 @@ def initialize(section=None): configure_general() configure_updates() configure_wake_on_lan() - configure_nzbs() - configure_torrents() + configure_nzbs(CFG) + configure_torrents(CFG) configure_remote_paths() configure_plex() configure_niceness() diff --git a/core/plugins/__init__.py b/core/plugins/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/core/plugins/downloaders/__init__.py b/core/plugins/downloaders/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/core/plugins/downloaders/configuration.py b/core/plugins/downloaders/configuration.py new file mode 100644 index 00000000..7ea4f881 --- /dev/null +++ b/core/plugins/downloaders/configuration.py @@ -0,0 +1,5 @@ +from core.plugins.downloaders.nzb.configuration import configure_nzbs +from core.plugins.downloaders.torrent.configuration import ( + configure_torrents, + configure_torrent_class, +) diff --git a/core/plugins/downloaders/nzb/__init__.py b/core/plugins/downloaders/nzb/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/core/plugins/downloaders/nzb/configuration.py b/core/plugins/downloaders/nzb/configuration.py new file mode 100644 index 00000000..d0883d01 --- /dev/null +++ b/core/plugins/downloaders/nzb/configuration.py @@ -0,0 +1,15 @@ +import core + + +def configure_nzbs(config): + nzb_config = config['Nzb'] + core.NZB_CLIENT_AGENT = nzb_config['clientAgent'] # sabnzbd + core.NZB_DEFAULT_DIRECTORY = nzb_config['default_downloadDirectory'] + + configure_sabnzbd(nzb_config) + + +def configure_sabnzbd(config): + core.SABNZBD_HOST = config['sabnzbd_host'] + core.SABNZBD_PORT = int(config['sabnzbd_port'] or 8080) # defaults to accommodate NzbGet + core.SABNZBD_APIKEY = config['sabnzbd_apikey'] diff --git a/core/plugins/downloaders/torrent/__init__.py b/core/plugins/downloaders/torrent/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/core/plugins/downloaders/torrent/configuration.py b/core/plugins/downloaders/torrent/configuration.py new file mode 100644 index 00000000..92001636 --- /dev/null +++ b/core/plugins/downloaders/torrent/configuration.py @@ -0,0 +1,81 @@ +import core +from core.plugins.downloaders.torrent.utils import create_torrent_class + + +def configure_torrents(config): + torrent_config = config['Torrent'] + core.TORRENT_CLIENT_AGENT = torrent_config['clientAgent'] # utorrent | deluge | transmission | rtorrent | vuze | qbittorrent |other + core.OUTPUT_DIRECTORY = torrent_config['outputDirectory'] # /abs/path/to/complete/ + core.TORRENT_DEFAULT_DIRECTORY = torrent_config['default_downloadDirectory'] + + configure_torrent_linking(torrent_config) + configure_flattening(torrent_config) + configure_torrent_deletion(torrent_config) + configure_torrent_categories(torrent_config) + configure_torrent_permissions(torrent_config) + configure_torrent_resuming(torrent_config) + configure_utorrent(torrent_config) + configure_transmission(torrent_config) + configure_deluge(torrent_config) + configure_qbittorrent(torrent_config) + + +def configure_torrent_linking(config): + core.USE_LINK = config['useLink'] # no | hard | sym + + +def configure_flattening(config): + core.NOFLATTEN = (config['noFlatten']) + if isinstance(core.NOFLATTEN, str): + core.NOFLATTEN = core.NOFLATTEN.split(',') + + +def configure_torrent_categories(config): + core.CATEGORIES = (config['categories']) # music,music_videos,pictures,software + if isinstance(core.CATEGORIES, str): + core.CATEGORIES = core.CATEGORIES.split(',') + + +def configure_torrent_resuming(config): + core.TORRENT_RESUME_ON_FAILURE = int(config['resumeOnFailure']) + core.TORRENT_RESUME = int(config['resume']) + + +def configure_torrent_permissions(config): + core.TORRENT_CHMOD_DIRECTORY = int(str(config['chmodDirectory']), 8) + + +def configure_torrent_deletion(config): + core.DELETE_ORIGINAL = int(config['deleteOriginal']) + + +def configure_utorrent(config): + core.UTORRENT_WEB_UI = config['uTorrentWEBui'] # http://localhost:8090/gui/ + core.UTORRENT_USER = config['uTorrentUSR'] # mysecretusr + core.UTORRENT_PASSWORD = config['uTorrentPWD'] # mysecretpwr + + +def configure_transmission(config): + core.TRANSMISSION_HOST = config['TransmissionHost'] # localhost + core.TRANSMISSION_PORT = int(config['TransmissionPort']) + core.TRANSMISSION_USER = config['TransmissionUSR'] # mysecretusr + core.TRANSMISSION_PASSWORD = config['TransmissionPWD'] # mysecretpwr + + +def configure_deluge(config): + core.DELUGE_HOST = config['DelugeHost'] # localhost + core.DELUGE_PORT = int(config['DelugePort']) # 8084 + core.DELUGE_USER = config['DelugeUSR'] # mysecretusr + core.DELUGE_PASSWORD = config['DelugePWD'] # mysecretpwr + + +def configure_qbittorrent(config): + core.QBITTORRENT_HOST = config['qBittorrenHost'] # localhost + core.QBITTORRENT_PORT = int(config['qBittorrentPort']) # 8080 + core.QBITTORRENT_USER = config['qBittorrentUSR'] # mysecretusr + core.QBITTORRENT_PASSWORD = config['qBittorrentPWD'] # mysecretpwr + + +def configure_torrent_class(): + # create torrent class + core.TORRENT_CLASS = create_torrent_class(core.TORRENT_CLIENT_AGENT) diff --git a/core/plugins/downloaders/torrent/deluge.py b/core/plugins/downloaders/torrent/deluge.py new file mode 100644 index 00000000..69ae2000 --- /dev/null +++ b/core/plugins/downloaders/torrent/deluge.py @@ -0,0 +1,21 @@ +from synchronousdeluge.client import DelugeClient + +import core +from core import logger + + +def configure_client(): + agent = 'deluge' + host = core.DELUGE_HOST + port = core.DELUGE_PORT + user = core.DELUGE_USER + password = core.DELUGE_PASSWORD + + logger.debug('Connecting to {0}: http://{1}:{2}'.format(agent, host, port)) + client = DelugeClient() + try: + client.connect(host, port, user, password) + except Exception: + logger.error('Failed to connect to Deluge') + else: + return client diff --git a/core/plugins/downloaders/torrent/qbittorrent.py b/core/plugins/downloaders/torrent/qbittorrent.py new file mode 100644 index 00000000..37671fea --- /dev/null +++ b/core/plugins/downloaders/torrent/qbittorrent.py @@ -0,0 +1,23 @@ +from qbittorrent import Client as qBittorrentClient + +import core +from core import logger + + +def configure_client(): + agent = 'qbittorrent' + host = core.QBITTORRENT_HOST + port = core.QBITTORRENT_PORT + user = core.QBITTORRENT_USER + password = core.QBITTORRENT_PASSWORD + + logger.debug( + 'Connecting to {0}: http://{1}:{2}'.format(agent, host, port) + ) + client = qBittorrentClient('http://{0}:{1}/'.format(host, port)) + try: + client.login(user, password) + except Exception: + logger.error('Failed to connect to qBittorrent') + else: + return client diff --git a/core/plugins/downloaders/torrent/transmission.py b/core/plugins/downloaders/torrent/transmission.py new file mode 100644 index 00000000..d122aaf0 --- /dev/null +++ b/core/plugins/downloaders/torrent/transmission.py @@ -0,0 +1,20 @@ +from transmissionrpc.client import Client as TransmissionClient + +import core +from core import logger + + +def configure_client(): + agent = 'transmission' + host = core.TRANSMISSION_HOST + port = core.TRANSMISSION_PORT + user = core.TRANSMISSION_USER + password = core.TRANSMISSION_PASSWORD + + logger.debug('Connecting to {0}: http://{1}:{2}'.format(agent, host, port)) + try: + client = TransmissionClient(host, port, user, password) + except Exception: + logger.error('Failed to connect to Transmission') + else: + return client diff --git a/core/utils/torrents.py b/core/plugins/downloaders/torrent/utils.py similarity index 57% rename from core/utils/torrents.py rename to core/plugins/downloaders/torrent/utils.py index 4c152e47..be2db4e8 100644 --- a/core/utils/torrents.py +++ b/core/plugins/downloaders/torrent/utils.py @@ -1,55 +1,28 @@ import time -from qbittorrent import Client as qBittorrentClient -from synchronousdeluge.client import DelugeClient -from transmissionrpc.client import Client as TransmissionClient -from utorrent.client import UTorrentClient - import core from core import logger +from .deluge import configure_client as deluge_client +from .qbittorrent import configure_client as qbittorrent_client +from .transmission import configure_client as transmission_client +from .utorrent import configure_client as utorrent_client + +torrent_clients = { + 'deluge': deluge_client, + 'qbittorrent': qbittorrent_client, + 'transmission': transmission_client, + 'utorrent': utorrent_client, +} + def create_torrent_class(client_agent): - # Hardlink solution for Torrents - tc = None - if not core.APP_NAME == 'TorrentToMedia.py': #Skip loading Torrent for NZBs. - return tc + if not core.APP_NAME == 'TorrentToMedia.py': + return # Skip loading Torrent for NZBs. - if client_agent == 'utorrent': - try: - logger.debug('Connecting to {0}: {1}'.format(client_agent, core.UTORRENT_WEB_UI)) - tc = UTorrentClient(core.UTORRENT_WEB_UI, core.UTORRENT_USER, core.UTORRENT_PASSWORD) - except Exception: - logger.error('Failed to connect to uTorrent') - - if client_agent == 'transmission': - try: - logger.debug('Connecting to {0}: http://{1}:{2}'.format( - client_agent, core.TRANSMISSION_HOST, core.TRANSMISSION_PORT)) - tc = TransmissionClient(core.TRANSMISSION_HOST, core.TRANSMISSION_PORT, - core.TRANSMISSION_USER, - core.TRANSMISSION_PASSWORD) - except Exception: - logger.error('Failed to connect to Transmission') - - if client_agent == 'deluge': - try: - logger.debug('Connecting to {0}: http://{1}:{2}'.format(client_agent, core.DELUGE_HOST, core.DELUGE_PORT)) - tc = DelugeClient() - tc.connect(host=core.DELUGE_HOST, port=core.DELUGE_PORT, username=core.DELUGE_USER, - password=core.DELUGE_PASSWORD) - except Exception: - logger.error('Failed to connect to Deluge') - - if client_agent == 'qbittorrent': - try: - logger.debug('Connecting to {0}: http://{1}:{2}'.format(client_agent, core.QBITTORRENT_HOST, core.QBITTORRENT_PORT)) - tc = qBittorrentClient('http://{0}:{1}/'.format(core.QBITTORRENT_HOST, core.QBITTORRENT_PORT)) - tc.login(core.QBITTORRENT_USER, core.QBITTORRENT_PASSWORD) - except Exception: - logger.error('Failed to connect to qBittorrent') - - return tc + client = torrent_clients.get(client_agent) + if client: + return client() def pause_torrent(client_agent, input_hash, input_id, input_name): diff --git a/core/plugins/downloaders/torrent/utorrent.py b/core/plugins/downloaders/torrent/utorrent.py new file mode 100644 index 00000000..95422bfe --- /dev/null +++ b/core/plugins/downloaders/torrent/utorrent.py @@ -0,0 +1,19 @@ +from utorrent.client import UTorrentClient + +import core +from core import logger + + +def configure_client(): + agent = 'utorrent' + web_ui = core.UTORRENT_WEB_UI + user = core.UTORRENT_USER + password = core.UTORRENT_PASSWORD + + logger.debug('Connecting to {0}: {1}'.format(agent, web_ui)) + try: + client = UTorrentClient(web_ui, user, password) + except Exception: + logger.error('Failed to connect to uTorrent') + else: + return client diff --git a/core/plugins/downloaders/utils.py b/core/plugins/downloaders/utils.py new file mode 100644 index 00000000..a84d270b --- /dev/null +++ b/core/plugins/downloaders/utils.py @@ -0,0 +1,5 @@ +from core.plugins.downloaders.torrent.utils import ( + pause_torrent, + remove_torrent, + resume_torrent, +) diff --git a/core/utils/__init__.py b/core/utils/__init__.py index 0083fbbd..b662e9ae 100644 --- a/core/utils/__init__.py +++ b/core/utils/__init__.py @@ -45,7 +45,6 @@ from core.utils.paths import ( ) from core.utils.processes import RunningProcess, restart from core.utils.subtitles import import_subs -from core.utils.torrents import create_torrent_class, pause_torrent, remove_torrent, resume_torrent requests.packages.urllib3.disable_warnings() shutil_custom.monkey_patch()