# Path: seedbox/common/timeutil.py
# def utcnow():
# def is_older_than(before, seconds):
# def is_newer_than(after, seconds):
# def delta_seconds(before, after):
# def total_seconds(delta):
# ONE_WEEK = total_seconds(datetime.timedelta(weeks=1))
# 
# Path: seedbox/constants.py
# INIT = 'init'
# READY = 'ready'
# ACTIVE = 'active'
# DONE = 'done'
# CANCELLED = 'cancelled'
# STATES = [INIT, READY, ACTIVE, DONE, CANCELLED]
# ACTIVE_STATES = [INIT, READY, ACTIVE]
# INACTIVE_STATES = [DONE, CANCELLED]
# 
# Path: seedbox/db/models.py
# class Model(object):
# class Torrent(Model):
# class MediaFile(Model):
# class AppState(Model):
#     PK_NAME = 'id'
#     PK_NAME = 'torrent_id'
#     PK_NAME = 'media_id'
#     PK_NAME = 'name'
#     def __init__(self, **kwds):
#     def as_dict(self):
#     def items(self):
#     def __eq__(self, other):
#     def __iter__(self):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value):
#     def __contains__(self, name):
#     def __str__(self):
#     def pk_filter(cls, value=None):
#     def make_empty(cls):
#     def __init__(self, torrent_id, name, created_at=None, updated_at=None,
#                  state=None, retry_count=None, failed=None, error_msg=None,
#                  invalid=None, purged=None, media_files=None):
#     def __init__(self, media_id, torrent_id, filename, file_ext,
#                  file_path=None, size=None, compressed=None, synced=None,
#                  missing=None, skipped=None, error_msg=None, total_time=None):
#     def __init__(self, name, value):


import logging
import os
from seedbox.common import timeutil
from seedbox import constants
from seedbox.db import models
                           ]}
        return self.get_torrents(qfilter)

    def get_torrents_by_state(self, state, failed=False):
        """Retrieve torrents by specified state.

        Perform select operation using a pre-defined criteria to get torrents
        by a specific state.

        :param state: name of the state of the torrent
        :param failed: flag indicating to include failed entries or not
        :return: torrent instance(s)
        :rtype: :class:`~seedbox.db.models.Torrent`
        """
        qfilter = {'and': [{'=': {'state': state}},
                           {'=': {'failed': True if failed else False}}
                           ]}
        return self.get_torrents(qfilter)

    def get_torrents_eligible_for_purging(self):
        """Retrieves eligible to purge.

        Perform select operation using a pre-defined criteria for what
        constitutes eligible for purging.

        :return: torrent instance(s)
        :rtype: :class:`~seedbox.db.models.Torrent`
        """
        qfilter = {'and': [{'=': {'invalid': False}},
                           {'=': {'purged': False}},

# Path: seedbox/common/timeutil.py
# def utcnow():
# def is_older_than(before, seconds):
# def is_newer_than(after, seconds):
# def delta_seconds(before, after):
# def total_seconds(delta):
# ONE_WEEK = total_seconds(datetime.timedelta(weeks=1))
# 
# Path: seedbox/constants.py
# INIT = 'init'
# READY = 'ready'
# ACTIVE = 'active'
# DONE = 'done'
# CANCELLED = 'cancelled'
# STATES = [INIT, READY, ACTIVE, DONE, CANCELLED]
# ACTIVE_STATES = [INIT, READY, ACTIVE]
# INACTIVE_STATES = [DONE, CANCELLED]
# 
# Path: seedbox/db/models.py
# class Model(object):
# class Torrent(Model):
# class MediaFile(Model):
# class AppState(Model):
#     PK_NAME = 'id'
#     PK_NAME = 'torrent_id'
#     PK_NAME = 'media_id'
#     PK_NAME = 'name'
#     def __init__(self, **kwds):
#     def as_dict(self):
#     def items(self):
#     def __eq__(self, other):
#     def __iter__(self):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value):
#     def __contains__(self, name):
#     def __str__(self):
#     def pk_filter(cls, value=None):
#     def make_empty(cls):
#     def __init__(self, torrent_id, name, created_at=None, updated_at=None,
#                  state=None, retry_count=None, failed=None, error_msg=None,
#                  invalid=None, purged=None, media_files=None):
#     def __init__(self, media_id, torrent_id, filename, file_ext,
#                  file_path=None, size=None, compressed=None, synced=None,
#                  missing=None, skipped=None, error_msg=None, total_time=None):
#     def __init__(self, name, value):


# Path: seedbox/db/api.py
import logging
import os
from seedbox.common import timeutil
from seedbox import constants
from seedbox.db import models

                           ]}
        return self.get_torrents(qfilter)

    def get_torrents_by_state(self, state, failed=False):
        """Retrieve torrents by specified state.

        Perform select operation using a pre-defined criteria to get torrents
        by a specific state.

        :param state: name of the state of the torrent
        :param failed: flag indicating to include failed entries or not
        :return: torrent instance(s)
        :rtype: :class:`~seedbox.db.models.Torrent`
        """
        qfilter = {'and': [{'=': {'state': state}},
                           {'=': {'failed': True if failed else False}}
                           ]}
        return self.get_torrents(qfilter)

    def get_torrents_eligible_for_purging(self):
        """Retrieves eligible to purge.

        Perform select operation using a pre-defined criteria for what
        constitutes eligible for purging.

        :return: torrent instance(s)
        :rtype: :class:`~seedbox.db.models.Torrent`
        """
        qfilter = {'and': [{'=': {'invalid': False}},
                           {'=': {'purged': False}},

                           {'in': {'state': constants.INACTIVE_STATES}}