Last active
March 19, 2020 02:11
-
-
Save blindstitch/368940ed98993b78e07b75a66b2b6cb5 to your computer and use it in GitHub Desktop.
Adds music to cmus with a very simple keyboard interface, some smart defaults, index cache generation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from argparse import ArgumentParser | |
import os | |
# Bad code deal with it | |
# run with -fc to generate your indexes | |
# Modify these paths to match your root folder for music | |
os.chdir('/kt/media/music/downloads') | |
filelist_path = '/kt/media/music/downloads/.index_files' | |
dirlist_path = '/kt/media/music/downloads/.index_dirs' | |
ap.add_argument('-f',action='store_true') | |
ap.add_argument('-d',action='store_true') | |
ap.add_argument('-fc',action='store_true') | |
ap.add_argument('searchterm',nargs='?') | |
args = ap.parse_args() | |
# print(args) | |
opts = {} | |
opts['d'] = getattr(args,'d') | |
opts['f'] = getattr(args,'f') | |
opts['fc'] = getattr(args,'fc') | |
# test_query = 'converge' | |
if opts['d'] and opts['f']: | |
exit('Remove one of the searchtype terms (-f, -d)') | |
if opts['d']: | |
searchtype = 'dir' | |
elif opts['f']: | |
searchtype = 'file' | |
else: | |
searchtype = 'dir' | |
searchterm = args.searchterm | |
def regen_indexes(): | |
# Expand to use cached index | |
list_files_cmd = ['find', '.', '-type', 'f'] | |
list_dirs_cmd = ['find', '.', '-type', 'd'] | |
stdout = subprocess.Popen(list_files_cmd, stdout=subprocess.PIPE) | |
results_f = stdout.communicate()[0].decode('UTF-8') | |
stdout = subprocess.Popen(list_dirs_cmd, stdout=subprocess.PIPE) | |
results_d = stdout.communicate()[0].decode('UTF-8') | |
index_f = open(filelist_path,'w') | |
index_f.write(results_f) | |
index_f.close() | |
index_d = open(dirlist_path,'w') | |
index_d.write(results_d) | |
index_d.close() | |
# Regen | |
if opts['fc']: | |
print('Regenerating indexes. This could take a moment') | |
regen_indexes() | |
print('Done.') | |
try: | |
opts['searchterm'] = getattr(args,'searchterm') | |
opts['searchterm'].lower() | |
except: | |
exit('No search term specified, exiting.') | |
def get_cached_indexes(): | |
return ( open(filelist_path,'r').read().split('\n'), | |
open(dirlist_path,'r').read().split('\n') ) | |
file_index, dir_index = get_cached_indexes() | |
def search(str,searchtype='dir'): | |
if searchtype == 'dir': | |
results = [d for d in dir_index if str.lower() in d.lower()] | |
elif searchtype == 'file': | |
results = [f for f in file_index if str.lower() in f.lower()] | |
else: | |
raise Exception('bad searchtype:',searchtype) | |
return results | |
search_results = search(searchterm, searchtype=searchtype) | |
for i in range(len(search_results)): | |
sr = search_results[i] | |
print('{}) {}'.format(i+1,sr)) | |
def choose_items(): | |
options = range(len(search_results)) | |
# Getting NameErrors on entry, possibly because input is not raw | |
choices = input('Choose options to add. (Item numbers spaces-separated, or "all" or "a") \n> ') | |
if choices == 'all' or choices == 'a': | |
return options | |
try: | |
choices_list = [int(s)-1 for s in choices.strip().split(' ')] | |
return choices_list | |
except: | |
raise Exception('Bad entry: {}'.format(choices)) | |
choices = choose_items() | |
filtered = [search_results[i] for i in choices if i in range(len(search_results))] # TODO Get this inside a function | |
# print(filtered) | |
def cmus_add(object_list): | |
# add to cmus ... | |
if len(object_list) == 0: | |
print('Adding nothing because nothing was selected.') | |
exit() | |
cmus_command = ['cmus-remote'] + object_list | |
subprocess.call(cmus_command) | |
cmus_add(filtered) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment