def check_state(session): logger.debug('start') status = session['status'] if 'key' not in session: status = bitops.sub(status, 3) status = bitops.sub(status, 4) status = bitops.sub(status, 5) status = bitops.add(status, 2) save_status(session, status)
def download_from_btsync(session): """Download from btsync and saves data to DB In db.btsync `download` flag has the following meaning: 0 - not synced via btsync 1 - synced via btsync 2 - processed and uploaded to Flickr but synced via btsync 3 - processed and uploaded to Flickr and unsynced via btsync 4 - unsynced via btsync because not squared 5 - processed and not matched but synced via btsync 6 - processed and not matched and unsynced via btsync """ logger.debug('start') check_dirs(session) status = session['status'] dir = get_dirs(session)[2] if dir[0] != '/': # used relative path dir = os.path.join(os.getcwd(), dir) if 'key' not in session: return key = session['key'] folder = btsync.request(method='get_folders', secret=key) if not folder: # folder is already added to sync logger.info('adding folder: %s', dir) response = btsync.request(method='add_folder', dir=dir, secret=key, selective_sync=1) if response['result'] != 0: logger.error('btsync error: %s', response) return # process files on the next run when some images will be downloaded check_filename = lambda x: x['name'].lower()[-4:] == '.jpg' # get only jpegs files = filter(check_filename, btsync.btsync_files(secret=key)) db_files = [x for x in db.btsync.find({'owner': session['username']})] # files already in db new_files = filter(lambda x: x['name'] not in [x['name'] for x in db_files], files) new_files = [dict(owner=session['username'], name=x['name'], size=x['size'], download=x['download']) for x in new_files] logger.info('found %d new files', len(new_files)) if new_files: db.btsync.insert(new_files) status = bitops.add(status, 1) db_files += new_files def download(*statuses): #logger.debug(session) def f(x): return x['download'] in statuses return f logger.debug(session) logger.debug(session) def stop_sync(file, status): logger.info('stopping sync file %s', file['name']) response = btsync.request(method='set_file_prefs', secret=key, path=file['name'], download=0) logger.debug('response from btsync on file %s: %s', file['name'], response) if response[0]['state'] != 'deleted': try: os.remove(os.path.join(dir, file['name'])) except OSError, e: logger.error(e) if e.errno != 2: return file['download'] = status db.btsync.save(file)