Example #1
0
def main():

    # Initialize the argument parser...
    argument_parser = argparse.ArgumentParser(
        description=_('Query the status of a Helios server.'))

    # Add common arguments to argument parser...
    add_common_arguments(argument_parser)

    # Parse the command line...
    arguments = argument_parser.parse_args()

    # Initialize terminal colour...
    colorama.init()

    # Try to query server status...
    success = False
    try:

        # If no host provided, use Zeroconf auto detection...
        if not arguments.host:
            arguments.host = zeroconf_find_server()[0]

        # Create a client...
        client = helios.client(token=arguments.token,
                               host=arguments.host,
                               port=arguments.port,
                               verbose=arguments.verbose)

        # Perform query...
        server_status = client.get_server_status()
        success = True

    # Helios exception...
    except helios.exceptions.ExceptionBase as someException:
        print(someException.what())

    # Some other kind of exception...
    except Exception as someException:
        print(_(f"An unknown exception occurred: {print(someException)}"))

    # Show server information if received and verbosity enabled...
    if success and arguments.verbose:
        pprint(attr.asdict(server_status))

    # Show success status or failure...
    if success:
        print(_(f"Server has {server_status.songs} songs."))
    else:
        print(
            f"{colored(_('There was a problem verifying the server status.'), 'red')}"
        )

    # If unsuccessful, bail...
    if not success:
        sys.exit(1)

    # Done...
    sys.exit(0)
    # Initialize all the other patchable song fields...
    if arguments.song_edit_album is not None: patch_song_dict['album'] = arguments.song_edit_album
    if arguments.song_edit_artist is not None: patch_song_dict['artist'] = arguments.song_edit_artist
    if arguments.song_edit_genre is not None: patch_song_dict['genre'] = arguments.song_edit_genre
    if arguments.song_edit_isrc is not None: patch_song_dict['isrc'] = arguments.song_edit_isrc
    if arguments.song_edit_reference is not None: patch_song_dict['reference'] = arguments.song_edit_reference
    if arguments.song_edit_title is not None: patch_song_dict['title'] = arguments.song_edit_title
    if arguments.song_edit_year is not None: patch_song_dict['year'] = arguments.song_edit_year

    # Try to modify the song...
    success = False
    try:

        # If no host provided, use Zeroconf auto detection...
        if not arguments.host:
            arguments.host = zeroconf_find_server()[0]

        # Create a client...
        client = helios.client(
            token=arguments.token,
            host=arguments.host,
            port=arguments.port,
            verbose=arguments.verbose)

        # Submit modification request...
        stored_song = client.modify_song(
            patch_song_dict=patch_song_dict,
            store=arguments.store,
            song_id=arguments.song_id,
            song_reference=arguments.song_reference)
def main():

    # Initialize the argument parser...
    argument_parser = argparse.ArgumentParser(
        description=_('Delete a remote song or songs on a Helios server.'))

    # Add common arguments to argument parser...
    add_common_arguments(argument_parser)

    # Add arguments specific to this utility to argument parser...
    add_arguments(argument_parser)

    # Parse the command line...
    arguments = argument_parser.parse_args()

    # Initialize terminal colour...
    colorama.init()

    # Status on whether there were any errors...
    success = False

    # Try to delete a single song or all songs...
    try:

        # A progress bar we may or may not construct later...
        progress_bar = None

        # If no host provided, use Zeroconf auto detection...
        if not arguments.host:
            arguments.host = zeroconf_find_server()[0]

        # Create a client...
        client = helios.client(host=arguments.host,
                               port=arguments.port,
                               token=arguments.token,
                               verbose=arguments.verbose)

        # Counter of the number of songs deleted...
        total_deleted = 0

        # Only requested a single song to delete...
        if arguments.song_id or arguments.song_reference:

            # Submit request to server...
            song_deleted = delete_song(
                client,
                song_id=arguments.song_id,
                song_reference=arguments.song_reference,
                delete_file_only=arguments.delete_file_only)

            # Update deletion counter...
            if song_deleted:
                total_deleted += 1

            # Note success...
            success = True

        # User requested to delete all song files only, keeping metadata...
        elif arguments.delete_all and arguments.delete_file_only:

            # Prompt user to make sure they are certain they know what they are
            #  doing...
            verification = input(
                _('Are you sure you know what you are doing? Type \'YES\': '))
            if verification != _('YES'):
                sys.exit(1)

            # How many songs are currently in the database?
            server_status = client.get_server_status()

            # Keep deleting songs while there are some...
            songs_remaining = True
            current_page = 1
            progress_bar = tqdm(desc=_('Deleting files'),
                                total=server_status.songs,
                                unit=_(' songs'))
            while songs_remaining:

                # Get a batch of songs...
                all_songs_list = client.get_all_songs(page=current_page,
                                                      page_size=100)

                # No more songs left...
                if len(all_songs_list) == 0:
                    songs_remaining = False

                # Delete each one's song file...
                for song in all_songs_list:

                    # Submit request to server...
                    song_deleted = delete_song(client,
                                               song_id=song.id,
                                               song_reference=song.reference,
                                               delete_file_only=True)

                    # Update deletion counter...
                    if song_deleted:
                        total_deleted += 1

                    # Update progress bar...
                    progress_bar.update(1)

                # Advance to next page...
                current_page += 1

            # Done...
            progress_bar.close()
            success = True

        # User requested to delete all songs and their files...
        elif arguments.delete_all and not arguments.delete_file_only:

            # Prompt user to make sure they are certain they know what they are
            #  doing...
            verification = input(
                _('Are you sure you know what you are doing? Type \'YES\': '))
            if verification != _('YES'):
                sys.exit(1)

            # How many songs are currently in the database?
            server_status = client.get_server_status()

            # Keep deleting songs while there are some...
            songs_remaining = True
            progress_bar = tqdm(desc=_('Deleting metadata and files'),
                                total=server_status.songs,
                                unit=_(' songs'))
            while songs_remaining:

                # Get a batch of songs. We can always start chomping at the
                #  first page because everything after it is shifted up the
                #  first page after being deleted...
                all_songs_list = client.get_all_songs(page=1, page_size=100)

                # No more songs left...
                if len(all_songs_list) == 0:
                    songs_remaining = False

                # Delete each one...
                for song in all_songs_list:

                    # Submit request to server...
                    song_deleted = delete_song(client,
                                               song_id=song.id,
                                               song_reference=song.reference,
                                               delete_file_only=False)

                    # Update deletion counter...
                    if song_deleted:
                        total_deleted += 1

                    # Update progress bar...
                    progress_bar.update(1)

            # Done...
            progress_bar.close()
            success = True

    # User trying to abort...
    except KeyboardInterrupt:
        sys.exit(1)

    # Helios exception...
    except helios.exceptions.ExceptionBase as someException:
        print(someException.what())

    # Some other kind of exception...
    except Exception as someException:
        print(_(f"An unknown exception occurred: {print(someException)}"))

    # Cleanup any resources...
    finally:

        # Dump any new output beginning on a new line...
        print('')

        # Cleanup progress bar, if one was constructed...
        if progress_bar:
            progress_bar.close()

    # Show deletion statistics...
    print(F'Deleted {total_deleted} successfully.')

    # If unsuccessful, bail...
    if not success:
        sys.exit(1)

    # Done...
    sys.exit(0)
def main():

    # Initialize the argument parser...
    argument_parser = argparse.ArgumentParser(
        description=_('Batch import songs into Helios.'))

    # Add common arguments to argument parser...
    add_common_arguments(argument_parser)

    # Add arguments specific to this utility to argument parser...
    add_arguments(argument_parser)

    # Parse the command line...
    arguments = argument_parser.parse_args()
    arguments.offset = max(arguments.offset, 1)

    # Setup logging...
    format = "%(asctime)s: %(message)s"
    if arguments.verbose:
        logging.basicConfig(format=format, level=logging.DEBUG, datefmt="%H:%M:%S")
    else:
        logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

    # Initialize terminal colour...
    colorama.init()

    # Success flag to determine exit code...
    success = False

    # Total number of songs in the input catalogue file...
    songs_total = 0

    # Prepare column fieldnames from CSV input catalogue...
    fieldnames = [
        'reference',
        'album',
        'artist',
        'title',
        'genre',
        'isrc',
        'year',
        'path'
    ]

    # Batch importer will be constructed within try block...
    batch_importer = None

    # Try to process the catalogue file...
    try:

        # If no host provided, use Zeroconf auto detection...
        if not arguments.host:
            arguments.host = zeroconf_find_server()[0]

        # Create a client...
        client = helios.client(
            token=arguments.token,
            host=arguments.host,
            port=arguments.port,
            verbose=arguments.verbose)

        # Count the number of songs in the input catalogue...
        with open(arguments.catalogue_file, 'r') as catalogue_file:
            for line in catalogue_file:
                if line.strip():
                    songs_total += 1

        # Verify we can reach the server...
        server_status = client.get_server_status()

        # If user requested to autodetect the number of threads, set to the
        #  number of logical cores on the server...
        if arguments.threads == 0:
            arguments.threads = server_status.cpu.cores

        # Initialize batch song importer...
        batch_importer = BatchSongImporter(arguments, songs_total)

        # Open catalogue file again, but this time for parsing. Set newline
        #  empty per <https://docs.python.org/3/library/csv.html#id3>
        catalogue_file = open(arguments.catalogue_file, 'r', newline='')

        # Initialize the CSV reader...
        reader = csv.DictReader(
            f=catalogue_file,
            fieldnames=fieldnames,
            delimiter=arguments.delimiter,
            doublequote=False,
            escapechar='\\',
            quoting=csv.QUOTE_MINIMAL,
            skipinitialspace=True,
            strict=True)

        # Submit the songs...
        batch_importer.start(reader)

        # Determine how we will exit...
        success = True #(errors_remaining == arguments.maximum_errors)

    # User trying to abort...
    except KeyboardInterrupt:
        print(_('Aborting, please wait a moment...'))
        success = False

    # Helios exception...
    except helios.exceptions.ExceptionBase as someException:
        print(someException.what())

    # Some other kind of exception...
    except Exception as someException:
        print(_(f"An unknown exception occurred: {str(someException)}"))

    # Cleanup...
    finally:

        # Stop batch import, in case it hasn't already...
        if batch_importer:
            batch_importer.stop()
            del batch_importer

        # Close input catalogue file...
        catalogue_file.close()

    # Exit with status code based on whether we were successful or not...
    if success:
        sys.exit(0)
    else:
        sys.exit(1)
def main():

    # Initialize the argument parser...
    argument_parser = argparse.ArgumentParser(
        description=_('Add a song to a Helios server.'))

    # Add common arguments to argument parser...
    add_common_arguments(argument_parser)

    # Add arguments specific to this utility to argument parser...
    add_arguments(argument_parser)

    # Parse the command line...
    arguments = argument_parser.parse_args()

    # Initialize terminal colour...
    colorama.init()

    # Success flag to determine exit code...
    success = False

    # Try to submit the song...
    try:

        # If no host provided, use Zeroconf auto detection...
        if not arguments.host:
            arguments.host = zeroconf_find_server()[0]

        # Create a client...
        client = helios.client(token=arguments.token,
                               host=arguments.host,
                               port=arguments.port,
                               verbose=arguments.verbose)

        # Prepare new song data...
        new_song_dict = {
            'file':
            base64.b64encode(open(arguments.song_file,
                                  'rb').read()).decode('ascii'),
            'reference':
            arguments.song_reference
        }

        # Progress bar to be allocated by tqdm as soon as we know the total size...
        progress_bar = None

        # Progress bar callback...
        def progress_callback(bytes_read, new_bytes, bytes_total):

            # Reference the outer function...
            nonlocal progress_bar

            # If the progress bar hasn't been allocated already, do so now...
            if not progress_bar:
                progress_bar = tqdm(desc=_('Uploading'),
                                    leave=False,
                                    smoothing=1.0,
                                    total=bytes_total,
                                    unit_scale=True,
                                    unit='B')

            # Update the progress bar with the bytes just read...
            progress_bar.update(new_bytes)

            #            time.sleep(0.001)

            # If we're done uploading, update description to analysis stage...
            if bytes_read == bytes_total:
                #progress_bar.n = 0
                progress_bar.set_description(_('Analyzing'))

            # Redraw the progress bar on the terminal...
            progress_bar.refresh()

        # Submit the song...
        stored_song = client.add_song(new_song_dict=new_song_dict,
                                      store=arguments.store,
                                      progress_callback=progress_callback)

        # Note the success...
        success = True

    # User trying to abort...
    except KeyboardInterrupt:
        sys.exit(1)

    # Helios exception...
    except helios.exceptions.ExceptionBase as someException:
        print(someException.what())

    # Some other kind of exception...
    except Exception as someException:
        print(_(f"An unknown exception occurred: {print(someException)}"))

    # Cleanup...
    finally:
        if progress_bar:
            progress_bar.close()

    # Show stored song model produced by server if successful and verbosity enabled...
    if success and arguments.verbose:
        pprint(attr.asdict(stored_song))

    # Show success status or failure...
    if success:
        print(_(f"[{colored(_(' OK '), 'green')}]: {arguments.song_file}"))
    else:
        print(_(f"[{colored(_('FAIL'), 'red')}]: {arguments.song_file}"))

    # If unsuccessful, bail...
    if not success:
        sys.exit(1)

    # Done...
    sys.exit(0)