Endolith (owner)

Revisions

gist: 207843 Download_button fork
public
Description:
Banshee remote server
Public Clone URL: git://gist.github.com/207843.git
Embed All Files: show embed
readme.txt #
1
Modified version of http://www.dartmouth.edu/~nstamato/android.html
bansheeserver.py #
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
 
#Copyright (C) 2009 Nikitas Stamatopoulos
# Modified by endolith@gmail.com 2009-10
 
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
 
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
 
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
 
import dbus
import re
import os
import SocketServer
import socket
import sys
 
args = sys.argv[1:]
if args:
if args[0].isdigit():
port = int(args[0])
print ('Using port %s' % port)
else:
print ('First argument should be a port number.')
sys.exit()
else:
print ('No port specified. Using default port.')
port = 8484
 
bus = dbus.SessionBus() # DBus requires X11 to be operating? Xvfb for headless operation?
banshee = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlayerEngine") # This starts a Banshee instance, too
controller = bus.get_object("org.bansheeproject.Banshee", "/org/bansheeproject/Banshee/PlaybackController")
volume = 0
home = os.environ['HOME']
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.settimeout(None)
server_socket.bind(("", port))
server_socket.listen(5)
test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
test_socket.connect(('google.com',0))
ip = test_socket.getsockname()[0]
test_socket.close()
print 'Started BansheeRemote server on port %s' % port
print 'Your IP is %s' % ip
try:
while 1:
client_socket, address = server_socket.accept()
received = client_socket.recv(512)
receivedSplit = received.split('/')
action = receivedSplit[0]
var = receivedSplit[1]
print action
 
if action == "coverImage":
artwork_id = banshee.GetCurrentTrack()['artwork-id']
try:
cover = open(home + '/.cache/album-art/' + artwork_id + '.jpg')
reply = cover.read()
cover.close()
except IOError:
reply = ""
client_socket.send(reply)
 
if action == "coverExists":
reply = "false"
if(banshee.GetCurrentTrack().has_key('artwork-id')):
reply = "true"
artwork_id = banshee.GetCurrentTrack()['artwork-id']
if os.path.isfile(home + '/.cache/album-art/' + artwork_id + '.jpg'):
size = int(os.path.getsize(home + '/.cache/album-art/' + artwork_id + '.jpg'))
if size > 110000:
reply = "false"
else:
reply = "false"
client_socket.send(reply)
 
if action == "playPause":
banshee.TogglePlaying()
 
if action == "next":
controller.Next(False)
 
if action == "prev":
controller.Previous(False)
 
# Why does this take so long?
# Is there a relative command?
# What does this change exactly?
if action == "volumeDown":
currVol = int(banshee.GetVolume())
currVol = currVol - 10
if currVol < 0:
currVol = 0
currVol = dbus.UInt16(currVol)
banshee.SetVolume(currVol)
 
if action == "volumeUp":
currVol = int(banshee.GetVolume())
currVol = currVol + 10
if currVol > 100:
currVol = 100
currVol = dbus.UInt16(currVol)
banshee.SetVolume(currVol)
 
# Is there a mute command?
if action == "mute":
currVol = int(banshee.GetVolume())
if volume != 0:
banshee.SetVolume(dbus.UInt16(volume))
volume = 0
else:
volume = currVol
banshee.SetVolume(dbus.UInt16(0))
 
if action == "status":
reply = str(banshee.GetCurrentState())
client_socket.send(reply)
 
if action == "album":
reply = banshee.GetCurrentTrack()["album"]
client_socket.send(reply.encode('utf-8'))
 
if action == "artist":
reply = banshee.GetCurrentTrack()["artist"]
client_socket.send(reply.encode('utf-8'))
 
if action == "title":
reply = banshee.GetCurrentTrack()["name"]
client_socket.send(reply.encode('utf-8'))
 
if action == "trackCurrentTime":
reply = str(int(banshee.GetPosition()) / 1000)
client_socket.send(reply)
 
if action == "trackTotalTime":
reply = str(int(banshee.GetLength()) / 1000)
client_socket.send(reply)
 
if action == "seek":
var = int(var)
var = var * 1000
banshee.SetPosition(dbus.UInt32(var))
 
if action == "shuffle":
shuffle = int(controller.GetShuffleMode())
controller.SetShuffleMode((shuffle + 1) % 2)
if shuffle == 0:
reply = "on"
else:
reply = "off"
client_socket.send(reply)
 
if action == "repeat":
repeat = int(controller.GetRepeatMode())
newrepeat = (repeat + 1) % 3
controller.SetRepeatMode(newrepeat)
if newrepeat == 0:
reply = "off"
if newrepeat == 1:
reply = "all"
if newrepeat == 2:
reply = "single"
client_socket.send(reply)
 
if action == "all":
status = str(banshee.GetCurrentState())
album = banshee.GetCurrentTrack()["album"] # Crashes if there is nothing playing or selected
artist = banshee.GetCurrentTrack()["artist"]
title = banshee.GetCurrentTrack()["name"]
trackCurrentTime = str(int(banshee.GetPosition()) / 1000)
trackTotalTime = str(int(banshee.GetLength()) / 1000)
coverExists = "false"
if banshee.GetCurrentTrack().has_key('artwork-id'):
coverExists = "true"
artwork_id = banshee.GetCurrentTrack()['artwork-id']
if os.path.isfile(home + '/.cache/album-art/' + artwork_id + '.jpg'):
size = int(os.path.getsize(home + '/.cache/album-art/' + artwork_id + '.jpg'))
if size > 110000:
coverExists = "false"
else:
coverExists = "false"
sep = '/'
fields = (status, album, artist, title, trackCurrentTime, trackTotalTime, coverExists)
reply = sep.join(fields)
client_socket.send(reply.encode('utf-8'))
client_socket.close()
 
except (KeyboardInterrupt, SystemExit): # http://effbot.org/zone/stupid-exceptions-keyboardinterrupt.htm
print "\nInterrupted by Ctrl+C"
server_socket.close()
sys.exit()