def clean_nzbs(): try: client = get_nzb_client() nzbs = client.list_nzbs(history=False) + client.list_nzbs(history=True) if nzbs: return config = client.get_config() base_path = str(Settings.get_settings('sabnzbd')['base_path']) for name in ('download_dir', 'complete_dir'): path = config['misc'].get(name) if not path: logger.error('failed to get sabnzbd %s', name) continue path = str(os.path.join(base_path, path)) if not os.path.exists(path): logger.error('sabnzbd %s path %s does not exist', name, path) continue for file in glob(path + '/*'): if remove_file(file): logger.info('removed obsolete sabnzbd path %s', file) except SabnzbdError, e: logger.error('nzb client error: %s', str(e))
def manage_nzbs(): client = get_nzb_client() for transfer in Transfer.find({ 'started': {'$ne': None}, 'finished': None, 'type': 'binsearch', }): nzb_id = transfer['info'].get('nzo_id') if not nzb_id: continue info = client.get_nzb(nzb_id) if not info: Transfer.update({'_id': transfer['_id']}, {'$set': {'finished': datetime.utcnow()}}, safe=True) else: info['name'] = info.get('filename', transfer['info'].get('name')) total = get_float(info.get('mb', 0)) * 1024 ** 2 Transfer.update({'_id': transfer['_id']}, {'$set': { 'total': total, 'transferred': total - get_float(info.get('mbleft', 0)) * 1024 ** 2, 'progress': get_float(info.get('percentage', 0)), 'info': info, }}, safe=True) paths = Settings.get_settings('paths') # Manage queued nzbs for nzb in client.list_nzbs(): transfer = get_nzb_transfer(nzb['nzo_id']) if not transfer: now = datetime.utcnow() Transfer.add(nzb['filename'], str(paths['default']), type='binsearch', added=now, started=now, queued=now, info={'nzo_id': nzb['nzo_id']}) elif transfer['finished']: client.remove_nzb(nzb['nzo_id']) logger.info('removed finished nzb "%s" (%s)', nzb['filename'], nzb['nzo_id']) # Manage finished nzbs for nzb in client.list_nzbs(history=True): transfer = get_nzb_transfer(nzb['nzo_id']) if nzb['status'] == 'Completed': dst = transfer['dst'] if transfer else str(paths['default']) elif nzb['status'] == 'Failed': dst = str(paths['invalid']) else: continue manage_nzb(client, nzb_id=nzb['nzo_id'], dst=dst)