datashaman / putio-automator

A suite of commands for managing torrents, transfers and files on put.io. Works well with SickRage, Sonarr, etc.
MIT License
71 stars 17 forks source link

torrent watch crashes with .magnet files #15

Closed Thinkscape closed 2 years ago

Thinkscape commented 6 years ago

Running docker container:

# /usr/local/bin/putio torrents watch
Traceback (most recent call last):
  File "/usr/local/bin/putio", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/putio_automator/manage.py", line 47, in main
    manager.run()
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python2.7/dist-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/putio_automator/commands/torrents.py", line 64, in watch
    add()
  File "/usr/local/lib/python2.7/dist-packages/putio_automator/commands/torrents.py", line 52, in add
    with_db(app, func)
  File "/usr/local/lib/python2.7/dist-packages/putio_automator/db.py", line 15, in with_db
    func(connection)
  File "/usr/local/lib/python2.7/dist-packages/putio_automator/commands/torrents.py", line 44, in func
    raise e
KeyError: 'transfer'

It seems that this will happen if the file provided to automator is not a torrent file or otherwise is rejected by put.io (does not result in a new transfer being created).

The error originates in putiopy where they just try to return transfer from put.io response JSON, but the docs state it's optional:

If the file is a torrent file, a transfer object is returned

I guess automator could catch that as well, as it's the point of origin for calling add_torrent and should react better to files being rejected.

cenkalti commented 6 years ago

You can catch ClientErrors, the type attribute of the exception will be NotTorrent. However, it is better to prevent uploading of non-torrent files in the first place.

Thinkscape commented 6 years ago

Nice. Thanks for a quick fix @cenkalti. A file being a torrent or not might not always be obvious, or put.io might not like the input on the backend, hence the requirement.

On the automator side we will still need to catch it, as the "torrent" directory can potentially contain some non-torrent files (for whatever reason). Current behaviour is a crash of the whole watcher process (command). IMO it should catch and continue with remaining files in such case.

datashaman commented 2 years ago

I am testing a hotfix which adds a try/except wrapper around the handler so that it does not crash the thread. https://pypi.org/project/putio-automator/2.0.1.dev4/ This will become version 2.0.1 when released.