/
yamc.py
293 lines (252 loc) · 7.62 KB
/
yamc.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import subprocess
import event
import plugin
import json
import time
import requests
from playlist import Playlist
from audiolibrary import AudioLibrary
import vlc
from rpc import RPC
from os import path,mkdir
from urllib import unquote,quote
if not path.exists("log"): mkdir("log")
def subdict(d,p):
return { k:d[k] for k in p if d.has_key(k) }
class JSONRPC(RPC):
version= { "major": 6, "minor": 25, "patch": 2}
@staticmethod
def Ping():
return 'pong'
@classmethod
def Version(c):
return c.Get(["version"])
class Settings(RPC):
audiooutputpassthrough= False
@classmethod
def GetSettingValue(c,setting):
name= "".join( setting.split(".") )
if hasattr(c, name):
return { setting:getattr(c,name) }
class Application(RPC):
version= { 'major':15, 'tag':'stable', 'minor':2,'revision':'unknown'}
volume= 50
muted= False
@classmethod
def VolumeChanged(c):
event.post().OnVolumeChanged(muted=c.muted, volume=c.volume)
@classmethod
def SetMute(c,mute):
c.muted= not c.muted
if c.muted:
vlc.command("volume",val=0)
else:
vlc.command("volume",val=256*(c.volume/100.0))
c.VolumeChanged()
return "OK"
@classmethod
def SetVolume(c,volume):
c.volume=volume
vlc.command("volume",val=256*(c.volume/100.0))
c.VolumeChanged()
def Mute(): Application.SetMute("toggle")
def skipminus(): Player.Minus()
def skipplus(): Player.Plus()
def forward(): vlc.command("seek",val="+5%")
def reverse(): vlc.command("seek",val="-5%")
def volumeminus(): Application.SetVolume( Application.volume-16 )
def volumeplus(): Application.SetVolume( Application.volume+16 )
#MusicLibrary= "Hello"
#Videos="Videos"
#TvShowTitles="TvShowTitles"
#def ActivateWindow(w): print "ActivateWindows:", w
class XBMC(RPC):
SystemPlatformLinux= True
SystemPlatformLinuxRaspberryPi= True
SystemKernelVersion= "Gilles"
SystemBuildVersion= "0.0"
def seconds2time(ss):
h= int( ss//3600 )
ss= ss%3600
m= int( ss//60 )
ss= ss%60
s= int( ss )
ss= ss%1
ms= int( ss*1000)
return { "hours": h, "minutes": m, "seconds": s, "milliseconds": ms }
def time2seconds(ss):
return ss["hours"]*3600 + ss["minutes"]*60 + ss["seconds"]
class Player(RPC):
playerid=0
type="audio"
audiostreams=[]
canseek= True
currentaudiostream={}
currentsubtitle=None
partymode= False
playlistid= 0
position= 0
repeat= "off"
shuffled= False
speed= 1
subtitleenabled= False
subtitles=[]
time= { "hours": 0, "minutes":0, "seconds": 0, "milliseconds":0 }
totaltime= { "hours": 0, "minutes":0, "seconds": 0, "milliseconds":0 }
duration=0
item={}
@classmethod
def OnPlay(c,songid=None,**item):
item[ "type" ]="song"
if songid!=None: item["id"]=songid
event.post().Player.OnPlay(
items= item,
player= { "playerid": c.playerid, "speed":c.speed } )
@classmethod
def Plus(c):
vlc.command("pl_next")
c.position= Playlist.position( **vlc.status() )
print "plus:", c.position
c.OnPlay( **Playlist.items[c.position] )
Playlist.dirty= True
@classmethod
def Minus(c):
vlc.command("pl_previous")
c.position= Playlist.position( **vlc.status)
c.OnPlay( **Playlist.items[c.position] )
Playlist.dirty= True
@classmethod
def GetActivePlayers(c):
print c.playerid, c.type
return [{"playerid":c.playerid, "type":c.type}]
@classmethod
def Open(c,item):
print item
c.item= Playlist.Open( **item )
if not c.item: raise Exception, 'Cannot open'+ str(item)
c.type= "audio"
c.playerid=0
c.OnPlay(**c.item)
@classmethod
def PlayPause(c,playerid,**k):
if k.has_key("play"):
if k["play"] and c.speed==1: return "OK"
if not k["play"] and c.speed==0: return "OK"
s=vlc.command("pl_pause")
if s["state"]=="paused":
c.speed= 0
elif s["state"]=="playing":
c.speed= 1
c.OnPlay()
return "OK"
@classmethod
def Stop(c,playerid):
vlc.command("pl_stop")
return "OK"
@classmethod
def SetSpeed(c,playerid,speed):
if speed==1:
vlc.command("pl_play")
@classmethod
def GoTo(c,playerid, to ):
vlc.command("pl_play",id=Playlist.getnodes()[to] )
c.position= to
return "OK"
@classmethod
def SetRepeat(c,playerid,repeat):
print "repeat:", repeat
if repeat=="one":
vlc.command("pl_loop")
elif repeat=="all":
vlc.command("pl_repeat")
c.repeat= repeat
@classmethod
def SetShuffle(c,playerid,shuffle):
vlc.command("pl_random")
return "OK"
@classmethod
def Seek(c,playerid,value):
if type(value)==dict:
pos= time2seconds(value)
elif type(value)==float:
pos= value*c.duration//100
print value,pos
vlc.command("seek",val=pos)
r= c.Get(["totaltime","percentage","time"] )
print r
return r
@classmethod
def GetItem(c,playerid,properties=[]):
r= subdict( c.item, properties )
# print c.item
return { "item":r }
@classmethod
def GetProperties(c,playerid,properties):
s= vlc.status();
try:
stream= vlc.audiostream( s["information"]["category"] )
XBMC.MusicPlayerCodec= stream["Codec"]
XBMC.MusicPlayerSampleRate= stream["Sample_rate"]
XBMC.MusicPlayerBitRate= stream["Bitrate"]
except KeyError as e:
print "KeyError:", e
if s["loop"]:
c.repeat="on"
elif s["repeat"]:
c.repeat="all"
else:
c.repeat="off"
c.shuffled= s["random"]
c.duration= s["length"]
c.percentage=s["position"]
c.time= seconds2time(s["position"]*s["length"])
c.totaltime= seconds2time(s["length"])
c.volume= s["volume"]/255.0
c.position= Playlist.position(**s)
if c.position<len(Playlist.items):
c.item= Playlist.items[c.position]
if s["state"]=="stopped":
c.speed=0
elif s["state"]=="playing":
c.speed=1
# print c.Get(properties)
return c.Get(properties)
class VideoLibrary(RPC):
musicvideos= [ { "musicvideoid":1, "album":"emerald" } ]
@classmethod
def GetMovies(c,**p):
return [ { "title": "indianajones", "file":"indiana.Avi" } ]
@classmethod
def GetTVShows(c,**p):
return [ { "title": "seinfield", "file":"seinfield.Avi" } ]
@classmethod
def GetMusicVideos(c,properties,limits,sort=False):
return c.GetList("musicvideos",properties,limits)
class TVLibrary(RPC):
@classmethod
def GetMovies(c,**p):
return [ { "title": "Hello", "file":"hello.Avi" } ]
class Files(RPC):
@classmethod
def GetSources(c,**p):
return {}
@classmethod
def GetDirectory(c,**p):
return {}
def handleudp(s):
event.handleudp(s,globals(), locals())
out= open("log/yamc.execute","w")
def execute(j):
print >> out, json.dumps(j)
c,m= j["method"].split(".")
C= globals()[c]
print c,m
if j.has_key("params"):
# print c,m,j["params"]
r=getattr(C,m)(**j["params"])
# print c,m,j["params"],":",r
return r
else:
r= getattr(C,m)()
# print c,m,":",r
return r