import argparse import config import logging import media import report import sys import os import fstools try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) def progress_callback_rip(p: float): bar_length = 40 progress = int(bar_length * p) out = "\rRipping.... - [ " out += "*" * progress out += "-" * (bar_length - progress) out += " ] %5.1f%%" % (p * 100) sys.stdout.write(out) def progress_callback_enc(p: float): bar_length = 40 progress = int(bar_length * p) out = "\rEncoding... - [ " out += "*" * progress out += "-" * (bar_length - progress) out += " ] %5.1f%%" % (p * 100) sys.stdout.write(out) def cddb_user_callback(what: int, info: dict): if what == media.CALLBACK_CDDB_CHOICE: keys = tuple(info.keys()) print("Multiple cddb entries detected:") print("-------------------------------") for i in range(0, len(keys)): print("%2d. %s" % (i + 1, info[keys[i]])) n = None while not n: fb = input("Choose: ") try: fb = int(fb) except ValueError: n = None else: if fb > 0 and fb <= len(keys): n = fb if n is None: print("Use a choice from above!") print() return keys[n - 1] elif what == media.CALLBACK_MAN_INPUT: print("No cddb entries detected:") print("-------------------------") for key in info: info[key] = input(key[0].upper() + key[1:] + ": ") return info def main(**kwargs): disc_data = media.get_media_data(media.get_disc_device(), cddb_user_callback) if disc_data is None: logger.error("Could not reading disc_data") sys.exit(1) print("Found Disc:") print("===========") print() print(disc_data[media.common.KEY_ARTIST], "-", disc_data[media.common.KEY_ALBUM]) print("-" * (len(disc_data[media.common.KEY_ARTIST]) + len(disc_data[media.common.KEY_ALBUM]) + 3)) for track_info in disc_data["tracks"]: i = track_info[media.common.KEY_TRACK] print("%2d. %s" % (i, track_info[media.common.KEY_TITLE])) wavfile = media.track_to_targetpath(kwargs.get("basepath"), track_info, 'wav') try: fstools.mkdir(os.path.dirname(wavfile)) except PermissionError: logger.exception("Unable to create ripping target path: %s", os.path.dirname(wavfile)) sys.exit(1) rv = media.disc_track_rip(i, wavfile, progress_callback_rip) if rv == 0: print() rv = media.wav_to_mp3(wavfile, kwargs.get("basepath"), track_info, progress_callback_enc) print() os.remove(wavfile) if rv != 0: logger.error("Error while ripping or encoding...") if __name__ == "__main__": default_baspath = os.path.join(os.getenv("HOME"), "rip") parser = argparse.ArgumentParser(description='Description') parser.add_argument('-v', '--verbose', help='Description for foo argument', action='store_true') parser.add_argument('-b', '--basepath', help=f'The rip and encode basepath (default is {default_baspath})', default=default_baspath) args = vars(parser.parse_args()) report.stdoutLoggingConfigure(log_name_lvl=((config.APP_NAME, logging.DEBUG if args.get('verbose') else logging.ERROR),)) main(**args)