simonschellaert / spotify2am

Import your Spotify library into Apple Music
368 stars 71 forks source link

Multiple playlist support
 #2

Open MatthieuTnsc opened 9 years ago

MatthieuTnsc commented 9 years ago

That would be nice to support multiple playlist file identifying in ‘retrieve-identifier.py’ Personally I managed to do it this way !

def findFiles (path, filter):
    for root, dirs, files in os.walk(path):
        for file in fnmatch.filter(files, filter):
            yield os.path.join(root, file)

itunes_identifiers = []

itunes_dir = "itunes"

if not os.path.exists(itunes_dir):
    os.makedirs(itunes_dir)

execution_dir = os.path.dirname(os.path.realpath(__file__))

for csv_files in findFiles(execution_dir, '*.csv'):

    csv_file_name = os.path.basename(csv_files)
    playlist_name, playlist_extension = os.path.splitext(csv_file_name)

    print("Found a playlist file named {}".format(playlist_name))

    with open(csv_file_name, encoding='utf-8') as playlist_file:
        playlist_reader = csv.reader(playlist_file)
        next(playlist_reader)

        for row in playlist_reader:
            title, artist = row[1], row[2]
            itunes_identifier = retrieve_itunes_identifier(title, artist)

            if itunes_identifier:
                itunes_identifiers.append(itunes_identifier)
                print("{} - {} => {}".format(title, artist, itunes_identifier))
            else:
                print("{} - {} => Not Found".format(title, artist))

    with open(execution_dir+'/itunes/'+csv_file_name, 'w', encoding='utf-8') as output_file:
        for itunes_identifier in itunes_identifiers:
            output_file.write(str(itunes_identifier) + "\n")