Skip to content

Instantly share code, notes, and snippets.

@macroramesh6
Last active November 29, 2024 09:43
Show Gist options
  • Save macroramesh6/2ab692a1cf82bd968e5ff69a15066c4a to your computer and use it in GitHub Desktop.
Save macroramesh6/2ab692a1cf82bd968e5ff69a15066c4a to your computer and use it in GitHub Desktop.
Generate Spotify Playlist for Poweramp
import csv
import os
import re
def format_track_name(track_name):
# Match the content inside parentheses
def replace_in_parentheses(match):
content = match.group(1)
# Replace the first space with double space
content = content.replace(' ', ' ', 1)
# Add a space before the closing parenthesis
return f"({content} )"
# Apply the regex and replacement
track_name = re.sub(r'\((.*?)\)', replace_in_parentheses, track_name)
return track_name
def generate_m3u8_playlist(csv_file_name):
"""
Generates an m3u8 playlist based on the list of audio files in the CSV file.
Args:
csv_file_name (str): The name of the CSV file containing the list of audio files.
"""
playlist_file_name = "playlist.m3u8"
base_path = "primary/Download/"
with open(csv_file_name, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip the header row
with open(playlist_file_name, 'w') as playlist_file:
playlist_file.write("#EXTM3U\n") # Add the M3U header
for row in reader:
track_name = row[1]
artist_name = row[3]
audio_file_name = f"{format_track_name(track_name)} - {artist_name.replace(',', ', ')}.m4a"
audio_file_path = os.path.join(base_path, audio_file_name)
playlist_file.write(f"{audio_file_path}\n")
if __name__ == "__main__":
csv_file_name = input("Enter the CSV file name: ")
generate_m3u8_playlist(csv_file_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment