File: itunescontroller.py

package info (click to toggle)
pyro4 4.82-2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 2,528 kB
  • sloc: python: 17,736; makefile: 169; sh: 113; javascript: 62
file content (55 lines) | stat: -rw-r--r-- 1,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from __future__ import print_function
import subprocess
import socket

import Pyro4


#  You can get a lot more info about scripting iTunes here:
#  http://dougscripts.com/itunes/

@Pyro4.expose
class ITunes(object):
    def __init__(self):
        # start itunes
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to player state"])

    def play(self):
        # continue play
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to play"])

    def pause(self):
        # pause play
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to pause"])

    def stop(self):
        # stop playing
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to stop"])

    def next(self):
        # next song in list
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to next track"])

    def previous(self):
        # previous song in list
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to previous track"])

    def playlist(self, listname):
        # start playing a defined play list
        subprocess.call(["osascript", "-e", "tell application \"iTunes\" to play playlist \"{0}\"".format(listname)])

    def currentsong(self):
        # return title and artist of current song
        return subprocess.check_output(["osascript", "-e", "tell application \"iTunes\"",
                                        "-e", "set thisTitle to name of current track",
                                        "-e", "set thisArtist to artist of current track",
                                        "-e", "set output to thisTitle & \" - \" & thisArtist",
                                        "-e", "end tell"]).strip()


print("starting...")
itunes = ITunes()
daemon = Pyro4.Daemon(host=socket.gethostname(), port=39001)
uri = daemon.register(itunes, "itunescontroller")
print("iTunes controller started, uri =", uri)
daemon.requestLoop()