Repository: Flexget/Flexget

Issue:
Crash when using convert_magnet with aria2.
There is a crash when using convert_magnet with aria2.

```
2017-01-02 19:51 CRITICAL task          discover_movies_hd BUG: Unhandled error in plugin aria2: u'file'
2017-01-02 19:51 CRITICAL manager       discover_movies_hd An unexpected crash has occurred. Writing crash report to /home/wyrm/.flexget/crash_report.2017.01.02.195150778857.log. Please verify you are running the latest version of flexget by using "flexget -V" from CLI or by using version_checker plugin at http://flexget.com/wiki/Plugins/version_checker. You are currently using version 2.8.17.dev
2017-01-02 19:51 WARNING  task          discover_movies_hd Aborting task (plugin: aria2)

```
I don't have a crashlog, sorry.


Discussion:
This was caused by a conflicting convert_magnet.py file. Sorry for the noise.
This still happens when using the current convert_magnet and the convert_magnet plugin fails.

```
2017-01-04 20:00 INFO     urlrewriter   discover_movies_hd Entry 'Star Trek: The Motion Picture (1979) 720p BrRip x264 - YIFY' URL rewritten to magnet:?xt=urn:btih:7cefc3469df978527e67c64d486de0cb78941fd0&dn=Star+Trek%3A+The+Motion+Picture+%281979%29+720p+BrRip+x264+-+YIFY&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fpublic.popcorn-tracker.org%3A6969 (with piratebay)
2017-01-04 20:00 INFO     convert_magnet discover_movies_hd Converting entry Star Trek: The Motion Picture (1979) 720p BrRip x264 - YIFY magnet URI to a torrent file
2017-01-04 20:00 ERROR    convert_magnet discover_movies_hd Unable to convert Magnet URI for entry Star Trek: The Motion Picture (1979) 720p BrRip x264 - YIFY: No registered converter was able to produce a C++ rvalue of type libtorrent::big_number from this Python object of type newbytes
2017-01-04 20:00 WARNING  download      discover_movies_hd Can't download magnet url
2017-01-04 20:00 INFO     download      discover_movies_hd Downloading: Star Trek: The Motion Picture (1979) 720p BrRip x264 - YIFY
2017-01-04 20:01 VERBOSE  details       discover_movies_hd Summary - Accepted: 1 (Rejected: 104 Undecided: 31 Failed: 0)
2017-01-04 20:01 INFO     aria2         discover_movies_hd Connecting to daemon at http://xxx:xxx@192.168.1.96:6800/rpc
2017-01-04 20:01 CRITICAL task          discover_movies_hd BUG: Unhandled error in plugin aria2: u'file'
2017-01-04 20:01 CRITICAL manager       discover_movies_hd An unexpected crash has occurred. Writing crash report to /home/wyrm/.flexget/crash_report.2017.01.04.200108381074.log. Please verify you are running the latest version of flexget by using "flexget -V" from CLI or by using version_checker plugin at http://flexget.com/wiki/Plugins/version_checker. You are currently using version 2.8.17.dev
2017-01-04 20:01 WARNING  task          discover_movies_hd Aborting task (plugin: aria2)
```
```
2017-01-04 20:01 CRITICAL task          discover_movies_hd BUG: Unhandled error in plugin aria2: u'file'
Traceback (most recent call last):
  File "/home/wyrm/flexget/flexget/task.py", line 483, in __run_plugin
    return method(*args, **kwargs)
  File "/home/wyrm/flexget/flexget/event.py", line 23, in __call__
    return self.func(*args, **kwargs)
  File "/home/wyrm/flexget/flexget/plugins/clients/aria2.py", line 89, in on_task_output
    self.add_entry(aria2, entry, config)
  File "/home/wyrm/flexget/flexget/plugins/clients/aria2.py", line 116, in add_entry
    return aria2.addTorrent(xmlrpc.client.Binary(open(entry['file'], mode='rb').read()))
  File "/home/wyrm/flexget/flexget/utils/lazy_dict.py", line 71, in __getitem__
    item = self.store[key]
KeyError: u'file'
```
Same also happens if convert_magnet succeeds, but download plugin is enabled at the same time as aria2.

Resolving patch:
diff --git a/flexget/plugins/clients/aria2.py b/flexget/plugins/clients/aria2.py
--- a/flexget/plugins/clients/aria2.py
+++ b/flexget/plugins/clients/aria2.py
@@ -111,9 +111,17 @@ def add_entry(self, aria2, entry, config):
             secret = 'token:%s' % config['secret']
         # handle torrent files
         if 'torrent' in entry:
+            if 'file' in entry:
+                torrent_file = entry['file']
+            elif 'location' in entry:
+                # in case download plugin moved the file elsewhere
+                torrent_file = entry['location']
+            else:
+                entry.fail('Cannot find torrent file')
+                return
             if secret:
-                return aria2.addTorrent(secret, xmlrpc.client.Binary(open(entry['file'], mode='rb').read()))
-            return aria2.addTorrent(xmlrpc.client.Binary(open(entry['file'], mode='rb').read()))
+                return aria2.addTorrent(secret, xmlrpc.client.Binary(open(torrent_file, mode='rb').read()), [], options)
+            return aria2.addTorrent(xmlrpc.client.Binary(open(torrent_file, mode='rb').read()), [], options)
         # handle everything else (except metalink -- which is unsupported)
         # so magnets, https, http, ftp .. etc
         if secret:
diff --git a/flexget/plugins/modify/convert_magnet.py b/flexget/plugins/modify/convert_magnet.py
--- a/flexget/plugins/modify/convert_magnet.py
+++ b/flexget/plugins/modify/convert_magnet.py
@@ -33,8 +33,10 @@ def magnet_to_torrent(self, magnet_uri, destination_folder, timeout):
         import libtorrent
         params = libtorrent.parse_magnet_uri(magnet_uri)
         session = libtorrent.session()
-        # for some reason the info_hash needs to be bytes but it's a struct called sha1_hash
-        params['info_hash'] = bytes(params['info_hash'])
+        lt_version = [int(v) for v in libtorrent.version.split('.')]
+        if lt_version > [0,16,13,0]:
+            # for some reason the info_hash needs to be bytes but it's a struct called sha1_hash
+            params['info_hash'] = params['info_hash'].to_bytes()
         handle = libtorrent.add_magnet_uri(session, magnet_uri, params)
         log.debug('Acquiring torrent metadata for magnet %s', magnet_uri)
         timeout_value = timeout


Test patch: