def run(self, uri_list): """ Takes a list of uri and returns when they are downloaded Args: uri_list (iterable): a list of strings Returns: None """ downloaders = [] # Generators to handle in_queue = [] # List of "TODO" uris _download_infos = dict(count=0, start_ts = time.time()) percent_memory = WeakKeyDictionary() write_out = sys.stdout.write def _download(): for dl in downloaders: try: ret = dl.next() except StopIteration: downloaders.remove(dl) _download_infos['count'] += 1 else: if isinstance(ret, int): percent_memory[dl] = ret # Display things t = time.time() if self._last_display + 0.1 < t: self._last_display = t sumup = ', '.join('%3d%%'%(val if int(val)<=100 else 0) for val in percent_memory.itervalues()) write_out(' [ %s ] %d \r'%(sumup, _download_infos['count'])) sys.stdout.flush() for uri in chain( uri_list, in_queue ): if len(downloaders) < self._nb_dl: try: dg = DownloadGenerator(uri) dg.next() # start the pump downloaders.append( dg ) except StopIteration: pass else: in_queue.append(uri) # Later please ! # iterate _download() # Terminate the job while downloaders: _download() t = time.time() - _download_infos['start_ts'] write_out(" \nGot %d files in %s. Enjoy ;)\n"%( _download_infos['count'], duration_tidy(t)))
def do_search(out=None, edit_mode=False): """ Search for song, display results. out can be "m3u" or "null", defaults to human-readable """ duration = 0 start_t = time() fields = list(valid_tags) fields.remove('filename') fields = tuple(fields) if callable(out): song_output = out elif out == 'm3u': print "#EXTM3U" def song_output(song): print u"#EXTINF:%d,%s - %s\n%s"%(song.length, song.artist, song.title, song.filename) elif out == 'null': def song_output(song): pass else: def song_output(song): txt = '%s :\n%s [%s, score: %s, tags: %s]'%(song.filename, '%s - %s - %s'%(song.artist, song.album, song.title), duration_tidy(song.length), song.score, song.tags, ) print txt.decode('utf8').encode('utf8') pat = string2python(' '.join(zshell.args)) if edit_mode: search_fn = zshell.songs.u_search else: search_fn = zshell.songs.search num = 0 for num, res in enumerate(search_fn(None, pat)): song_output(res) duration += res.length sys.stderr.write("# %d results in %s for a total of %s!\n"%( num, duration_tidy(time()-start_t), duration_tidy(duration) ))
def do_scan(up=False): """ Scan a directory for songs (fill Database) See "help" for a more complete documentation parameter: up: if True, updates tags of already scanned songs """ if not zshell.args: sys.exit('At least one argument must be specified!') orig_nb = len(zshell.songs) start_t = time() archives = [] directories = [] for path in zshell.args: path = clean_path(path) if os.path.isdir(path): directories.append(path) else: archives.append(path) for path in archives: _scan(archive=path, db_name=zshell.songs.db_name) for path in directories: _scan(directory=path, db_name=zshell.songs.db_name, update=up) elapsed = time() - start_t delta = len(zshell.songs)-orig_nb print "\nProcessed %d (%s%d) songs in %s (%.2f/s.)"%( len(zshell.songs), '-' if delta < 0 else '+', abs(delta), duration_tidy(elapsed), len(zshell.songs)/elapsed)
def song_output(song): txt = '%s :\n%s [%s, score: %s, tags: %s]'%(song.filename, '%s - %s - %s'%(song.artist, song.album, song.title), duration_tidy(song.length), song.score, song.tags, ) print txt.decode('utf8').encode('utf8')