1 |
|
2 |
# This Python file uses the following encoding: utf-8 |
3 |
|
4 |
''' |
5 |
Todic plugin for XBMC |
6 |
Version 0.0.14 |
7 |
''' |
8 |
|
9 |
import sys |
10 |
import cgi as urlparse |
11 |
import os |
12 |
|
13 |
|
14 |
import xbmc |
15 |
import xbmcaddon |
16 |
import xbmcgui |
17 |
import xbmcplugin |
18 |
import urllib |
19 |
import urllib2 |
20 |
|
21 |
from xml.dom.minidom import parseString |
22 |
|
23 |
__addon__ = xbmcaddon.Addon(id='plugin.video.todic') |
24 |
__key__ = __addon__.getSetting('xbmckey').lower() |
25 |
__backend__ = "http://todic.dk/xbmc.php?xbmckey=" + __key__ |
26 |
fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg') |
27 |
datapath = xbmc.translatePath('special://profile/addon_data/plugin.video.todic/') |
28 |
|
29 |
ADDON_PATH = __addon__.getAddonInfo('path') |
30 |
SkinMasterPath = os.path.join(ADDON_PATH, 'skins' ) + '/' |
31 |
MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/' |
32 |
MySkin = 'main.xml' |
33 |
|
34 |
|
35 |
class TodicMovieDialog(xbmcgui.WindowXMLDialog): |
36 |
def __new__(cls): |
37 |
return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH) |
38 |
|
39 |
def __init__(self): |
40 |
super(TodicMovieDialog, self).__init__() |
41 |
|
42 |
def onClick( self, controlId ): |
43 |
print "OnClick: " + str(controlId) |
44 |
|
45 |
if (controlId == 50): |
46 |
self.close() |
47 |
play_real_video(self.url, self.name) |
48 |
|
49 |
if ( controlId == 98 ): |
50 |
self.close() |
51 |
|
52 |
|
53 |
def onInit(self): |
54 |
|
55 |
print "ONINIT" |
56 |
self.getControl( 1 ).setLabel( self.name); |
57 |
self.getControl( 2 ).setLabel( self.description ); |
58 |
|
59 |
def setUrl( self, url): |
60 |
self.url = url |
61 |
|
62 |
def setName( self, name ): |
63 |
self.name = name |
64 |
|
65 |
def setDescription( self, description ): |
66 |
self.description = description |
67 |
|
68 |
|
69 |
class TodicPlayer(xbmc.Player): |
70 |
def __init__(self, *args, **kwargs): |
71 |
#xbmc.Player.__init__(selv,*args,**kwargs) |
72 |
xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER ) |
73 |
self.stopped = False |
74 |
self.started = False |
75 |
print "[TodicPlayer] init" |
76 |
|
77 |
# @catchall |
78 |
def onPlayBackStarted(self): |
79 |
self.started = True |
80 |
print "[TodicPlayer] : started" |
81 |
# super.onPlayBackStarted() |
82 |
|
83 |
def onPlayBackStopped(self): |
84 |
self.stopped = True |
85 |
print "[TodicPlayer] : stopped" |
86 |
|
87 |
def onPlayBackEnded(self): |
88 |
self.stopped = True |
89 |
print "[TodicPlayer] : ended" |
90 |
|
91 |
def callbackLoop(self): |
92 |
print "[Todic] startLoop" |
93 |
while (self.stopped == False): |
94 |
if (self.started == True ): |
95 |
print "[todic] " + str(self.getTime()) |
96 |
xbmc.sleep(5000) |
97 |
|
98 |
|
99 |
def getText2(nodelist): |
100 |
rc = [] |
101 |
for node in nodelist: |
102 |
if node.nodeType == node.TEXT_NODE: |
103 |
rc.append(node.data) |
104 |
else: |
105 |
rc.append( getText(node.childNodes) ) |
106 |
return ''.join(rc) |
107 |
|
108 |
def getText(nodelist): |
109 |
if nodelist.length == 0: |
110 |
return '' |
111 |
else: |
112 |
if nodelist[0].childNodes.length == 0: |
113 |
return '' |
114 |
else: |
115 |
return nodelist[0].childNodes[0].nodeValue |
116 |
|
117 |
def SaveFile(path, data): |
118 |
file = open(path,'w') |
119 |
file.write(data) |
120 |
file.close() |
121 |
|
122 |
|
123 |
def open_url(url): |
124 |
req = urllib2.Request(url) |
125 |
content = urllib2.urlopen(req) |
126 |
data = content.read() |
127 |
content.close() |
128 |
return data |
129 |
|
130 |
def rootMenu(): |
131 |
|
132 |
msg = open_url(__backend__ + "&action=messages") |
133 |
msg = msg.strip() |
134 |
|
135 |
if msg != "": |
136 |
dialog = xbmcgui.Dialog() |
137 |
dialog.ok('XBMC Todic', msg) |
138 |
|
139 |
buildList(__backend__, "", False) # call default list |
140 |
|
141 |
# Adde xtra items to root menu |
142 |
listitem = xbmcgui.ListItem(label = "Søg film ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png') |
143 |
listitem.setProperty('Fanart_Image', fanartImage) |
144 |
|
145 |
u = sys.argv[0] + "?mode=10&name=" |
146 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True) |
147 |
|
148 |
#add search series |
149 |
listitem = xbmcgui.ListItem(label = "Søg Serier ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png') |
150 |
listitem.setProperty('Fanart_Image', fanartImage) |
151 |
|
152 |
u = sys.argv[0] + "?mode=11&name=" |
153 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True) |
154 |
|
155 |
xbmcplugin.endOfDirectory(int(sys.argv[1])) |
156 |
|
157 |
|
158 |
def buildList(url,title, endlist=True): |
159 |
print '[TODIC]:'+str(url) |
160 |
|
161 |
link = open_url(url) |
162 |
doc = parseString(link) |
163 |
ty = doc.getElementsByTagName("meta")[0].getAttribute("type") |
164 |
print '[TODIC]'+str(ty) |
165 |
|
166 |
if ty == 'clipList': |
167 |
mode = '50' |
168 |
folder = False |
169 |
else: |
170 |
mode = '1' |
171 |
folder = True |
172 |
|
173 |
|
174 |
entries = doc.getElementsByTagName("entry") |
175 |
l=len(entries) |
176 |
description = '' |
177 |
for entry in entries: |
178 |
name = getText( entry.getElementsByTagName("title") ) |
179 |
url = getText( entry.getElementsByTagName("url") ) |
180 |
thumb = getText( entry.getElementsByTagName("cover") ) |
181 |
description = getText( entry.getElementsByTagName("description") ) |
182 |
playcount = getText( entry.getElementsByTagName("playcount") ) |
183 |
|
184 |
if playcount == '': |
185 |
playcount = '0' |
186 |
playcount = int(playcount) |
187 |
|
188 |
|
189 |
## print "name:" + name |
190 |
# print "url:" + url |
191 |
# print "thumb:" + thumb |
192 |
# print "description:" + description |
193 |
|
194 |
|
195 |
listitem = xbmcgui.ListItem(label = name, label2='test', iconImage = 'DefaultFolder.png', thumbnailImage = thumb) |
196 |
listitem.setProperty('Fanart_Image', fanartImage) |
197 |
if mode == '50': |
198 |
infoLabels = {} |
199 |
infoLabels['title'] = name |
200 |
infoLabels['plot'] = description |
201 |
infoLabels['playcount'] = playcount |
202 |
listitem.setInfo('video', infoLabels) |
203 |
|
204 |
name = name.encode('UTF-8') |
205 |
description = description.encode('UTF-8') |
206 |
|
207 |
|
208 |
u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description) |
209 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = folder, totalItems = l) |
210 |
|
211 |
if (endlist == True): |
212 |
xbmcplugin.endOfDirectory(int(sys.argv[1])) |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
def play_video(url, name,description): |
218 |
if (description == None or description == ""): |
219 |
play_real_video(url,name) |
220 |
else: |
221 |
d = TodicMovieDialog() |
222 |
d.setUrl( url) |
223 |
d.setName( name ) |
224 |
d.setDescription( description ) |
225 |
|
226 |
d.doModal() |
227 |
|
228 |
|
229 |
def play_real_video(url, name): |
230 |
xml = open_url(url) |
231 |
print 'TODIC url: ' + str(url) |
232 |
print 'TODIC xml: '+ xml |
233 |
|
234 |
doc = parseString(xml) |
235 |
url = getText( doc.getElementsByTagName("url") ) |
236 |
|
237 |
subtitleurl = getText( doc.getElementsByTagName("subtitles") ) |
238 |
subtitlesfile = os.path.join(datapath,'temp.srt') |
239 |
|
240 |
#if old srt file exists delete it first |
241 |
if os.path.isfile(subtitlesfile): |
242 |
os.unlink(subtitlesfile) |
243 |
|
244 |
print '[TODIC] subs: '+str(subtitleurl) |
245 |
if len(subtitleurl) > 0: |
246 |
subtitles = open_url(subtitleurl) |
247 |
SaveFile(subtitlesfile, subtitles) |
248 |
print 'TODIC downloaded subtitles' |
249 |
|
250 |
|
251 |
|
252 |
image = xbmc.getInfoImage( 'ListItem.Thumb' ) |
253 |
listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = image) |
254 |
listitem.setInfo( type = "Video", infoLabels={ "Title": name } ) |
255 |
|
256 |
player = TodicPlayer(xbmc.PLAYER_CORE_AUTO) |
257 |
player.play(str(url), listitem) |
258 |
|
259 |
#kan ikke loade subtitles hvis foerend playeren koerer |
260 |
count = 0 |
261 |
while not xbmc.Player().isPlaying(): |
262 |
xbmc.sleep(500) |
263 |
count += 1 |
264 |
if count > 10: |
265 |
break |
266 |
|
267 |
|
268 |
if xbmc.Player().isPlaying(): |
269 |
if os.path.isfile(subtitlesfile): |
270 |
player.setSubtitles(subtitlesfile) |
271 |
print 'TODIC started subtitles' |
272 |
else: |
273 |
player.disableSubtitles() |
274 |
|
275 |
# player.callbackLoop() |
276 |
|
277 |
|
278 |
|
279 |
def search(): |
280 |
search = getUserInput("Todic Søgning") |
281 |
|
282 |
if (search != None and search != ""): |
283 |
url = __backend__ + "&action=search&search=" + urllib.quote_plus(search) |
284 |
|
285 |
#print "[TODIC] Search start: " + search |
286 |
#print "[TODIC] Search url: " + url |
287 |
|
288 |
buildList(url, "søgning") |
289 |
|
290 |
def searchSeries(): |
291 |
search = getUserInput("Todic Serie Søgning") |
292 |
|
293 |
if (search != None and search != ""): |
294 |
url = __backend__ + "&action=searchseries&search=" + urllib.quote_plus(search) |
295 |
|
296 |
#print "[TODIC] Search start: " + search |
297 |
#print "[TODIC] Search url: " + url |
298 |
|
299 |
buildList(url, "serie søgning") |
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
#=================================== Tool Box ======================================= |
306 |
# shows a more userfriendly notification |
307 |
def showMessage(heading, message): |
308 |
duration = 15*1000 |
309 |
xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) ) |
310 |
|
311 |
|
312 |
# raise a keyboard for user input |
313 |
def getUserInput(title = "Input", default="", hidden=False): |
314 |
result = None |
315 |
|
316 |
# Fix for when this functions is called with default=None |
317 |
if not default: |
318 |
default = "" |
319 |
|
320 |
keyboard = xbmc.Keyboard(default, title) |
321 |
keyboard.setHiddenInput(hidden) |
322 |
keyboard.doModal() |
323 |
|
324 |
if keyboard.isConfirmed(): |
325 |
result = keyboard.getText() |
326 |
|
327 |
return result |
328 |
|
329 |
|
330 |
def get_params(): |
331 |
param=[] |
332 |
paramstring=sys.argv[2] |
333 |
if len(paramstring)>=2: |
334 |
params=sys.argv[2] |
335 |
cleanedparams=params.replace('?','') |
336 |
if (params[len(params)-1]=='/'): |
337 |
params=params[0:len(params)-2] |
338 |
pairsofparams=cleanedparams.split('&') |
339 |
param={} |
340 |
for i in range(len(pairsofparams)): |
341 |
splitparams={} |
342 |
splitparams=pairsofparams[i].split('=') |
343 |
if (len(splitparams))==2: |
344 |
param[splitparams[0]]=splitparams[1] |
345 |
return param |
346 |
|
347 |
|
348 |
params = get_params() |
349 |
url = None |
350 |
name = None |
351 |
mode = None |
352 |
description = None |
353 |
|
354 |
try: |
355 |
url = urllib.unquote_plus(params["url"]) |
356 |
except: |
357 |
pass |
358 |
try: |
359 |
name = urllib.unquote_plus(params["name"]) |
360 |
except: |
361 |
pass |
362 |
try: |
363 |
mode = int(params["mode"]) |
364 |
except: |
365 |
pass |
366 |
try: |
367 |
description = urllib.unquote_plus(params["description"]) |
368 |
except: |
369 |
pass |
370 |
|
371 |
try: |
372 |
open_url("http://todic.dk") |
373 |
except: |
374 |
showMessage("Fejl", "Kunne ikke forbinde til todic.dk") |
375 |
exit() |
376 |
|
377 |
|
378 |
if url == 'refresh': |
379 |
#xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker ikke med XBMC12 |
380 |
xbmc.executebuiltin("Container.Refresh") |
381 |
|
382 |
|
383 |
elif mode == None: |
384 |
#build main menu |
385 |
rootMenu() |
386 |
|
387 |
elif mode == 1: |
388 |
#build list of movie starting letters |
389 |
buildList(url, name) |
390 |
|
391 |
elif mode == 10: |
392 |
search() |
393 |
|
394 |
elif mode == 11: |
395 |
searchSeries() |
396 |
|
397 |
|
398 |
elif mode == 50: |
399 |
play_video(url, name, description) |