def search(searchterm): db = freezerdb.FreezerDB() ans = [] for row in db.read_all(): if "".join(row).lower().count(searchterm.lower()) > 0: ans.append(row) return ans
def save_scan_results(scanresult): if os.path.exists(freezerdb.FREEZER_DB): print("remove existing db at {}? [y/n] default is no".format( freezerdb.FREEZER_DB)) i = input() if i == 'y': os.remove(freezerdb.FREEZER_DB) else: print("aborting scan") return init_workspace() db = freezerdb.FreezerDB() results = sorted(scanresult, key=lambda s: s[0].lower()) for result in results: db.insert_album(result)
def main(): parser = get_args() args = parser.parse_args() thefreezer = FreezerInstance(args.remote_host) db = freezerdb.FreezerDB() if args.command == "init": init_workspace() elif args.command == "scan": result = scan.perform_scan(get_freezer_indexed_paths(), 8) save_scan_results(result) elif args.command == "show": if args.what_to_show == "all": for i in db.read_all(): print("{:33}{:43}{}".format(i[0], i[1], i[2])) elif args.what_to_show == "albums": for i in db.read_albums(): print("{:43}{}".format(i[1], i[0])) elif args.what_to_show == "artists": for i in db.read_artists(): print(i[0]) else: parser.print_usage() elif args.command == "archive": # In a remote freezer situation, "album_name" is actually the remote file path outpath, zipbytes = thefreezer.zip_album(args.album_to_zip) outf = open(outpath, 'wb') outf.write(zipbytes.data) print(outpath) elif args.command == "play": play_album(thefreezer, args) elif args.command == "play_random_album": play_random_album(db, thefreezer, args) elif args.command == "search_path": add_indexed_path(args.filenames) elif args.command == "play": add_indexed_path(args.filenames) elif args.command == "search": for i in thefreezer.search(args.query): print("{:33}{:43}{}".format(i[0], i[1], i[2])) else: parser.print_help()
def zip_album(query): """ Retrieve an album based on query then write matching songs into a zip file. Stores the zip file in FREEZER_TMP_DIR and returns the filename. # TODO: fix the network zip file functionality (this function used to return the bytes) """ assert type(query) is str db = freezerdb.FreezerDB() album_path = None album_name = None artist_name = None for album_tuple in db.read_all(): if "".join(album_tuple).lower().count(query.lower()): album_path = album_tuple[-1] album_name = album_tuple[1] artist_name = album_tuple[0] break output_dir = FREEZER_TMP_DIR arist_name = replace_bad_chars(artist_name) album_name = replace_bad_chars(album_name) artist_album_str = "{} - {}".format(artist_name, album_name) outfilename = os.path.join(output_dir, artist_album_str + '.zip') if not os.path.exists(outfilename): os.makedirs(output_dir, exist_ok=True) zf = ZipFile(outfilename, 'w') for root, dirs, files in os.walk(album_path.strip()): for filename in files: song_path = os.path.join(root, filename) zip_output_path = os.path.join(artist_album_str, os.path.basename(song_path)) zip_output_path.replace(":", "_") zf.write(song_path, arcname=zip_output_path) zf.close() assert os.path.exists(outfilename) print("outfilename is {}".format(outfilename)) with open(outfilename, 'rb') as zipbytes: return (outfilename, zipbytes.read())
def init_workspace(): os.makedirs(FREEZER_DIR, exist_ok=True) db = freezerdb.FreezerDB() db.init_db()
def setUp(self): self.fake_conn = MagicMock() self.db = freezerdb.FreezerDB(self.fake_conn)