Changeset 3

User picture

Author: holler

(2008/11/17 02:19) Over 3 years ago

0.2.1

Affected files

Updated itunes.py Download diff

23
1
#!/usr/bin/python
1
#!/usr/bin/python
2
###################################################
2
###################################################
3
#
3
#
4
#		iTunes Python Shell Script v0.2
4
#		iTunes Python Shell Script v0.2.1
5
#			http://blog.jadecell.org/
5
#			http://blog.jadecell.org/
6
#
6
#
7
#			inspired by the bash shell scripts on MacOSXHints
7
#			inspired by the bash shell scripts on MacOSXHints
...
...
10
#		README
10
#		README
11
#			- give read/execute permissions to the file (ex 'chmod 755 ./itunes')
11
#			- give read/execute permissions to the file (ex 'chmod 755 ./itunes')
12
#			- import this script into your bin directory (ex '/usr/bin' or '~/bin')
12
#			- import this script into your bin directory (ex '/usr/bin' or '~/bin')
13
#			- alternatively you can make a symbolic link:
14
#				sudo ln -s /path/to/your/mypytunes/itunes.py /usr/bin/itunes
13
15
14
from os import system as cmd
16
from os import system as cmd
15
from sys import argv as args
17
from sys import argv as args
16
18
17
# Main Script function to run
19
def pyTunes(args):
18
def pyTunes():
20
	"""Main Script function to run"""
21
19
	try: args[1]			# if no argument
22
	try: args[1]			# if no argument
20
	except: defHelp()		#	show help definition and exit
23
	except: defHelp()		#	show help definition and exit
21
24
22
	if(mDefs.has_key(args[1])):	# Verify if argument is available and execute
25
	if(mDefs.has_key(args[1])):	# Verify if argument is available and execute
23
		mounter()
26
		mounter(args)
24
	else:				# otherwise show list of arguments available
27
	else:				# otherwise show list of arguments available
25
		print "\n** invalid argument **\n"
28
		print "\n** invalid argument **\n"
26
		showHelp()
29
		showHelp()
27
# Raise exception and exit out of program
28
def myException(x):
30
def myException(x):
31
	"""Raise exception and exit out of the program"""
32
29
	print x
33
	print x
30
	raise SystemExit
34
	raise SystemExit
31
# Minimal Help function (tells user to run 'help' for list of arguments)
32
def defHelp():
35
def defHelp():
33
	myException(" No arguments provided - Usage %s <option>\n for a list of commands: %s -h"%(args[0],args[0]))
36
	"""Minimal Help function (tells user to run 'help' for list of arguments)"""
34
# Help function
37
38
	myException(" No arguments provided - Usage %s <option>\n For a list of commands: %s help"%(args[0],args[0]))
35
def showHelp():
39
def showHelp():
40
	"""Help function showing a list of commands when '<script> -h' is run"""
41
36
	helpHeader = "---------------------------------------------------"
42
	helpHeader = "---------------------------------------------------"
37
	print helpHeader+"\n  iTunes Command Line Interface\n"+helpHeader
43
	print helpHeader+"\n  iTunes Command Line Interface\n"+helpHeader
38
	print "Usage: %s <option>\n"%args[0]
44
	print "Usage: %s <option>\n"%args[0]
...
...
49
		x += mDefs[i]['h']
55
		x += mDefs[i]['h']
50
		print x
56
		print x
51
	myException(helpHeader)
57
	myException(helpHeader)
52
# Main function for iTunes execution
58
def mounter(args):
53
def mounter():
59
	"""iTunes execution"""
54
	v = mDefs[args[1]]
60
	v = mDefs[args[1]]
55
	try:
61
	try:
56
		cmd("osascript -e 'tell application \"iTunes\" "+v['c']+"'")
62
		cmd("osascript -e 'tell application \"iTunes\" "+v['c']+"'")
...
...
60
			v['r']()
66
			v['r']()
61
		except KeyError:
67
		except KeyError:
62
			print "invalid 2"
68
			print "invalid 2"
63
# Play streaming location
64
def ostream():
69
def ostream():
70
	"""Play streaming location"""
65
	try:
71
	try:
66
		print "Playing playlist %s"%(args[2])
72
		print "Playing playlist %s"%(args[2])
67
		#cmd('osascript -e \'tell application "iTunes"\' -e "open location \"%s\"" -e "end tell"\''%(args[2]))
73
		#cmd('osascript -e \'tell application "iTunes"\' -e "open location \"%s\"" -e "end tell"\''%(args[2]))
68
		cmd('osascript -e \'tell application "iTunes"\' -e \'open location "%s"\' -e \'end tell\''%(args[2]))
74
		cmd('osascript -e \'tell application "iTunes"\' -e \'open location "%s"\' -e \'end tell\''%(args[2]))
69
	except:
75
	except:
70
		myException("Please provide the stream location you want to play")
76
		myException("Please provide the stream location you want to play")
71
# Show iTunes player status (play/pause)
72
def status():
77
def status():
78
	"""Play streaming location"""
73
	cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')
79
	cmd('osascript -e \'tell application "iTunes" to player state as string\' > /tmp/iTuneState')
74
	f = open('/tmp/iTuneState', 'r')
80
	f = open('/tmp/iTuneState', 'r')
75
	s = f.readline().strip()
81
	s = f.readline().strip()
...
...
78
	shufstat()
84
	shufstat()
79
	if s == "playing":
85
	if s == "playing":
80
		trackstat()
86
		trackstat()
81
# Show Playlist, Artist, and Track that is currently playing
82
def trackstat():
87
def trackstat():
88
	"""Show Playlist, Artist, and Track that is currently playing"""
83
	cmd('osascript -e \'tell application "iTunes" to name of current playlist as string\' > /tmp/iTunesPlaylist')
89
	cmd('osascript -e \'tell application "iTunes" to name of current playlist as string\' > /tmp/iTunesPlaylist')
84
	cmd('osascript -e \'tell application "iTunes" to artist of current track as string\' >> /tmp/iTunesPlaylist')
90
	cmd('osascript -e \'tell application "iTunes" to artist of current track as string\' >> /tmp/iTunesPlaylist')
85
	cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')
91
	cmd('osascript -e \'tell application "iTunes" to name of current track as string\' >> /tmp/iTunesPlaylist')
...
...
88
	x,y,z,a = s.split('\n')
94
	x,y,z,a = s.split('\n')
89
	print "Current track %s: %s, on playlist \"%s\""%(y,z,x)
95
	print "Current track %s: %s, on playlist \"%s\""%(y,z,x)
90
	cmd('rm /tmp/iTunesPlaylist')
96
	cmd('rm /tmp/iTunesPlaylist')
91
# Show the shuffle status (on/off)
92
def shufstat():
97
def shufstat():
98
	"""Show the shuffle status (on/off)"""
93
	cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")
99
	cmd("osascript -e 'tell application \"iTunes\" to get shuffle of current playlist' > /tmp/iTunesShuffle")
94
	f = open('/tmp/iTunesShuffle', 'r')
100
	f = open('/tmp/iTunesShuffle', 'r')
95
	s = f.readline().strip()
101
	s = f.readline().strip()
96
	print "Shuffling: %s"%(s)
102
	print "Shuffling: %s"%(s)
97
	cmd('rm /tmp/iTunesShuffle')
103
	cmd('rm /tmp/iTunesShuffle')
98
# Change volume
99
def vol():
104
def vol():
105
	"""Change Volume"""
100
	print "Changing iTunes volume level."
106
	print "Changing iTunes volume level."
101
	cmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')
107
	cmd('osascript -e \'tell application "iTunes" to get (sound volume as integer)\' > /tmp/iTunesVol')
102
	f = open('/tmp/iTunesVol')
108
	f = open('/tmp/iTunesVol')
...
...
115
	cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))
121
	cmd('osascript -e \'tell application "iTunes" to set sound volume to (%i as integer)\''%(newvol))
116
	print "from %i to %i"%(s,newvol)
122
	print "from %i to %i"%(s,newvol)
117
	cmd('rm /tmp/iTunesVol')
123
	cmd('rm /tmp/iTunesVol')
118
# Change playlist
119
def playlist():
124
def playlist():
125
	"""Change playlist"""
120
	try:
126
	try:
121
		print "Changing iTunes plalist to %s"%(args[2])
127
		print "Changing iTunes plalist to %s"%(args[2])
122
		cmd('osascript -e \'tell application "iTunes"\' -e \'set the new_playlist to "%s" as string\' -e "play playlist new_playlist" -e
"end tell"'%(args[2]))
128
		cmd('osascript -e \'tell application "iTunes"\' -e \'set the new_playlist to "%s" as string\' -e "play playlist new_playlist" -e
"end tell"'%(args[2]))
123
	except:
129
	except:
124
		myException("Pleasae define the playlist you want to play")
130
		myException("Pleasae define the playlist you want to play")
125
# List playlists available on iTunes
126
def llist():
131
def llist():
132
	"""List playlists available on iTunes"""
127
	print "Listing available playlists:"
133
	print "Listing available playlists:"
128
	cmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" >
/tmp/iTunesPlaylists')
134
	cmd('osascript -e \'tell application "iTunes"\' -e \'name of every user playlist of source "Library"\' -e "end tell" >
/tmp/iTunesPlaylists')
129
	f = open('/tmp/iTunesPlaylists')
135
	f = open('/tmp/iTunesPlaylists')
...
...
242
}
248
}
243
249
244
if __name__ == "__main__":
250
if __name__ == "__main__":
245
	pyTunes()
251
	pyTunes(args)