# 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
        if last_purge_date is None:
            LOG.info('First running...setting last purge date to now')
            # never been purged so need to set an initial date (today)
            self.save_appstate(models.AppState('last_purge_date',
                                               timeutil.utcnow()))
            return

        if timeutil.is_older_than(last_purge_date, timeutil.ONE_WEEK):
            LOG.debug('last purge > 1 week ago....ready for some clean up.')
            # ready to start purge process....
            torrents = list(self.get_torrents_eligible_for_purging())
            if torrents:
                LOG.debug('found torrents eligible for purging: %s',
                          len(torrents))
                # perform database backup
                self.backup()

                LOG.debug('purging media associated with torrents....')
                qfilter = {
                    'in': {'torrent_id': [tor.torrent_id for tor in torrents]}
                }
                self.delete_medias(qfilter)

                qfilter = {
                    'in': {'id': [tor.torrent_id for tor in torrents]}
                }
                self.bulk_save_torrents({'purged': True}, qfilter)

                # now reset the last purge date to now.
                self.save_appstate(models.AppState('last_purge_date',

# 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

        if last_purge_date is None:
            LOG.info('First running...setting last purge date to now')
            # never been purged so need to set an initial date (today)
            self.save_appstate(models.AppState('last_purge_date',
                                               timeutil.utcnow()))
            return

        if timeutil.is_older_than(last_purge_date, timeutil.ONE_WEEK):
            LOG.debug('last purge > 1 week ago....ready for some clean up.')
            # ready to start purge process....
            torrents = list(self.get_torrents_eligible_for_purging())
            if torrents:
                LOG.debug('found torrents eligible for purging: %s',
                          len(torrents))
                # perform database backup
                self.backup()

                LOG.debug('purging media associated with torrents....')
                qfilter = {
                    'in': {'torrent_id': [tor.torrent_id for tor in torrents]}
                }
                self.delete_medias(qfilter)

                qfilter = {
                    'in': {'id': [tor.torrent_id for tor in torrents]}
                }
                self.bulk_save_torrents({'purged': True}, qfilter)

                # now reset the last purge date to now.
                self.save_appstate(models.AppState('last_purge_date',

                                                   timeutil.utcnow()))