Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Popular songs of Last.fm to iTunes library
from Tkinter import *
import requests
import os
import sys
# set utf8 encoding to avoid problems with names of songs
reload(sys)
sys.setdefaultencoding("utf-8")
def cmdPlaylist():
doPlaylist(entry.get())
def doPlaylist(artistParam):
params = dict(
artist=artistParam,
api_key='your api key :)',
format='json',
method='artist.gettoptracks'
)
resp = requests.get(url='http://ws.audioscrobbler.com/2.0/', params=params)
data = resp.json()
toptracks = data['toptracks']['track']
# delete songs of playlist
os.system("osascript -e 'tell application \"iTunes\"' -e 'delete tracks of playlist \"_Popular\"' -e 'end tell'")
for track in toptracks:
os.system("osascript -e 'tell application \"iTunes\"' -e 'set trackSel to (first track whose name contains \"" + track['name'] + "\" and artist contains \""+ artistParam +"\")' -e 'duplicate trackSel to playlist \"_Popular\"' -e 'end tell'")
os.system("osascript -e 'tell application \"iTunes\" to play playlist \"_Popular\"'")
# If called with arguments auto-launch playlist else, entry window
if len(sys.argv) > 1:
doPlaylist(sys.argv[1:][0])
else:
root = Tk()
label1 = Label(root, text="Input artist name")
entry = Entry(root, bd=5)
submit = Button(root, text="Submit", command=cmdPlaylist)
label1.pack()
entry.pack()
submit.pack(side=BOTTOM)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.