-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
303 lines (248 loc) · 9.94 KB
/
utilities.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
298
299
300
301
302
303
# -*- coding: utf-8 -*-
#
import os, sys
import xbmc,xbmcaddon,xbmcgui
import time, socket
try: import simplejson as json
except ImportError: import json
import urllib, re
try:
# Python 3.0 +
import http.client as httplib
except ImportError:
# Python 2.7 and earlier
import httplib
try:
# Python 2.6 +
from hashlib import sha as sha
except ImportError:
# Python 2.5 and earlier
import sha
__author__ = "Ross Glass"
__credits__ = ["Ross Glass"]
__license__ = "GPL"
__maintainer__ = "Ross Glass"
__email__ = "fade@entropism.org"
__status__ = "Production"
__settings__ = xbmcaddon.Addon( "script.HATrigger" )
__language__ = __settings__.getLocalizedString
host = __settings__.getSetting("host")
port = sha.new(__settings__.getSetting("port")).hexdigest()
debug = __settings__.getSetting( "debug" )
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
def Debug(msg, force=False):
if (debug == 'true' or force):
try:
print "HA Trigger: " + msg
except UnicodeEncodeError:
print "HA Trigger: " + msg.encode( "utf-8", "ignore" )
def notification( header, message, time=5000, icon=__settings__.getAddonInfo( "icon" ) ):
xbmc.executebuiltin( "XBMC.Notification(%s,%s,%i,%s)" % ( header, message, time, icon ) )
def checkSettings(daemon=False):
if host == "":
if daemon:
notification("HA Trigger", __language__(1106).encode( "utf-8", "ignore" )) # please enter the host
else:
xbmcgui.Dialog().ok("HA Trigger", __language__(1106).encode( "utf-8", "ignore" )) # please enter the host
__settings__.openSettings()
return False
elif __settings__.getSetting("host") == "":
if daemon:
notification("port", __language__(1107).encode( "utf-8", "ignore" )) # please enter the port
else:
xbmcgui.Dialog().ok("HA Trigger", __language__(1107).encode( "utf-8", "ignore" )) # please enter the port
__settings__.openSettings()
return False
return True
# SQL string quote escaper
def xcp(s):
return re.sub('''(['])''', r"''", str(s))
# make a httpapi based XBMC db query (get data)
def xbmcHttpapiQuery(query):
Debug("[httpapi-sql] query: "+query)
xml_data = xbmc.executehttpapi( "QueryVideoDatabase(%s)" % urllib.quote_plus(query), )
match = re.findall( "<field>((?:[^<]|<(?!/))*)</field>", xml_data,)
Debug("[httpapi-sql] responce: "+xml_data)
Debug("[httpapi-sql] matches: "+str(match))
return match
# execute a httpapi based XBMC db query (set data)
def xbmcHttpapiExec(query):
xml_data = xbmc.executehttpapi( "ExecVideoDatabase(%s)" % urllib.quote_plus(query), )
return xml_data
# get tvshows from XBMC
def getTVShowsFromXBMC():
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows','params':{'properties': ['title', 'year', 'imdbnumber', 'playcount']}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getTVShowsFromXBMC: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']
except KeyError:
Debug("getTVShowsFromXBMC: KeyError: result['result']")
return None
# get seasons for a given tvshow from XBMC
def getSeasonsFromXBMC(tvshow):
Debug("getSeasonsFromXBMC: "+str(tvshow))
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetSeasons','params':{'tvshowid': tvshow['tvshowid']}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getSeasonsFromXBMC: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']
except KeyError:
Debug("getSeasonsFromXBMC: KeyError: result['result']")
return None
# get episodes for a given tvshow / season from XBMC
def getEpisodesFromXBMC(tvshow, season):
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes','params':{'tvshowid': tvshow['tvshowid'], 'season': season, 'properties': ['playcount', 'episode']}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getEpisodesFromXBMC: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']
except KeyError:
Debug("getEpisodesFromXBMC: KeyError: result['result']")
return None
# get a single episode from xbmc given the id
def getEpisodeDetailsFromXbmc(libraryId, fields):
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodeDetails','params':{'episodeid': libraryId, 'properties': fields}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getEpisodeDetailsFromXbmc: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']['episodedetails']
except KeyError:
Debug("getEpisodeDetailsFromXbmc: KeyError: result['result']['episodedetails']")
return None
# get movies from XBMC
def getMoviesFromXBMC():
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovies','params':{'properties': ['title', 'year', 'originaltitle', 'imdbnumber', 'playcount', 'lastplayed']}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getMoviesFromXBMC: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']['movies']
Debug("getMoviesFromXBMC: KeyError: result['result']['movies']")
except KeyError:
return None
# get a single movie from xbmc given the id
def getMovieDetailsFromXbmc(libraryId, fields):
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'VideoLibrary.GetMovieDetails','params':{'movieid': libraryId, 'properties': fields}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("getMovieDetailsFromXbmc: " + str(error))
return None
except KeyError:
pass # no error
try:
return result['result']['moviedetails']
except KeyError:
Debug("getMovieDetailsFromXbmc: KeyError: result['result']['moviedetails']")
return None
# get current video being played from XBMC
def getCurrentPlayingVideoFromXBMC():
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'Player.GetActivePlayers','params':{}, 'id': 1})
result = xbmc.executeJSONRPC(rpccmd)
result = json.loads(result)
# check for error
try:
error = result['error']
Debug("[Util] getCurrentPlayingVideoFromXBMC: " + str(error))
return None
except KeyError:
pass # no error
try:
for player in result['result']:
if player['type'] == 'video':
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'Player.GetProperties','params':{'playerid': player['playerid'], 'properties':['playlistid', 'position']}, 'id': 1})
result2 = xbmc.executeJSONRPC(rpccmd)
result2 = json.loads(result2)
# check for error
try:
error = result2['error']
Debug("[Util] getCurrentPlayingVideoFromXBMC, Player.GetProperties: " + str(error))
return None
except KeyError:
pass # no error
playlistid = result2['result']['playlistid']
position = result2['result']['position']
rpccmd = json.dumps({'jsonrpc': '2.0', 'method': 'Playlist.GetItems','params':{'playlistid': playlistid}, 'id': 1})
result2 = xbmc.executeJSONRPC(rpccmd)
result2 = json.loads(result2)
# check for error
try:
error = result2['error']
Debug("[Util] getCurrentPlayingVideoFromXBMC, Playlist.GetItems: " + str(error))
return None
except KeyError:
pass # no error
Debug("Current playlist: "+str(result2['result']))
return result2['result'][position]
Debug("[Util] getCurrentPlayingVideoFromXBMC: No current video player")
return None
except KeyError:
Debug("[Util] getCurrentPlayingVideoFromXBMC: KeyError")
return None
def getMovieIdFromXBMC(imdb_id, title):
# httpapi till jsonrpc supports searching for a single movie
# Get id of movie by movies IMDB
Debug("Searching for movie: "+imdb_id+", "+title)
match = xbmcHttpapiQuery(
" SELECT idMovie FROM movie"+
" WHERE c09='%(imdb_id)s'" % {'imdb_id':imdb_id}+
" UNION"+
" SELECT idMovie FROM movie"+
" WHERE upper(c00)='%(title)s'" % {'title':xcp(title.upper())}+
" LIMIT 1")
if not match:
Debug("getMovieIdFromXBMC: cannot find movie in database")
return -1
return match[0]
def getShowIdFromXBMC(tvdb_id, title):
# httpapi till jsonrpc supports searching for a single show
# Get id of show by shows tvdb id
Debug("Searching for show: "+str(tvdb_id)+", "+title)
match = xbmcHttpapiQuery(
" SELECT idShow FROM tvshow"+
" WHERE c12='%(tvdb_id)s'" % {'tvdb_id':xcp(tvdb_id)}+
" UNION"+
" SELECT idShow FROM tvshow"+
" WHERE upper(c00)='%(title)s'" % {'title':xcp(title.upper())}+
" LIMIT 1")
if not match:
Debug("getShowIdFromXBMC: cannot find movie in database")
return -1
return match[0]