def cd_command(out, *arg): path = memory.get('path', []) arg = ' '.join(arg) if arg == '..': if path: path.pop(-1) elif arg == '/' or not arg: path = [] else: if 0 == len(path): if arg.startswith('ar'): path.append('artist') else: path.append('album') elif 1 == len(path): path.append(arg) elif path[0].startswith('artist') and len(path) == 2: path = ['album', arg] out(['Changed path to album/%s'%arg]) else: out(["Can't go that far ! :)"]) memory['path'] = path
def tidy_show(it): offs = memory['show_offset'] now = int(memory.get('pls_position', -1)) for i, line in enumerate(it): idx = offs+i yield '%3s %s'%(idx if idx != now else ' >> ', ' | '.join(line.split(' | ')[:4]))
def complete_cd(cw, args): a = ' '.join(args[1:]) word = len(args)-2 # remove last (index starts to 0, and first item doesn't count) if not cw: word += 1 # we are already on next word if not memory.get('path'): # nothing in memory lls = ['artist', 'album'] else: lls = memory.get('last_ls') if lls: riri = [w.split() for w in lls if w.startswith(a)] if len(riri) > 1: return (w[word] for w in riri) else: return [' '.join(riri[0][word:])]
def inject_playlist(output, symbol): uri = memory.get('last_search') if not uri: print "Do a search first !" return pattern = uri[0].split('pattern=', 1)[1] # the pattern should be the same for anybody # crazy escaping substr = ("%s%%20pls%%3A%%20%s%%23"%(pattern, quote(symbol))).replace('%', '%%') v = "/search?host=%(db_host)s&pattern="+substr return v
def get_last_search(output): uris = memory.get('last_search') if not uris: print "No previous search, use shell to re-use previous result!" return to_download = [] all_filenames = set() dst_prefix = config.download_dir for infos in unroll(iter_webget(u) for u in memory['last_search']): if infos[-1] == '|': infos += ' ' try: uri, artist, album, title = infos.split(' | ', 3) except ValueError: print repr(infos) import pdb; pdb.set_trace() raise ext = uri.rsplit('.', 1)[1].rsplit('?', 1)[0].strip() if len(ext) < 2: print "WARNING: Ext=%s for %s\n"%(ext, uri) ext = 'mp3' title = title.replace(os.path.sep, '-').split('\n', 1)[0] album = album.replace(os.path.sep, '-').split('\n', 1)[0] artist = artist.replace(os.path.sep, '-').split('\n', 1)[0] out_dir = os.path.join(dst_prefix.strip(), artist.strip(), album.strip()) fname = os.path.join(out_dir, title.strip() or 'unknown') + "." if fname+ext in all_filenames: count = 1 while True: new_name = "%s-%d%s"%(fname, count, ext) if new_name not in all_filenames: fname = new_name break count += 1 else: fname += ext out_dir = os.path.dirname(fname) if not os.path.exists(out_dir): os.makedirs(out_dir) to_download.append((uri, fname)) all_filenames.add(fname) del all_filenames Downloader().run(to_download) output((e[1] for e in to_download))
def ls_command(out, *arg): path = memory.get('path', []) arg = ' '.join(arg) if len(path) == 0: out(['artists', 'albums']) return if len(path) == 2 or arg: return ('/db/search?fmt=txt&host=%(db_host)s&pattern='+quote('artist:%(args)s' if arg else ":".join(path)).replace('%', r'%%')) elif len(path) == 1: if path[0].startswith('ar'): return '/db/artists' else: return '/db/albums'
def modify_show(output, answers=10): answers = get_index_or_slice(answers) if isinstance(answers, slice): memory['show_offset'] = answers.start results = 0 if answers.stop <= 0 else answers.stop - answers.start return '/playlist?res=%s&start=%s'%(results, answers.start) else: pos = memory.get('pls_position') if pos is None: return '' try: position = int(pos) except TypeError: position = -1 if position >= 0: memory['show_offset'] = position return '/playlist?res=%s&start=%s'%(answers, position) else: memory['show_offset'] = 0 return '/playlist?res=%s'%(answers)
def pwd_command(out): path = memory.get('path', []) out(['/'+('/'.join(path))])