-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrarserver.py
executable file
·138 lines (113 loc) · 3.82 KB
/
rarserver.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
#!/usr/bin/python
# This script is inspired by : http://www.navarin.de/projects/rbox/
# Website ans repository : https://github.com/bsaleil/rhythmbox-android-remote
#Copyright (C) 2009 Baptiste Saleil
#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/>.
# TODO :
# Socket : Close socket in a good way
# Repeat : Not available with mpris interfaces
# Library sync : Not yet implemented
import socket
import dbus
# DBus
playerPath = 'org.mpris.MediaPlayer2.Player'
bus = dbus.SessionBus()
rbox = bus.get_object( 'org.gnome.Rhythmbox3', '/org/mpris/MediaPlayer2')
player = dbus.Interface(rbox, playerPath)
playerPr = dbus.Interface(rbox, 'org.freedesktop.DBus.Properties')
# Get current IP
ipsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ipsocket.connect(('www.google.com',0))
ip = ipsocket.getsockname()[0]
ipsocket.close();
# Server socket
port = 8484
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.settimeout(None)
server_socket.bind(("", port))
server_socket.listen(5)
# Welcome prints
print "Server is listening."
print "Host : " + ip
print "Port : " + str(port)
print "(Ctrl+C to quit)"
# Main loop
while 1:
# Intercept keyboard interruption
try:
# Get action from device
client_socket, address = server_socket.accept()
received = client_socket.recv(512)
action, var = received.split('/')
if action != "all" : print action
# Set new position
if action == "seek" :
current = playerPr.Get(playerPath,"Position")
new = int(var)*1000000
seekVal = new - current
player.Seek(seekVal)
# Enable/Disable shuffle mode
if action == "shuffle" :
current = playerPr.Get(playerPath, "Shuffle")
if current == 0 :
playerPr.Set(playerPath, "Shuffle", True)
else :
playerPr.Set(playerPath, "Shuffle", False)
# Volume Up
if action == "volumeUp" :
current = playerPr.Get(playerPath, "Volume")
playerPr.Set(playerPath, "Volume", current + 0.1)
# Volume Down
if action == "volumeDown" :
current = playerPr.Get(playerPath, "Volume")
playerPr.Set(playerPath, "Volume", current - 0.1)
# Cover request
if action == "coverImage" :
meta = playerPr.Get(playerPath,"Metadata")
if meta.has_key("mpris:artUrl") :
path = str(meta["mpris:artUrl"])
path = path[7:len(path)]
cover = open(path)
reply=cover.read()
cover.close()
client_socket.send(reply)
# All informations about current song
if action == "all":
status = playerPr.Get(playerPath,"PlaybackStatus")
meta = playerPr.Get(playerPath,"Metadata")
position = playerPr.Get(playerPath,"Position")/1000000
artExists = "false";
if meta.has_key("mpris:artUrl") :
artExists = "true";
ret = ""
ret += status.lower() + "/"
ret += meta["xesam:album"] + "/"
ret += meta["xesam:artist"][0] + "/"
ret += meta["xesam:title"] + "/"
ret += str(position) + "/"
ret += str(meta["mpris:length"] / 1000000) + "/"
ret += str(artExists)
client_socket.send(ret.encode('utf-8'))
# Previous track
if action == "prev":
player.Previous()
# Play/Pause action
if action == "playPause":
player.PlayPause()
# Next track
if action == "next":
player.Next()
client_socket.close()
# End of the program
except (KeyboardInterrupt, SystemExit):
server_socket.close()
break;