Last active
August 29, 2015 14:06
-
-
Save PaNaVTEC/707ed669b4982d3e0c4a to your computer and use it in GitHub Desktop.
Popular songs of Last.fm to iTunes library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to use, | |
You need a organized iTunes library :) | |
1. Create manually a playlist called "_Popular" in iTunes | |
2. Replace "YOUR_API" in script with a valid last.fm api key | |
3. Execute script, if you send a parameter it will auto launch, else will show a window to input artist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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