def download(link, code): song_dir = file_management.get_songs_path() dest = os.path.join(song_dir, f'{code}.%(ext)s') cmd = ['youtube-dl'] cmd.append('-f') cmd.append('bestaudio[ext=m4a]') cmd.append('--write-info-json') cmd.append('-o') cmd.append(dest) cmd.append(link) print('running', cmd) sp = subprocess.run(cmd, capture_output=True) if sp.returncode != 0: raise # return title info_file = f'{code}.info.json' try: with open(os.path.join(song_dir, info_file)) as file: title = json.load(file)['title'] return title except FileNotFoundError: sys.exit(f'Cannot find json file info_file')
def song_exists(link) -> bool: songs_dir = file_management.get_songs_path() for filepath in glob.glob(os.path.join(songs_dir, '*.info.json')): # print(filepath) abspath = os.path.join(songs_dir, filepath) with open(abspath) as file: obj = json.load(file) if obj['webpage_url'] == link: return True return False
def get_song_info(song_id): songs_dir = file_management.get_songs_path() filename = f'{song_id}.info.json' dest = os.path.join(songs_dir, filename) if not os.path.exists(dest): raise KeyError(f'song does not exist: {dest}') return json.load(open(dest))
def remove_song(song_id): playlists_dir = file_management.get_playlists_path() for filename in os.listdir(playlists_dir): print(f'examining {filename}') suf = Path(filename).suffix print(suf) if suf == 'playlist': playlist_name = os.path.splitext(filename)[0] playlists.remove_song(song_id, playlist_name) ids_dest = file_management.get_ids_path() util.purge_id(song_id, ids_dest) # delete relevant files songs_dir = file_management.get_songs_path() for filepath in glob.glob(os.path.join(songs_dir, f'{song_id}.*')): abspath = os.path.join(songs_dir, filepath) print(f'deleting {abspath}') os.remove(abspath)
import argparse import os import util import subprocess import file_management plp = file_management.get_playlists_path() sgp = file_management.get_songs_path() parser = argparse.ArgumentParser() parser.add_argument('-p', '--playlist', type=str, metavar='<TITLE>') args = parser.parse_args() playlist = args.playlist aspath = os.path.join(plp, f'{playlist}.playlist') with open(aspath) as f: for line in util.lines(f): song_dest = os.path.join(sgp, f'{line}.m4a') # subprocess.run(['ffplay', '-nodisp', '-nostats', '-hide_banner', song_dest]) subprocess.run(['afplay', song_dest])