v19 Adding songs to playlist
#1
I have an addon script that adds songs to the playlist using:

            playList = xbmc.PlayList(0)   # 0 is the Music Playlist, 1 is the Video playlist
            playList.add(url=filename, index=pos)

Alas once added the playlist seen on Chorus or Kore is just a list of filenames!

It turns out if I display the playlist explicity in Kodi with:

            xbmc.executebuiltin("Dialog.Close(all, true)")
            xbmc.executebuiltin("ActivateWindow(musicplaylist)")

then on Chorus and Kore I see a proper playlist with cover art, song title and artist (the first line is to dismiss CULyrics if it's up as the second bombs if it is).

I'm curious, is there a way to do this without displaying the playlist in Kodi?

I note that playList.add has a listitem argument that may be useful for this, but I can't see an easy way to use that argument to do what displaying the playlist does.
Reply
#2
OK, this is now solved. It took a while, the way to see the playlist updated with icons and track title and artist (witout having to display the Kodi playlist as described in the original post) is as follows:

First what I had:

SongFiles contains a list of song files as filesystem paths

playList = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
for filename in SongFiles:
    playList.add(url=filename)

And that will show in Chorus and Kore as a list of filesystem paths. Ugly as. The workaround was to force Kodi to display the playlist and this sees Kodi convert the file system paths to rich list items.

It turns out you can build and provide that listitem such that:

playList = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
for filename in SongFiles:
    playList.add(url=filename, listitem=CreateListItem(filename))

works beautifully. The trick is in writing the function CreateListItem().

But here is a way to that using the JSON RPC (it's a tad involved, but gets the job done):


def FilePath_2_KodiPath(Song):
    GetFileDetails = {  "jsonrpc": "2.0",
                        "method": "Files.GetFileDetails",
                        "params": {
                            "file": f"{Song}",
                            "media": "music",
                            "properties": ["albumid"]
                        },
                        "id": 1
                    }

    request = json.dumps(GetFileDetails)
    response = xbmc.executeJSONRPC(request)
    FileDetails = json.loads(response)
    _, file_ext = os.path.splitext(Song)
    song_id = FileDetails["result"]["filedetails"]["id"]
    album_id = FileDetails["result"]["filedetails"]["albumid"]
    kodi_path = f" musicdb://songs/{song_id}.{file_ext}?albumid={album_id}"
    return kodi_path

def KodiPath(Song):
    if Song.startswith("musicdb://"):
        return Song
    else:
        return FilePath_2_KodiPath(Song)

def SongID(Song):
    song = KodiPath(Song)
    m = re.search('musicdb://songs/(\d+)', song)
    song_id = int(m.group(1))
    return song_id

def SongProperties(Song):
    song_id = SongID(Song)
    # This is all the song properties known:
    #  See: https://github.com/xbmc/xbmc/blob/master...n#L526L541
    GetSongDetails = { "jsonrpc": "2.0",
                       "method": "AudioLibrary.GetSongdetails",
                       "params": { "songid" : song_id,
                                   "properties": [
                "title", "artist", "albumartist", "genre", "year",
                "rating", "album", "track", "duration", "comment",
                "lyrics", "musicbrainztrackid", "musicbrainzartistid",
                "musicbrainzalbumid", "musicbrainzalbumartistid",
                "playcount", "fanart", "thumbnail", "file", "albumid",
                "lastplayed", "disc", "genreid", "artistid", "displayartist",
                "albumartistid", "albumreleasetype", "dateadded",
                "votes", "userrating", "mood", "contributors",
                "displaycomposer", "displayconductor", "displayorchestra", "displaylyricist",
                "sortartist", "art", "sourceid", "disctitle", "releasedate", "originaldate",
                "bpm", "samplerate", "bitrate", "channels", "datemodified", "datenew"                                     ]
                                 },
                       "id": 1}

    request = json.dumps(GetSongDetails)
    response = xbmc.executeJSONRPC(request)
    SongDetails = json.loads(response)
    return SongDetails["result"]["songdetails"]

def CreateListItem(Song):
    sp = SongProperties(Song)

    if (len(sp["artist"]) > 0):
        artist = sp["artist"][0]
    trackTitle = sp["title"]
    album      = sp["album"]
    trackPath  = sp["file"]
    thumb      = sp["thumbnail"]
    duration   = int(sp["duration"])
    fanart     = sp["fanart"]

    if (fanart == ""):
        cache_name = xbmc.getCacheThumbName( str(artist) )
        fanart = f"special://profile/Thumbnails/Music/Fanart/{cache_name}"

    listitem = xbmcgui.ListItem(trackTitle)
    listitem.setInfo('music', { 'title': trackTitle, 'artist': artist, 'album': album, 'duration': duration })
    listitem.setArt({"thumb":thumb, "fanart": fanart})

    return listitem

And voila, the playlist in Chorus and in Kore looks neat without touching the Kodi UI.
Reply



Logout Mark Read Team Forum Stats Members Help
Adding songs to playlist0
This forum uses Lukasz Tkacz MyBB addons.