Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@endolith
Created October 11, 2009 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save endolith/207843 to your computer and use it in GitHub Desktop.
Save endolith/207843 to your computer and use it in GitHub Desktop.
Banshee remote server (by Nikitas Stamatopoulos)
Modified version of http://www.dartmouth.edu/~nstamato/android.html
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment