forked from ver007/py-kodi-remote-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodi_api.py
297 lines (270 loc) · 8.84 KB
/
kodi_api.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
292
293
294
295
296
297
#!/usr/bin/env python
# coding=utf-8
#
# Copyright 2015 Arn-O. See the LICENSE file at the top-level directory of this
# distribution and at
# https://github.com/Arn-O/py-kodi-remote-controller/blob/master/LICENSE.
'''
Module of functions for Kodi API management.
'''
import requests
import json
import logging
logger = logging.getLogger(__name__)
# API call management
def call_api(server_params, command):
if server_params['tcp']:
ret = call_api_tcp(
server_params['ip'],
server_params['port'],
command)
else:
ret = call_api_http(server_params, command)
return ret
def call_api_http(server_params, command):
logger.debug('call call_api_http')
logger.debug('command: %s', command)
kodi_url = 'http://' + server_params['ip'] + ':' + str(server_params['port']) + '/jsonrpc'
headers = {'Content-Type': 'application/json'}
r = requests.post(
kodi_url,
data=json.dumps(command),
headers=headers,
auth=(server_params['user'], server_params['password']))
ret = r.json()
logger.debug('url: %s', r.url)
logger.debug('status code: %s', r.status_code)
logger.debug('text: %s', r.text)
return ret
def call_api_tcp(ip, port, command):
'''Send the command using TCP'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
logger.debug('command: %s', command)
s.send(json.dumps(command))
data = ''
while True:
filler = s.recv(BUFFER_SIZE)
logger.debug('data received: %s', filler)
logger.debug('length of the filler: %i', len(filler))
data += filler
nb_open_brackets = data.count('{') - data.count('}')
logger.debug('number of open brackets: %i', nb_open_brackets)
if nb_open_brackets == 0:
break
else:
logger.info('api reception incomplete')
s.close()
logger.debug('data length: %i', len(data))
ret = json.loads(data)
logger.debug('return: %s', ret)
def display_result(ret):
'''Display command result for simple methods'''
logger.debug('call display_result')
if 'error' in ret:
logger.error('too bad, something went wrong!')
logger.error('error message: %s', ret['error']['message'])
else:
logger.info('command processed successfully')
# audiolibrary
def audiolibrary_get_albums(server_params, album_id_start, album_id_end):
'''Retrieve all albums whithin limits'''
command = {"jsonrpc": "2.0",
"method": "AudioLibrary.GetAlbums",
"params": {
"limits": {
"start": album_id_start,
"end": album_id_end }
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
return ret['result']
def audiolibrary_get_songs(server_params, song_id_start, song_id_end):
'''Retrieve all songs whithin limits'''
command = {"jsonrpc": "2.0",
"method": "AudioLibrary.GetSongs",
"params": {
"limits": {
"start": song_id_start,
"end": song_id_end }
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
return ret['result']
# playlist
def playlist_add(item_type, item_id, server_params):
'''Add an item to the audio playlist'''
logger.debug('call function playlist_add')
command = {"jsonrpc": "2.0",
"method": "Playlist.Add",
"params": {
"playlistid": 0,
"item": {
item_type: item_id } },
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def playlist_clear(server_params):
'''Clear the audio playlist'''
logger.debug('call function playlist_clear')
command = {"jsonrpc": "2.0",
"method": "Playlist.Clear",
"params": {
"playlistid": 0 },
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def playlist_get_items(server_params):
'''Get all items from the audio playlist'''
#TODO: change to return the item id only
logger.debug('call playlist_get_items')
command = {"jsonrpc": "2.0",
"method": "Playlist.GetItems",
"params": {
"playlistid": 0,
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
items = []
try:
for item in ret['result']['items']:
items.append(item['id'])
logger.debug('items in the playlist: %s', items)
except KeyError:
pass
return items
# player
def player_get_active(server_params):
'''Returns active audio players (boolean)'''
logger.debug('call function player_get_active')
command = {"jsonrpc": "2.0",
"method": "Player.GetActivePlayers",
"id": 1,
}
ret = call_api(server_params, command)
display_result(ret)
is_active = False
for player in ret ['result']:
if player['playerid'] == 0:
is_active = True
logger.debug('active audio player: %s', is_active)
return is_active
def player_get_item(server_params):
'''Get the current played item'''
#TODO: change to return item id only
logger.debug('call function get_item')
command = {"jsonrpc": "2.0",
"method": "Player.GetItem",
"params": {
"playerid": 0,
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
if 'result' in ret:
return ret['result']['item']['id']
else:
return None
def player_get_properties(server_params):
'''Get properties of the played item'''
logger.debug('call function player_get_properties')
command = {"jsonrpc": "2.0",
"method": "Player.GetProperties",
"params": {
"playerid": 0,
"properties": [
"time",
"totaltime",
"percentage",
"position" ] },
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
if 'result' in ret:
result = ret['result']
else:
logger.debug('no properties found, player not active')
result = None
return result
def player_goto(server_params):
'''Go to the next item'''
logger.debug('call function player_goto')
command = {"jsonrpc": "2.0",
"method": "Player.GoTo",
"params":{
"playerid": 0,
"to": 'next'},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def player_open(server_params):
'''Open the audio playlist'''
logger.debug('call function player_open')
command = {"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"playlistid": 0 }
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def player_open_party(server_params):
'''Open the audio player in partymode'''
logger.debug('call function player_open_party')
command = {"jsonrpc": "2.0",
"method": "Player.Open",
"params": {
"item": {
"partymode": "music" }
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def player_play_pause(server_params):
'''Pauses or unpause playback'''
logger.debug('call function player_play_pause')
command = {"jsonrpc": "2.0",
"method": "Player.PlayPause",
"params": {
"playerid": 0,
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
def player_stop(server_params):
'''Stop playback'''
logger.debug('call function player_stop')
command = {"jsonrpc": "2.0",
"method": "Player.Stop",
"params": {
"playerid": 0 },
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
# application
def player_volume(server_params,volume):
'''Volume'''
logger.debug('call function player_volume')
command = {"jsonrpc": "2.0",
"method": "Application.SetVolume",
"params": {
"volume": volume,
},
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
# system
def system_friendly_name(server_params):
'''Get the system name and hostname'''
command = {"jsonrpc": "2.0",
"method": "XBMC.GetInfoLabels",
"params": {
"labels": ["System.FriendlyName"] },
"id": 1}
ret = call_api(server_params, command)
display_result(ret)
return ret['result']['System.FriendlyName']