import datetime
import textwrap
import transmissionrpc

from axel import config
from axel import pb_notify


def clean():
    transmission_client = transmissionrpc.Client(
        config['transmission']['host'], port=config['transmission']['port']
    )

    torrents = transmission_client.get_torrents()
    now = datetime.datetime.now()

    time_threshold = config['transmission']['time_threshold']
    for torrent in torrents:
        if torrent.status == 'seeding':
            done = torrent.date_done
            diff = now - done

            if diff.days >= time_threshold:
                # TODO: Use pb_notify instead
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {torrent} older than {days} days:
                            removing (with data)
                        '''.format(torrent=torrent.name, days=time_threshold)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )
            elif torrent.ratio >= config['transmission']['ratio_threshold']:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {0} reached threshold ratio or higher:
                            removing (with data)
                        '''.format(torrent.name)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )

import datetime
import textwrap
import transmissionrpc

from axel import config
from axel import pb_notify


def clean():
    transmission_client = transmissionrpc.Client(
        config['transmission']['host'], port=config['transmission']['port']
    )

    torrents = transmission_client.get_torrents()
    now = datetime.datetime.now()

    time_threshold = config['transmission']['time_threshold']
    for torrent in torrents:
        if torrent.status in ('seeding', 'stopped'):
            done = torrent.date_done
            diff = now - done

            if diff.days >= time_threshold:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {torrent} older than {days} days:
                            removing (with data)
                        '''.format(torrent=torrent.name, days=time_threshold)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )
            elif torrent.ratio >= config['transmission']['ratio_threshold']:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {0} reached threshold ratio or higher:
                            removing (with data)
                        '''.format(torrent.name)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )

Check stopped torrents when cleaning
Check stopped torrents when cleaning

  import datetime
  import textwrap
  import transmissionrpc
  
  from axel import config
  from axel import pb_notify
  
  
  def clean():
      transmission_client = transmissionrpc.Client(
          config['transmission']['host'], port=config['transmission']['port']
      )
  
      torrents = transmission_client.get_torrents()
      now = datetime.datetime.now()
  
      time_threshold = config['transmission']['time_threshold']
      for torrent in torrents:
-         if torrent.status == 'seeding':
+         if torrent.status in ('seeding', 'stopped'):
              done = torrent.date_done
              diff = now - done
  
              if diff.days >= time_threshold:
-                 # TODO: Use pb_notify instead
                  pb_notify(
                      textwrap.dedent(
                          '''
                              Torrent {torrent} older than {days} days:
                              removing (with data)
                          '''.format(torrent=torrent.name, days=time_threshold)
                      ).strip()
                  )
  
                  transmission_client.remove_torrent(
                      torrent.id, delete_data=True
                  )
              elif torrent.ratio >= config['transmission']['ratio_threshold']:
                  pb_notify(
                      textwrap.dedent(
                          '''
                              Torrent {0} reached threshold ratio or higher:
                              removing (with data)
                          '''.format(torrent.name)
                      ).strip()
                  )
  
                  transmission_client.remove_torrent(
                      torrent.id, delete_data=True
                  )
  
Check stopped torrents when cleaning
## Code Before:
import datetime
import textwrap
import transmissionrpc

from axel import config
from axel import pb_notify


def clean():
    transmission_client = transmissionrpc.Client(
        config['transmission']['host'], port=config['transmission']['port']
    )

    torrents = transmission_client.get_torrents()
    now = datetime.datetime.now()

    time_threshold = config['transmission']['time_threshold']
    for torrent in torrents:
        if torrent.status == 'seeding':
            done = torrent.date_done
            diff = now - done

            if diff.days >= time_threshold:
                # TODO: Use pb_notify instead
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {torrent} older than {days} days:
                            removing (with data)
                        '''.format(torrent=torrent.name, days=time_threshold)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )
            elif torrent.ratio >= config['transmission']['ratio_threshold']:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {0} reached threshold ratio or higher:
                            removing (with data)
                        '''.format(torrent.name)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )

## Instruction:
Check stopped torrents when cleaning
## Code After:
import datetime
import textwrap
import transmissionrpc

from axel import config
from axel import pb_notify


def clean():
    transmission_client = transmissionrpc.Client(
        config['transmission']['host'], port=config['transmission']['port']
    )

    torrents = transmission_client.get_torrents()
    now = datetime.datetime.now()

    time_threshold = config['transmission']['time_threshold']
    for torrent in torrents:
        if torrent.status in ('seeding', 'stopped'):
            done = torrent.date_done
            diff = now - done

            if diff.days >= time_threshold:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {torrent} older than {days} days:
                            removing (with data)
                        '''.format(torrent=torrent.name, days=time_threshold)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )
            elif torrent.ratio >= config['transmission']['ratio_threshold']:
                pb_notify(
                    textwrap.dedent(
                        '''
                            Torrent {0} reached threshold ratio or higher:
                            removing (with data)
                        '''.format(torrent.name)
                    ).strip()
                )

                transmission_client.remove_torrent(
                    torrent.id, delete_data=True
                )
