Skip to content

Instantly share code, notes, and snippets.

@PaNaVTEC
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaNaVTEC/707ed669b4982d3e0c4a to your computer and use it in GitHub Desktop.
Save PaNaVTEC/707ed669b4982d3e0c4a to your computer and use it in GitHub Desktop.
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