(2009-08-15, 12:59)ErlendSB Wrote: MusicIP mixer works great!
I threw togheter a little script that uses the currently playing song to create a playlist.
Add the script to a remotekey or on your favourite dialog, and you've got a quick way of creating intelligent playlists.
As you can see from the code, the port number for the MusicIP api is hardcoded to "1234", and the playlist size is "12". Change them as you feel.
Code:
# Import XBMC module
import xbmc,urllib,urllib2 , re, os
from string import split, replace, find
# Script constants
__scriptname__ = "Music MIX"
if xbmc.Player().isPlayingAudio() == False:
self.close()
currentlyPlaying = xbmc.Player().getMusicInfoTag().getURL()
print currentlyPlaying
currentlyPlaying = currentlyPlaying.replace(":","%3A").replace(" ","+")
apiPath = 'http://localhost:1234/api/mix?song='
options = '&size=12&sizeType=tracks&content=text'
# The url in which to use
Base_URL = apiPath + currentlyPlaying + options
print Base_URL
#Pre-define global Lists
LinkURL = []
WebSock = urllib.urlopen(Base_URL) # Opens a 'Socket' to URL
WebHTML = WebSock.read() # Reads Contents of URL and saves to Variable
WebSock.close() # Closes connection to url
LinkURL = WebHTML.split("\n")
xbmc.PlayList(0).clear()
for l in LinkURL:
if not l == "":
xbmc.PlayList(0).add(l)
I've been using this script for ages but found it was behaving a bit oddly since Kodi arrived. Simply put, if I was playing track 7 of an album and generated a musicIP mix from that track, the script would correctly generate a mix but only play it from track 8. I've fixed that and I've also altered the script so it copes with URL unsafe characters (", &, + etc.) in music paths and so it pops up a notification to give the user some feedback that a mix has been generated.
This is my first delve into the python XBMC API, it would be cool to turn it into an addon - but I've no idea how to do that. Also, I've been using this for a few days and have tested the various edge scenarios I can think of and the script behaves fine, but there may still be bugs in it. I'm open to suggestions
PS - the notification includes a high res musicIP icon that I can't attach to this post, so I've uploaded it to a random image hosting website
here. The script will need pointing to wherever you put this (edit the last line).
Code:
# Import XBMC module
import xbmc,urllib,urllib2 , re, os
from string import split, replace, find
# Script constants
__scriptname__ = "Music MIX"
if xbmc.Player().isPlayingAudio() == False:
self.close()
currentlyPlaying = xbmc.Player().getMusicInfoTag().getURL()
currentlyPlaying = urllib.quote_plus(currentlyPlaying)
tag = xbmc.Player().getMusicInfoTag()
artist = tag.getArtist()
title = tag.getTitle()
size=str(25)
apiPath = 'http://localhost:10002/api/mix?song='
options = '&size='+size+'&sizeType=tracks&content=text'
# The url in which to use
Base_URL = apiPath + currentlyPlaying + options
#Pre-define global Lists
LinkURL = []
WebSock = urllib.urlopen(Base_URL) # Opens a 'Socket' to URL
WebHTML = WebSock.read() # Reads Contents of URL and saves to Variable
WebSock.close() # Closes connection to url
LinkURL = WebHTML.split("\n")
playList = xbmc.PlayList(0)
currentPos = playList.getposition()
if playList.size()>1 and currentPos < playList.size()-1: #Since Kodi playlist clear would leave the new play list playing at oldplaylist.position, this code fixes that
for i in range (currentPos+1, playList.size()):
playList.remove(playList[currentPos+1].getfilename())
first = True #first item returned from musicIP is the item currently playing, so don't add that to playlist.
for l in LinkURL:
if not l == "":
if not (first):
playList.add(url=l)
first=False
xbmc.executebuiltin('Notification(Playlist Generated,'+size +' songs like the current coming up...,5000,d:\scripts\musicip.png)')