Repository: pymedusa/Medusa

Issue:
Deprecation Warning Call set_torrent_stop_at_ratio to Deluge
When Medusa snatches torrents to Deluge with any number besides `0` set in `Seed ratio` under `Provider Options & Feel` results in Deluge showing the below Depreciated warning call error (error taken from Deluge logs). My Deluge log file is polluted and overfilled with this same error anytime a torrent is snatched.

```
/usr/lib/python3/dist-packages/deluge/core/rpcserver.py:326: DeprecationWarning: Call to deprecated function set_torrent_stop_at_ratio.

  ret = self.factory.methods[method](*args, **kwargs)

/usr/lib/python3/dist-packages/deluge/core/rpcserver.py:326: DeprecationWarning: Call to deprecated function set_torrent_stop_ratio.

  ret = self.factory.methods[method](*args, **kwargs)
```






**Medusa**
- Docker: linuxserver/medusa:latest
- Branch: master
- Version: 0.5.18
- Database: 44.18
- OS: Armbian Linux-5.4.142-odroidxu4-armv7l-with
- Python Version: 3.8.10 (default, May  6 2021, 06:30:44)  [GCC 9.3.0]
- SSL Version: OpenSSL 1.1.1l  24 Aug 2021
- Locale: en_US.UTF-8
- Timezone: EDT


**Deluge**
- Docker: linuxserver/deluge:latest
- deluged 2.0.3-2-201906121747-ubuntu18.04.1
- OS: Armbian Linux-5.4.142-odroidxu4-armv7l-with
- libtorrent: 1.1.5.0
- Python: 3.6.9







Discussion:
Your right, the api command needs to be upgraded to: `set_torrent_options`. Only thing is that I can't see from wich version this can be used. As soon as I know that, i'll try to update it. 
https://forum.deluge-torrent.org/viewtopic.php?f=8&t=56055

Resolving patch:
diff --git a/medusa/clients/torrent/deluge.py b/medusa/clients/torrent/deluge.py
--- a/medusa/clients/torrent/deluge.py
+++ b/medusa/clients/torrent/deluge.py
@@ -120,6 +120,7 @@ def __init__(self, host=None, username=None, password=None):
         super(DelugeAPI, self).__init__('Deluge', host, username, password)
         self.session.headers.update({'Content-Type': 'application/json'})
         self.url = urljoin(self.host, 'json')
+        self.version = None
 
         self._get_auth()
 
@@ -216,8 +217,28 @@ def _get_auth(self):
                           {'name': self.name})
                 return None
 
+        if self.auth:
+            self._get_version()
+
         return self.auth
 
+    def _get_version(self):
+        params = {
+            'method': 'daemon.get_version',
+            'params': {},
+            'id': 12
+        }
+
+        version = self.session.post(
+            self.url,
+            data=json.dumps(params),
+            verify=app.TORRENT_VERIFY_CERT
+        )
+
+        split_version = version.json()['result'].split('.')[0:2]
+        self.version = tuple(int(x) for x in split_version)
+        return self.version
+
     def _add_torrent_uri(self, result):
 
         post_data = json.dumps({
@@ -385,30 +406,50 @@ def _set_torrent_ratio(self, result):
 
         # blank is default client ratio, so we also shouldn't set ratio
         if ratio and float(ratio) >= 0:
-            post_data = json.dumps({
-                'method': 'core.set_torrent_stop_at_ratio',
-                'params': [
-                    result.hash,
-                    True,
-                ],
-                'id': 5,
-            })
+            if self.version >= (2, 0):
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_options',
+                    'params': [
+                        result.hash,
+                        {'stop_at_ratio': True},
+                    ],
+                    'id': 5,
+                })
+            else:
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_stop_ratio',
+                    'params': [
+                        result.hash,
+                        True,
+                    ],
+                    'id': 6,
+                })
 
             self._request(method='post', data=post_data)
 
-            # if unable to set_torrent_stop_at_ratio return False
+            # if unable to set_torrent_options return False
             # No reason to set ratio.
             if self.response.json()['error']:
                 return False
 
-            post_data = json.dumps({
-                'method': 'core.set_torrent_stop_ratio',
-                'params': [
-                    result.hash,
-                    float(ratio),
-                ],
-                'id': 6,
-            })
+            if self.version >= (2, 0):
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_options',
+                    'params': [
+                        result.hash,
+                        {'stop_ratio': float(ratio)},
+                    ],
+                    'id': 5,
+                })
+            else:
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_stop_ratio',
+                    'params': [
+                        result.hash,
+                        float(ratio),
+                    ],
+                    'id': 5,
+                })
 
             self._request(method='post', data=post_data)
 
@@ -416,9 +457,21 @@ def _set_torrent_ratio(self, result):
 
         elif ratio and float(ratio) == -1:
             # Disable stop at ratio to seed forever
-            post_data = json.dumps({'method': 'core.set_torrent_stop_at_ratio',
-                                    'params': [result.hash, False],
-                                    'id': 5})
+            if self.version >= (2, 0):
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_options',
+                    'params': [
+                        result.hash,
+                        {'stop_at_ratio': False},
+                    ],
+                    'id': 5,
+                })
+            else:
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_stop_at_ratio',
+                    'params': [result.hash, False],
+                    'id': 5
+                })
 
             self._request(method='post', data=post_data)
 
@@ -429,25 +482,45 @@ def _set_torrent_ratio(self, result):
     def _set_torrent_path(self, result):
 
         if app.TORRENT_PATH:
-            post_data = json.dumps({
-                'method': 'core.set_torrent_move_completed',
-                'params': [
-                    result.hash,
-                    True,
-                ],
-                'id': 7,
-            })
+            if self.version >= (2, 0):
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_options ',
+                    'params': [
+                        result.hash,
+                        {'move_completed': True},
+                    ],
+                    'id': 7,
+                })
+            else:
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_move_completed',
+                    'params': [
+                        result.hash,
+                        True,
+                    ],
+                    'id': 7,
+                })
 
             self._request(method='post', data=post_data)
 
-            post_data = json.dumps({
-                'method': 'core.set_torrent_move_completed_path',
-                'params': [
-                    result.hash,
-                    app.TORRENT_PATH,
-                ],
-                'id': 8,
-            })
+            if self.version >= (2, 0):
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_options',
+                    'params': [
+                        result.hash,
+                        {'move_completed_path': app.TORRENT_PATH},
+                    ],
+                    'id': 8,
+                })
+            else:
+                post_data = json.dumps({
+                    'method': 'core.set_torrent_move_completed_path',
+                    'params': [
+                        result.hash,
+                        app.TORRENT_PATH,
+                    ],
+                    'id': 8,
+                })
 
             self._request(method='post', data=post_data)
 
diff --git a/medusa/clients/torrent/deluged.py b/medusa/clients/torrent/deluged.py
--- a/medusa/clients/torrent/deluged.py
+++ b/medusa/clients/torrent/deluged.py
@@ -301,6 +301,19 @@ def connect(self):
             log.warning('Error while trying to connect to deluge daemon. Error: {error}', {'error': error})
             raise
 
+    def get_version(self):
+        """Return the deluge daemon major, minor version as a tuple."""
+        version = None
+        try:
+            version = self.client.daemon.get_version()
+            split_version = version.split('.')[0:2]
+            version = tuple(int(x) for x in split_version)
+        except Exception as error:
+            log.warning('Error while trying to get the deluge daemon version. Error: {error}', {'error': error})
+            raise
+
+        return version
+
     def disconnect(self):
         """Disconnect RPC client."""
         self.client.disconnect()
@@ -420,8 +433,12 @@ def set_torrent_path(self, torrent_id, path):
         """
         try:
             self.connect()
-            self.client.core.set_torrent_move_completed_path(torrent_id, path)
-            self.client.core.set_torrent_move_completed(torrent_id, 1)
+            if self.get_version() >= (2, 0):
+                self.client.core.set_torrent_options(torrent_id, {'completed_path': path})
+                self.client.core.set_torrent_options(torrent_id, {'move_completed': 1})
+            else:
+                self.client.core.set_torrent_move_completed_path(torrent_id, path)
+                self.client.core.set_torrent_move_completed(torrent_id, 1)
         except Exception:
             return False
         else:
@@ -464,8 +481,13 @@ def set_torrent_ratio(self, torrent_id, ratio):
         """
         try:
             self.connect()
-            self.client.core.set_torrent_stop_at_ratio(torrent_id, True)
-            self.client.core.set_torrent_stop_ratio(torrent_id, ratio)
+            if self.get_version() >= (2, 0):
+                self.client.core.set_torrent_options(torrent_id, {'stop_at_ratio': True})
+                self.client.core.set_torrent_options(torrent_id, {'stop_ratio': ratio})
+            else:
+                self.client.core.set_torrent_stop_at_ratio(torrent_id, True)
+                self.client.core.set_torrent_stop_ratio(torrent_id, ratio)
+
         except Exception:
             return False
         else:


Test patch: