def main():
    cli = dict((key.lstrip("-<").rstrip(">"), value)
               for key, value in docopt(__doc__).items())

    if cli['no-recursion']:
        cli['max-depth'] = 0
    else:
        cli['max-depth'] = int(
            cli['max-depth']) if cli['max-depth'] else float('inf')

    if cli['quiet']:
        logger.setLevel(QUIET)
    else:
        logger.setLevel(logging.INFO)

    if not cli['input']:
        cli['input'] = [os.getcwd()]

    mmw = MusicManagerWrapper(enable_logging=cli['log'])
    mmw.login(oauth_filename=cli['cred'], uploader_id=cli['uploader-id'])

    if not mmw.is_authenticated:
        sys.exit()

    include_filters = [
        tuple(filt.split(':', 1)) for filt in cli['include-filter']
    ]
    exclude_filters = [
        tuple(filt.split(':', 1)) for filt in cli['exclude-filter']
    ]

    songs_to_upload, songs_to_filter, songs_to_exclude = mmw.get_local_songs(
        cli['input'],
        include_filters=include_filters,
        exclude_filters=exclude_filters,
        all_includes=cli['all-includes'],
        all_excludes=cli['all-excludes'],
        exclude_patterns=cli['exclude'],
        max_depth=cli['max-depth'])

    songs_to_upload.sort()
    songs_to_exclude.sort()

    if cli['dry-run']:
        logger.info("\nFound {0} song(s) to upload".format(
            len(songs_to_upload)))

        if songs_to_upload:
            logger.info("\nSongs to upload:\n")

            for song in songs_to_upload:
                logger.log(QUIET, song)
        else:
            logger.info("\nNo songs to upload")

        if songs_to_filter:
            logger.info("\nSongs to filter:\n")

            for song in songs_to_filter:
                logger.log(QUIET, song)
        else:
            logger.info("\nNo songs to filter")

        if songs_to_exclude:
            logger.info("\nSongs to exclude:\n")

            for song in songs_to_exclude:
                logger.log(QUIET, song)
        else:
            logger.info("\nNo songs to exclude")
    else:
        if songs_to_upload:
            logger.info("\nUploading {0} song(s) to Google Music\n".format(
                len(songs_to_upload)))

            mmw.upload(songs_to_upload,
                       enable_matching=cli['match'],
                       delete_on_success=cli['delete-on-success'])
        else:
            logger.info("\nNo songs to upload")

    mmw.logout()
    logger.info("\nAll done!")
Esempio n. 2
0
def main():
    cli = dict((key.lstrip("-<").rstrip(">"), value)
               for key, value in docopt(__doc__).items())

    if cli['quiet']:
        logger.setLevel(QUIET)
    else:
        logger.setLevel(logging.INFO)

    if not cli['output']:
        cli['output'] = os.getcwd()

    mmw = MusicManagerWrapper(enable_logging=cli['log'])
    mmw.login(oauth_filename=cli['cred'], uploader_id=cli['uploader-id'])

    if not mmw.is_authenticated:
        sys.exit()

    include_filters = [
        tuple(filt.split(':', 1)) for filt in cli['include-filter']
    ]
    exclude_filters = [
        tuple(filt.split(':', 1)) for filt in cli['exclude-filter']
    ]

    songs_to_download, songs_to_filter = mmw.get_google_songs(
        include_filters=include_filters,
        exclude_filters=exclude_filters,
        all_includes=cli['all-includes'],
        all_excludes=cli['all-excludes'])

    songs_to_download.sort(key=lambda song: (song.get(
        'artist'), song.get('album'), song.get('track_number')))

    if cli['dry-run']:
        logger.info("\nFound {0} song(s) to download".format(
            len(songs_to_download)))

        if songs_to_download:
            logger.info("\nSongs to download:\n")

            for song in songs_to_download:
                title = song.get('title', "<title>")
                artist = song.get('artist', "<artist>")
                album = song.get('album', "<album>")
                song_id = song['id']

                logger.log(
                    QUIET,
                    "{0} -- {1} -- {2} ({3})".format(title, artist, album,
                                                     song_id))
        else:
            logger.info("\nNo songs to download")

        if songs_to_filter:
            logger.info("\nSongs to filter:\n")

            for song in songs_to_filter:
                logger.log(QUIET, song)
        else:
            logger.info("\nNo songs to filter")
    else:
        if songs_to_download:
            logger.info("\nDownloading {0} song(s) from Google Music\n".format(
                len(songs_to_download)))
            mmw.download(songs_to_download, template=cli['output'])
        else:
            logger.info("\nNo songs to download")

    mmw.logout()
    logger.info("\nAll done!")
Esempio n. 3
0
def main():
    cli = dict((key.lstrip("-<").rstrip(">"), value)
               for key, value in docopt(__doc__).items())

    if cli['no-recursion']:
        cli['max-depth'] = 0
    else:
        cli['max-depth'] = int(
            cli['max-depth']) if cli['max-depth'] else float('inf')

    if cli['quiet']:
        logger.setLevel(QUIET)
    else:
        logger.setLevel(logging.INFO)

    if not cli['input']:
        cli['input'] = [os.getcwd()]

    if not cli['output']:
        cli['output'] = os.getcwd()

    include_filters = [
        tuple(filt.split(':', 1)) for filt in cli['include-filter']
    ]
    exclude_filters = [
        tuple(filt.split(':', 1)) for filt in cli['exclude-filter']
    ]

    mmw = MusicManagerWrapper(enable_logging=cli['log'])
    mmw.login(oauth_filename=cli['cred'], uploader_id=cli['uploader-id'])

    if not mmw.is_authenticated:
        sys.exit()

    if cli['down']:
        matched_google_songs, _ = mmw.get_google_songs(
            include_filters=include_filters,
            exclude_filters=exclude_filters,
            all_includes=cli['all-includes'],
            all_excludes=cli['all-excludes'])

        logger.info("")

        cli['input'] = [
            template_to_base_path(cli['output'], matched_google_songs)
        ]

        matched_local_songs, __, __ = mmw.get_local_songs(
            cli['input'], exclude_patterns=cli['exclude'])

        logger.info("\nFinding missing songs...")
        songs_to_download = compare_song_collections(matched_google_songs,
                                                     matched_local_songs)

        songs_to_download.sort(key=lambda song: (song.get(
            'artist'), song.get('album'), song.get('track_number')))

        if cli['dry-run']:
            logger.info("\nFound {0} song(s) to download".format(
                len(songs_to_download)))

            if songs_to_download:
                logger.info("\nSongs to download:\n")

                for song in songs_to_download:
                    title = song.get('title', "<title>")
                    artist = song.get('artist', "<artist>")
                    album = song.get('album', "<album>")
                    song_id = song['id']

                    logger.log(
                        QUIET, "{0} -- {1} -- {2} ({3})".format(
                            title, artist, album, song_id))
            else:
                logger.info("\nNo songs to download")
        else:
            if songs_to_download:
                logger.info(
                    "\nDownloading {0} song(s) from Google Music\n".format(
                        len(songs_to_download)))
                mmw.download(songs_to_download, template=cli['output'])
            else:
                logger.info("\nNo songs to download")
    else:
        matched_google_songs, _ = mmw.get_google_songs()

        logger.info("")

        matched_local_songs, songs_to_filter, songs_to_exclude = mmw.get_local_songs(
            cli['input'],
            include_filters=include_filters,
            exclude_filters=exclude_filters,
            all_includes=cli['all-includes'],
            all_excludes=cli['all-excludes'],
            exclude_patterns=cli['exclude'],
            max_depth=cli['max-depth'])

        logger.info("\nFinding missing songs...")

        songs_to_upload = compare_song_collections(matched_local_songs,
                                                   matched_google_songs)

        # Sort lists for sensible output.
        songs_to_upload.sort()
        songs_to_exclude.sort()

        if cli['dry-run']:
            logger.info("\nFound {0} song(s) to upload".format(
                len(songs_to_upload)))

            if songs_to_upload:
                logger.info("\nSongs to upload:\n")

                for song in songs_to_upload:
                    logger.log(QUIET, song)
            else:
                logger.info("\nNo songs to upload")

            if songs_to_filter:
                logger.info("\nSongs to filter:\n")

                for song in songs_to_filter:
                    logger.log(QUIET, song)
            else:
                logger.info("\nNo songs to filter")

            if songs_to_exclude:
                logger.info("\nSongs to exclude:\n")

                for song in songs_to_exclude:
                    logger.log(QUIET, song)
            else:
                logger.info("\nNo songs to exclude")
        else:
            if songs_to_upload:
                logger.info("\nUploading {0} song(s) to Google Music\n".format(
                    len(songs_to_upload)))

                mmw.upload(songs_to_upload,
                           enable_matching=cli['match'],
                           delete_on_success=cli['delete-on-success'])
            else:
                logger.info("\nNo songs to upload")

                # Delete local files if they already exist on Google Music.
                if cli['delete-on-success']:
                    for song in matched_local_songs:
                        try:
                            os.remove(song)
                        except:
                            logger.warning(
                                "Failed to remove {} after successful upload".
                                format(song))

    mmw.logout()
    logger.info("\nAll done!")