Repository: Flexget/Flexget

Issue:
Transmission cleanup crashing
<!---
Before opening an issue, verify:

- Is this a feature request? Post it on https://feathub.com/Flexget/Flexget
- Is this an issue with webui? Make an issue over on https://github.com/Flexget/webui
- Did you recently upgrade? Look at the Change Log and Upgrade Actions to make sure that you don't need to make any changes to your config https://flexget.com/ChangeLog https://flexget.com/UpgradeActions
- Are you running FlexGet as a daemon? Stop it completely and then start it again https://flexget.com/CLI/daemon
- Did you search to see if the issue already exists? https://github.com/Flexget/Flexget/issues
- Did you fill out the issue template as completely as possible?

The issue template is here because it helps to ensure you submitted all the necessary information the first time, and allows us to more quickly review issues. Please fill it out correctly and do not ignore it, no matter how irrelevant you think it may be. Thanks in advance for your help with this!
--->

### Expected behaviour:

<!---
Please don't just say "it doesn't crash" or "it works". Explain what the expected result is.
--->

Remove completed downloads

### Actual behaviour:

Task fails and crashes

### Steps to reproduce:
- Step 1: download something on transmission
- Step 2: create task in flexget config
- Step 3: run task interactively with flexget

#### Config:
```
  cleanup-transmission:
    from_transmission:
      host: '{? transmission.host ?}'
      #only_complete: yes
    disable: builtins
    if:
      - transmission_progress == 100: accept      
    accept_all: yes
    transmission:
      host: '{? transmission.host ?}'
      action: remove
```
  
#### Log:
<details>
 <summary>(click to expand)</summary>

```
2021-07-16 12:00:12 DEBUG    manager       cleanup-transmission-local Not running db cleanup, last run 2021-07-09 18:00:00
2021-07-16 12:00:12 DEBUG    scheduler                     task finished executing: cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    task          cleanup-transmission-local executing cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    template      cleanup-transmission-local Merging template cleanup-transmission into task cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    template      cleanup-transmission-local Merging template global into task cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    disable       cleanup-transmission-local Disabled plugins: remember_rejected, seen_info_hash, nzb_size, metainfo_task, retry_failed, torrent_size, urlrewriting, dump, proxy, change_warn, metainfo_quality, --try-regexp, seen, template, series_db, verbose, dump_config, torrent_files, metainfo_content_size, history, torrent, entry_operations, metainfo_media_id, magnet_btih, details, scan_imdb, include, urlfix, backlog, status
2021-07-16 12:00:12 DEBUG    transmission  cleanup-transmission-local Connecting to http://TRANSMISSION_HOST:9091/transmission/
2021-07-16 12:00:12 CRITICAL task          cleanup-transmission-local BUG: Unhandled error in plugin from_transmission: can't compare offset-naive and offset-aware datetimes
Traceback (most recent call last):

  File "/usr/local/lib/python3.9/threading.py", line 912, in _bootstrap
    self._bootstrap_inner()
    │    └ <function Thread._bootstrap_inner at 0x7f69ae33d3a0>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
    │    └ <function Thread.run at 0x7f69ae33d0d0>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
    │    │        │    │        │    └ {}
    │    │        │    │        └ <Thread(task_queue, started daemon 140091748047672)>
    │    │        │    └ ()
    │    │        └ <Thread(task_queue, started daemon 140091748047672)>
    │    └ <bound method TaskQueue.run of <flexget.task_queue.TaskQueue object at 0x7f69a7fd8d60>>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/site-packages/flexget/task_queue.py", line 46, in run
    self.current_task.execute()
    │    │            └ <function Task.execute at 0x7f69ab6c4e50>
    │    └ <flexget.task.Task object at 0x7f69a614bbb0>
    └ <flexget.task_queue.TaskQueue object at 0x7f69a7fd8d60>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 87, in wrapper
    return func(self, *args, **kw)
           │    │      │       └ {}
           │    │      └ ()
           │    └ <flexget.task.Task object at 0x7f69a614bbb0>
           └ <function Task.execute at 0x7f69ab6c4dc0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 722, in execute
    self._execute()
    │    └ <function Task._execute at 0x7f69ab6c4d30>
    └ <flexget.task.Task object at 0x7f69a614bbb0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 688, in _execute
    self.__run_task_phase(phase)
    │                     └ 'input'
    └ <flexget.task.Task object at 0x7f69a614bbb0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 514, in __run_task_phase
    response = self.__run_plugin(plugin, phase, args)
               │                 │       │      └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
               │                 │       └ 'input'
               │                 └ <PluginInfo(name=from_transmission)>
               └ <flexget.task.Task object at 0x7f69a614bbb0>
> File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 547, in __run_plugin
    result = method(*args, **kwargs)
             │       │       └ {}
             │       └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
             └ <Event(name=plugin.from_transmission.input,func=on_task_input,priority=128)>
  File "/usr/local/lib/python3.9/site-packages/flexget/event.py", line 20, in __call__
    return self.func(*args, **kwargs)
           │    │     │       └ {}
           │    │     └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
           │    └ <bound method PluginTransmissionInput.on_task_input of <flexget.plugins.clients.transmission.PluginTransmissionInput object a...
           └ <Event(name=plugin.from_transmission.input,func=on_task_input,priority=128)>
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 197, in on_task_input
    seed_ratio_ok, idle_limit_ok = self.check_seed_limits(torrent, session)
                                   │    │                 │        └ <transmission_rpc.session.Session object at 0x7f69a5f97bb0>
                                   │    │                 └ <Torrent 2 "TORRENT">
                                   │    └ <function TransmissionBase.check_seed_limits at 0x7f69a965a790>
                                   └ <flexget.plugins.clients.transmission.PluginTransmissionInput object at 0x7f69a80b82e0>
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 131, in check_seed_limits
    torrent.date_active + timedelta(minutes=session.idle_seeding_limit)
    │       │             │                 └ <transmission_rpc.session.Session object at 0x7f69a5f97bb0>
    │       │             └ <class 'datetime.timedelta'>
    │       └ <property object at 0x7f69a96cfdb0>
    └ <Torrent 2 "[SubsPlease] Obey Me! - 01 (720p) [5C677718].mkv">

TypeError: can't compare offset-naive and offset-aware datetimes
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 547, in __run_plugin
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flexget/event.py", line 20, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 197, in on_task_input
    seed_ratio_ok, idle_limit_ok = self.check_seed_limits(torrent, session)
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 131, in check_seed_limits
    torrent.date_active + timedelta(minutes=session.idle_seeding_limit)
TypeError: can't compare offset-naive and offset-aware datetimes

```
</details>

### Additional information:

- FlexGet version: 3.1.131
- transmission-rpc version: 3.2.6
- Transmission version: 3.0
- Python version: 3.9.4
- Installation method: pip
- Using daemon (yes/no): yes
- OS and version: Alpine 3.13.4 in Docker container

<!---
In config and debug/crash logs, remember to redact any personal or sensitive information such as passwords, API keys, private URLs and so on.

Please verify that the following data is present before submitting your issue:

- Link to a paste service or paste above the relevant config (preferably full config, including templates if present). Please make sure the paste does not expire, if possible.
- Link to a paste service or paste above debug-level logs of the relevant task/s (use `flexget -L debug execute --tasks <Task_name>`).
- FlexGet version (use `flexget -V` to get it).
- Full Python version, for example `2.7.11` (use `python -V` to get it). 
- Installation method (pip, git install, etc).
- Whether or not you're running FlexGet as a daemon.
- OS and version.
- Attach crash log if one was generated, in addition to the debug-level log. It can be found in the directory with your config file.
--->

Transmission cleanup crashing
<!---
Before opening an issue, verify:

- Is this a feature request? Post it on https://feathub.com/Flexget/Flexget
- Is this an issue with webui? Make an issue over on https://github.com/Flexget/webui
- Did you recently upgrade? Look at the Change Log and Upgrade Actions to make sure that you don't need to make any changes to your config https://flexget.com/ChangeLog https://flexget.com/UpgradeActions
- Are you running FlexGet as a daemon? Stop it completely and then start it again https://flexget.com/CLI/daemon
- Did you search to see if the issue already exists? https://github.com/Flexget/Flexget/issues
- Did you fill out the issue template as completely as possible?

The issue template is here because it helps to ensure you submitted all the necessary information the first time, and allows us to more quickly review issues. Please fill it out correctly and do not ignore it, no matter how irrelevant you think it may be. Thanks in advance for your help with this!
--->

### Expected behaviour:

<!---
Please don't just say "it doesn't crash" or "it works". Explain what the expected result is.
--->

Remove completed downloads

### Actual behaviour:

Task fails and crashes

### Steps to reproduce:
- Step 1: download something on transmission
- Step 2: create task in flexget config
- Step 3: run task interactively with flexget

#### Config:
```
  cleanup-transmission:
    from_transmission:
      host: '{? transmission.host ?}'
      #only_complete: yes
    disable: builtins
    if:
      - transmission_progress == 100: accept      
    accept_all: yes
    transmission:
      host: '{? transmission.host ?}'
      action: remove
```
  
#### Log:
<details>
 <summary>(click to expand)</summary>

```
2021-07-16 12:00:12 DEBUG    manager       cleanup-transmission-local Not running db cleanup, last run 2021-07-09 18:00:00
2021-07-16 12:00:12 DEBUG    scheduler                     task finished executing: cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    task          cleanup-transmission-local executing cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    template      cleanup-transmission-local Merging template cleanup-transmission into task cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    template      cleanup-transmission-local Merging template global into task cleanup-transmission-local
2021-07-16 12:00:12 DEBUG    disable       cleanup-transmission-local Disabled plugins: remember_rejected, seen_info_hash, nzb_size, metainfo_task, retry_failed, torrent_size, urlrewriting, dump, proxy, change_warn, metainfo_quality, --try-regexp, seen, template, series_db, verbose, dump_config, torrent_files, metainfo_content_size, history, torrent, entry_operations, metainfo_media_id, magnet_btih, details, scan_imdb, include, urlfix, backlog, status
2021-07-16 12:00:12 DEBUG    transmission  cleanup-transmission-local Connecting to http://TRANSMISSION_HOST:9091/transmission/
2021-07-16 12:00:12 CRITICAL task          cleanup-transmission-local BUG: Unhandled error in plugin from_transmission: can't compare offset-naive and offset-aware datetimes
Traceback (most recent call last):

  File "/usr/local/lib/python3.9/threading.py", line 912, in _bootstrap
    self._bootstrap_inner()
    │    └ <function Thread._bootstrap_inner at 0x7f69ae33d3a0>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
    │    └ <function Thread.run at 0x7f69ae33d0d0>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
    │    │        │    │        │    └ {}
    │    │        │    │        └ <Thread(task_queue, started daemon 140091748047672)>
    │    │        │    └ ()
    │    │        └ <Thread(task_queue, started daemon 140091748047672)>
    │    └ <bound method TaskQueue.run of <flexget.task_queue.TaskQueue object at 0x7f69a7fd8d60>>
    └ <Thread(task_queue, started daemon 140091748047672)>
  File "/usr/local/lib/python3.9/site-packages/flexget/task_queue.py", line 46, in run
    self.current_task.execute()
    │    │            └ <function Task.execute at 0x7f69ab6c4e50>
    │    └ <flexget.task.Task object at 0x7f69a614bbb0>
    └ <flexget.task_queue.TaskQueue object at 0x7f69a7fd8d60>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 87, in wrapper
    return func(self, *args, **kw)
           │    │      │       └ {}
           │    │      └ ()
           │    └ <flexget.task.Task object at 0x7f69a614bbb0>
           └ <function Task.execute at 0x7f69ab6c4dc0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 722, in execute
    self._execute()
    │    └ <function Task._execute at 0x7f69ab6c4d30>
    └ <flexget.task.Task object at 0x7f69a614bbb0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 688, in _execute
    self.__run_task_phase(phase)
    │                     └ 'input'
    └ <flexget.task.Task object at 0x7f69a614bbb0>
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 514, in __run_task_phase
    response = self.__run_plugin(plugin, phase, args)
               │                 │       │      └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
               │                 │       └ 'input'
               │                 └ <PluginInfo(name=from_transmission)>
               └ <flexget.task.Task object at 0x7f69a614bbb0>
> File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 547, in __run_plugin
    result = method(*args, **kwargs)
             │       │       └ {}
             │       └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
             └ <Event(name=plugin.from_transmission.input,func=on_task_input,priority=128)>
  File "/usr/local/lib/python3.9/site-packages/flexget/event.py", line 20, in __call__
    return self.func(*args, **kwargs)
           │    │     │       └ {}
           │    │     └ (<flexget.task.Task object at 0x7f69a614bbb0>, {'port': 9091, 'host': 'TRANSMISSION_HOST', 'enabled': True, 'main_file_ratio': 0.9, 'o...
           │    └ <bound method PluginTransmissionInput.on_task_input of <flexget.plugins.clients.transmission.PluginTransmissionInput object a...
           └ <Event(name=plugin.from_transmission.input,func=on_task_input,priority=128)>
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 197, in on_task_input
    seed_ratio_ok, idle_limit_ok = self.check_seed_limits(torrent, session)
                                   │    │                 │        └ <transmission_rpc.session.Session object at 0x7f69a5f97bb0>
                                   │    │                 └ <Torrent 2 "TORRENT">
                                   │    └ <function TransmissionBase.check_seed_limits at 0x7f69a965a790>
                                   └ <flexget.plugins.clients.transmission.PluginTransmissionInput object at 0x7f69a80b82e0>
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 131, in check_seed_limits
    torrent.date_active + timedelta(minutes=session.idle_seeding_limit)
    │       │             │                 └ <transmission_rpc.session.Session object at 0x7f69a5f97bb0>
    │       │             └ <class 'datetime.timedelta'>
    │       └ <property object at 0x7f69a96cfdb0>
    └ <Torrent 2 "[SubsPlease] Obey Me! - 01 (720p) [5C677718].mkv">

TypeError: can't compare offset-naive and offset-aware datetimes
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flexget/task.py", line 547, in __run_plugin
    result = method(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flexget/event.py", line 20, in __call__
    return self.func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 197, in on_task_input
    seed_ratio_ok, idle_limit_ok = self.check_seed_limits(torrent, session)
  File "/usr/local/lib/python3.9/site-packages/flexget/plugins/clients/transmission.py", line 131, in check_seed_limits
    torrent.date_active + timedelta(minutes=session.idle_seeding_limit)
TypeError: can't compare offset-naive and offset-aware datetimes

```
</details>

### Additional information:

- FlexGet version: 3.1.131
- transmission-rpc version: 3.2.6
- Transmission version: 3.0
- Python version: 3.9.4
- Installation method: pip
- Using daemon (yes/no): yes
- OS and version: Alpine 3.13.4 in Docker container

<!---
In config and debug/crash logs, remember to redact any personal or sensitive information such as passwords, API keys, private URLs and so on.

Please verify that the following data is present before submitting your issue:

- Link to a paste service or paste above the relevant config (preferably full config, including templates if present). Please make sure the paste does not expire, if possible.
- Link to a paste service or paste above debug-level logs of the relevant task/s (use `flexget -L debug execute --tasks <Task_name>`).
- FlexGet version (use `flexget -V` to get it).
- Full Python version, for example `2.7.11` (use `python -V` to get it). 
- Installation method (pip, git install, etc).
- Whether or not you're running FlexGet as a daemon.
- OS and version.
- Attach crash log if one was generated, in addition to the debug-level log. It can be found in the directory with your config file.
--->



Discussion:
Looks like the library returns timezone aware datetimes. I think we just need to start using timezone aware `now`. i.e. `datetime.now(tzinfo=timezone.utc)`
sounds promising. I also get this occastionally on startup: 

```
/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py:1213: UnknownTimezoneWarning: tzname CEST identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.
today at 12:10:04 PM   warnings.warn("tzname {tzname} identified but not understood.  "
```
Looks like the library returns timezone aware datetimes. I think we just need to start using timezone aware `now`. i.e. `datetime.now(tzinfo=timezone.utc)`
sounds promising. I also get this occastionally on startup: 

```
/usr/local/lib/python3.9/site-packages/dateutil/parser/_parser.py:1213: UnknownTimezoneWarning: tzname CEST identified but not understood.  Pass `tzinfos` argument in order to correctly return a timezone-aware datetime.  In a future version, this will raise an exception.
today at 12:10:04 PM   warnings.warn("tzname {tzname} identified but not understood.  "
```

Resolving patch:
diff --git a/flexget/plugins/clients/transmission.py b/flexget/plugins/clients/transmission.py
--- a/flexget/plugins/clients/transmission.py
+++ b/flexget/plugins/clients/transmission.py
@@ -123,13 +123,13 @@ def check_seed_limits(self, torrent, session):
 
         if torrent.seedIdleMode == 1:  # use torrent's own idle limit
             idle_limit_ok = (
-                torrent.date_active + timedelta(minutes=torrent.seedIdleLimit) < datetime.now()
+                torrent.date_active + timedelta(minutes=torrent.seedIdleLimit) < datetime.now().astimezone()
             )
         elif torrent.seedIdleMode == 0:  # use global rules
             if session.idle_seeding_limit_enabled:
                 idle_limit_ok = (
                     torrent.date_active + timedelta(minutes=session.idle_seeding_limit)
-                    < datetime.now()
+                    < datetime.now().astimezone()
                 )
 
         return seed_limit_ok, idle_limit_ok


Test patch: