Skip to content

Offtopic: Adding new songs

Eric Mink edited this page Feb 12, 2022 · 2 revisions

When I download a new song on my PC, I'd like to be able to later listen to my "newly added songs" on the phone and then assign it to the playlists it should be in.

There is a list called "Recently Added" in PowerAmp. It's probably perfectly fine.

But I was not sure how exactly it works and it is likely not the same thing as "All songs you have not yet assigned to a playlist" so I wrote a python script. It takes all songs from UNASSIGNED_SONGS_DIR, moves them to PERMANENT_SONGS_DIR which has a name depending on the current month, and adds them to an m3u playlist file using relative paths.

Now I can listen to that playlist on the phone on shuffle and assign the songs to playlists. Once I want, I remove it from that unassigned playlist.

Update Unordered Songs

This script could run at cron. But for now I will just run it manually after downloading a few songs to the UNASSIGNED_SONGS_DIR.

#!/usr/bin/env python3
# This file assumes you are running it directly. Because otherwise __file__ might not be set
# and in that case we can't get the file's directory
import pathlib, os, datetime, shutil

THIS_FILEs_DIR = pathlib.Path(__file__).parent.absolute()
UNASSIGNED_SONGS_DIR = os.path.join(THIS_FILEs_DIR, "songs")
PERMANENT_SONGS_DIR_PARENT = os.path.join(THIS_FILEs_DIR, "../")
VERBOSE = True
UNASSIGNED_PLST_NAME = "unassigned.m3u8"
UNASSIGNED_PLST_PATH = os.path.join(THIS_FILEs_DIR, "../", "playlists_relative/", UNASSIGNED_PLST_NAME)

def add_to_unassigned_playlist(songfile_path: pathlib.Path):
    # open file for appending, create if not exists
    with open(UNASSIGNED_PLST_PATH, "a+", encoding="utf-8") as plst:
        relative_song_path = os.path.relpath(songfile_path, UNASSIGNED_PLST_PATH)
        # append
        plst.write(relative_song_path+"\n")

def main():
    if VERBOSE:
        print("Path of newly added songs: {}".format(UNASSIGNED_SONGS_DIR)) 
        print("Path of playlist to add them to: {}".format(UNASSIGNED_PLST_PATH)) 

    # create month directory
    now = datetime.datetime.now()
    monthdir_path = os.path.join(PERMANENT_SONGS_DIR_PARENT, "{year}".format(year=now.year), "{month}".format(month=now.month))
    os.makedirs(monthdir_path, exist_ok=True)
    if VERBOSE:
        print("Moving them to: {}".format(monthdir_path))
    
    for directory, subdirs, songfiles in os.walk(UNASSIGNED_SONGS_DIR):
        for songfile in songfiles:
            # move every new songfile to permanent directory
            target_path = os.path.join(monthdir_path, songfile)
            current_path = os.path.join(directory, songfile)
            if not os.path.exists(target_path):
                shutil.move(os.path.abspath(current_path), 
                    os.path.abspath(target_path))
                add_to_unassigned_playlist(target_path)
            else:
                if VERBOSE:
                    print("Target File {} already exists. Will retry next time.".format(target_path))
                # TODO: what if it's already there? Just wait?



if __name__ == "__main__":
    main()
Clone this wiki locally