Skip to content

Commit

Permalink
Merge branch 'Flexget:develop' into update_sftp_list_and_download_sym…
Browse files Browse the repository at this point in the history
…link_handling
  • Loading branch information
kingamajick committed Mar 15, 2023
2 parents a07f18a + 8b04065 commit 2e41181
Show file tree
Hide file tree
Showing 7 changed files with 1,464 additions and 1,408 deletions.
2 changes: 1 addition & 1 deletion flexget/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
The github actions release job will automatically strip the .dev for release,
and update the version again for continued development.
"""
__version__ = '3.5.30.dev'
__version__ = '3.5.33.dev'
37 changes: 37 additions & 0 deletions flexget/components/ftp/sftp_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
import logging
import os
import time
from base64 import b64decode
from dataclasses import dataclass
Expand All @@ -24,6 +25,7 @@
}

try:
import paramiko
import pysftp

logging.getLogger("paramiko").setLevel(logging.ERROR)
Expand All @@ -35,6 +37,40 @@
logger = logger.bind(name='sftp_client')


def _set_authentication_patch(self, password, private_key, private_key_pass):
"""
Patch pysftp.Connection._set_authentication to support additional
key types
"""
if password is None:
# Use Private Key.
if not private_key:
# Try to use default key.
if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
private_key = '~/.ssh/id_rsa'
elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')):
private_key = '~/.ssh/id_dsa'
else:
raise pysftp.exceptions.CredentialException("No password or key specified.")

if isinstance(private_key, (paramiko.AgentKey, paramiko.RSAKey)):
# use the paramiko agent or rsa key
self._tconnect['pkey'] = private_key
else:
# isn't a paramiko AgentKey or RSAKey, try to build a
# key from what we assume is a path to a key
private_key_file = os.path.expanduser(private_key)
for key in [paramiko.RSAKey, paramiko.DSSKey, paramiko.Ed25519Key, paramiko.ECDSAKey]:
try: # try all the keys
self._tconnect['pkey'] = key.from_private_key_file(
private_key_file, private_key_pass
)
return
except paramiko.SSHException: # if it fails, try dss
pass
raise paramiko.SSHException(f'Unknown key type: {private_key}')


@dataclass
class HostKey:
"""
Expand Down Expand Up @@ -280,6 +316,7 @@ def _connect(self, connection_tries: int) -> 'pysftp.Connection':

while not sftp:
try:
pysftp.Connection._set_authentication = _set_authentication_patch
sftp = pysftp.Connection(
host=self.host,
username=self.username,
Expand Down
11 changes: 4 additions & 7 deletions flexget/plugins/clients/transmission.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import base64
import os
import re
from datetime import datetime, timedelta
Expand Down Expand Up @@ -70,6 +69,7 @@ def create_rpc_client(self, config) -> 'transmissionrpc.Client':
path=path,
username=user,
password=password,
timeout=30,
)
except TransmissionError as e:
if e.original and e.original.code == 401:
Expand Down Expand Up @@ -419,14 +419,11 @@ def on_task_output(self, task, config):
try:
if downloaded:
with open(entry['file'], 'rb') as f:
filedump = base64.b64encode(f.read()).decode('utf-8')
torrent_info = client.add_torrent(filedump, 30, **options['add'])
torrent_info = client.add_torrent(f.read(), **options['add'])
else:
if options['post'].get('magnetization_timeout', 0) > 0:
options['add']['paused'] = False
torrent_info = client.add_torrent(
entry['url'], timeout=30, **options['add']
)
torrent_info = client.add_torrent(entry['url'], **options['add'])
except TransmissionError as e:
logger.opt(exception=True).debug('TransmissionError')
logger.debug('Failed options dict: {}', options['add'])
Expand Down Expand Up @@ -613,7 +610,7 @@ def on_task_output(self, task, config):

# Set any changed file properties
if list(options['change'].keys()):
client.change_torrent(torrent_info.id, 30, **options['change'])
client.change_torrent(torrent_info.id, **options['change'])

start_torrent = partial(client.start_torrent, [torrent_info.id])

Expand Down
8 changes: 6 additions & 2 deletions flexget/plugins/input/torznab.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flexget.event import event
from flexget.plugin import PluginError
from flexget.utils.requests import RequestException
from flexget.utils.tools import parse_timedelta

logger = logger.bind(name='torznab')

Expand All @@ -33,6 +34,7 @@ def schema(self):
'default': 'search',
},
'website': {'type': 'string', 'format': 'url'},
'timeout': {"type": "string", "format": "interval"},
},
'required': ['website', 'apikey'],
'additionalProperties': False,
Expand Down Expand Up @@ -74,6 +76,8 @@ def _build_url(self, **kwargs):
def _setup(self, task, config):
"""Set up parameters"""
self.base_url = config['website'].rstrip('/')
config.setdefault('timeout', '30 seconds')
self.timeout = parse_timedelta(config['timeout']).total_seconds()
self.supported_params = []
if config['searcher'] == 'tv':
config['searcher'] = 'tvsearch'
Expand All @@ -87,7 +91,7 @@ def _setup(self, task, config):
def _setup_caps(self, task, searcher, categories):
"""Gets the capabilities of the torznab indexer and matches it with the provided configuration"""

response = task.requests.get(self._build_url(t='caps'))
response = task.requests.get(self._build_url(t='caps'), timeout=self.timeout)
logger.debug('Raw caps response {}', response.content)
root = ElementTree.fromstring(response.content)
self._setup_searcher(root, searcher, categories)
Expand Down Expand Up @@ -161,7 +165,7 @@ def create_entries_from_query(self, url, task):
logger.info('Fetching URL: {}', url)

try:
response = task.requests.get(url)
response = task.requests.get(url, timeout=self.timeout)
except RequestException as e:
raise PluginError("Failed fetching '{}': {}".format(url, e))

Expand Down
Loading

0 comments on commit 2e41181

Please sign in to comment.