Skip to content

Instantly share code, notes, and snippets.

@taruta811
Created March 23, 2011 19:17
Show Gist options
  • Save taruta811/883746 to your computer and use it in GitHub Desktop.
Save taruta811/883746 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding:utf-8
from appscript import *
import sys
class iTunes(object):
def __init__(self):
self.itunes = app("iTunes")
self.search_result = []
def ShowLists(self):
play_lists = self.itunes.user_playlists()
cnt = 0
for plist in play_lists:
print "%3d:%s" % (cnt, plist.name().encode('utf-8'))
cnt += 1
def ShowTracks(self,playlist=None):
ctrack = self.itunes.current_track()
if playlist == None:
plist = self.itunes.current_playlist
else:
for item in self.itunes.user_playlists():
if item.name() == playlist:
plist = item
break
self.search_result = plist.tracks()
cnt = 0
print "PlayList Name : %s" % plist.name().encode('utf-8')
for track in self.search_result:
check_marker = "*" if ctrack == track else " "
sys.stdout.write("%s%3d:" % (check_marker,cnt))
self.ShowTrack(track)
cnt += 1
def ShowTrack(self,track=None):
ctrack = self.itunes.current_track
if track == None:
track = ctrack
print "%s : %s" % ((track.name()+"("+track.time()+")").encode('utf-8'),track.artist().encode('utf-8'))
def SearchTrack(self,word,only="all",playlist="current"):
Only = {"all":k.all,"albums":k.albums,"songs":k.songs,"artists":k.artists,"composers":k.composers,"displayed":k.displayed}
Playlist = {}
for plist in self.itunes.user_playlists():
Playlist.setdefault(plist.name(),plist)
Playlist.setdefault("current",self.itunes.current_playlist)
if not only in Only:
only = "all"
if not playlist in Playlist:
playlist = "current"
self.search_result = Playlist[playlist].search(for_=word,only=Only[only])
cnt = 0
for track in self.search_result:
sys.stdout.write("%3d:" % cnt)
self.ShowTrack(track)
cnt += 1
def PlayPause(self):
self.itunes.playpause()
def Play(self):
self.itunes.play()
def Stop(self):
self.itunes.stop()
def Pause(self):
self.itunes.pause()
def Next(self):
self.itunes.next_track()
def Prev(self):
self.itunes.previous_track()
def Select(self,num):
if not 0<= num < len(self.search_result):
return
self.search_result[num].play()
def Volume(self,vol = -1):
if not 0 <= vol <= 100:
print self.itunes.sound_volume()
else:
self.itunes.sound_volume.set(vol)
def ShowHelp():
help_str = """
usage: itp play
itp next
itp stop
itp pause
itp togle
itp next
itp prev
itp volume [vol]
itp plists
itp list [playlist]
itp track
itp search word [only] [playlist]
"""
print help_str
if __name__ == '__main__':
it = iTunes()
argv = sys.argv
args = []
clen = len(argv)
if clen == 1 or clen >= 6:
ShowHelp()
sys.exit()
elif 1 <= clen-2 <= 3:
for item in argv[2:]:
if item.isdigit():
args.append(int(item))
else:
args.append(item)
clen -= 2
command = argv[1]
Commands = [
{"togle":it.PlayPause,"help":ShowHelp,"play" :it.Play, "stop":it.Stop, "pause" :it.Pause, "next":it.Next,"prev":it.Prev,"volume":it.Volume,"plists":it.ShowLists,"list":it.ShowTracks,"track":it.ShowTrack},
{"volume":it.Volume,"search":it.SearchTrack,"list":it.ShowTracks,"track":it.ShowTrack},
{"search":it.SearchTrack},
{"search":it.SearchTrack}
]
if not command in Commands[clen]:
print "command not found : \"%s\"" % command
ShowHelp()
sys.exit()
if clen==0:
Commands[clen][command]()
if command == "next" or command == "prev":
it.ShowTrack()
else:
try:
Commands[clen][command](*args)
except:
print "ArgsError"
sys.exit()
if command == "list" or command == "search":
if len(it.search_result) == 0:
sys.exit()
try:
to = raw_input('Input track number >> ')
to = int(to)
it.Select(to)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment