Ejemplo n.º 1
0
    def process_transfer(self, id):
        transfer = Transfer.find_one({'_id': id, 'queued': None})
        if not transfer:
            return

        try:
            info = get_torrent_client().add_torrent(transfer['src'])
            Transfer.update({'_id': transfer['_id']},
                    {'$set': {
                        'queued': datetime.utcnow(),
                        'info': info,
                    }}, safe=True)
        except TorrentExists, e:
            Transfer.update({'_id': transfer['_id']},
                    {'$set': {'finished': datetime.utcnow()}},
                    safe=True)
            logger.debug('failed to start torrent: %s', str(e))
Ejemplo n.º 2
0
def manage_torrents():
    client = get_torrent_client()

    for transfer in Transfer.find({
            'started': {'$ne': None},
            'finished': None,
            'type': 'torrent',
            }):
        hash = transfer['info'].get('hash')
        if not hash:
            continue
        try:
            torrent = client.get_torrent(hash=hash)
        except TorrentError:
            continue
        if not torrent:
            transfer['finished'] = datetime.utcnow()
            logger.debug('torrent %s is not queued', hash)
        else:
            transfer['info'] = torrent
            transfer['transferred'] = torrent.transferred
            transfer['total'] = torrent.size
            transfer['progress'] = torrent.progress
        Transfer.save(transfer, safe=True)

    default_dir = str(Settings.get_settings('paths')['default'])
    for torrent in client.iter_torrents():
        transfer = Transfer.find_one({'info.hash': torrent.hash},
                sort=[('created', DESCENDING)])
        if not transfer or (transfer['finished'] \
                and torrent.date_added > transfer['finished']):
            now = datetime.utcnow()
            Transfer.add(torrent.magnet_url, default_dir,
                    type='torrent', added=now, started=now,
                    info={'hash': torrent.hash})
            logger.info('started torrent transfer %s to %s', torrent.magnet_url, default_dir)
        elif transfer['finished']:
            client.remove_torrent(hash=torrent.hash, delete_data=True)
            logger.debug('removed finished torrent "%s" (%s)', torrent.name, torrent.hash)
        else:
            manage_torrent(client, hash=torrent.hash, dst=transfer['dst'])
Ejemplo n.º 3
0
def clean_torrents():
    try:
        get_torrent_client().clean_download_directory()
    except TransmissionError, e:
        logger.error('failed to get torrent client: %s', str(e))