/[projects]/misc/xbmc/plugin.video.todic/default.py
ViewVC logotype

Contents of /misc/xbmc/plugin.video.todic/default.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3152 - (show annotations) (download) (as text)
Fri Nov 25 16:17:45 2016 UTC (7 years, 5 months ago) by torben
File MIME type: text/x-python
File size: 14295 byte(s)
implement series resume play
1
2 # This Python file uses the following encoding: utf-8
3
4 '''
5 Todic plugin for XBMC
6 Version 0.1.0
7 '''
8
9 import sys
10 import os
11
12
13 import xbmc
14 import xbmcaddon
15 import xbmcgui
16 import xbmcplugin
17 import urllib
18 import urllib2
19
20 # import pprint
21
22 from xml.dom.minidom import parseString
23 from time import time
24
25 __addon__ = xbmcaddon.Addon(id='plugin.video.todic')
26 __key__ = __addon__.getSetting('xbmckey').lower()
27 __backend__ = "https://todic.dk/xbmc.php?xbmckey=" + __key__
28 fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg')
29 datapath = xbmc.translatePath(
30 'special://profile/addon_data/plugin.video.todic/')
31
32 ADDON_PATH = __addon__.getAddonInfo('path')
33 SkinMasterPath = os.path.join(ADDON_PATH, 'skins') + '/'
34 MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/'
35 MySkin = 'main.xml'
36
37
38 class TodicMovieDialog(xbmcgui.WindowXMLDialog):
39
40 def __new__(cls):
41 return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH)
42
43 def __init__(self):
44 super(TodicMovieDialog, self).__init__()
45 self.position = 0
46
47 def onClick(self, controlId):
48 print "[Todic] MovieDialog OnClick: " + str(controlId)
49
50 if (controlId == 50):
51 self.close()
52 play_real_video(self.url, self.name, 0)
53
54 if (controlId == 51):
55 self.close()
56 play_real_video(self.url, self.name, self.position)
57
58 if (controlId == 98):
59 self.close()
60
61 def onInit(self):
62
63 print "[Todic] MovieDialog onInit"
64 self.getControl(1).setLabel(self.name)
65 self.getControl(2).setLabel(self.moviegroups)
66 self.getControl(3).setLabel(self.description)
67 self.getControl(10).setLabel(self.playlength)
68 self.getControl(11).setLabel(self.codecdetails)
69
70 if (self.position > 0):
71 self.getControl(51).setVisible(True)
72 self.getControl(50).setPosition(100, 570)
73 self.getControl(51).setPosition(450, 570)
74 self.getControl(50).controlLeft( self.getControl(51) )
75 self.getControl(50).controlRight( self.getControl(51) )
76 else:
77 self.getControl(51).setVisible(False)
78
79 #orig_img_width = self.getControl(40).getWidth()
80 #self.starwidth = (float(self.imdbrating) / 10.0) * orig_img_width
81 #self.getControl(40).setWidth(int(self.starwidth))
82
83 def setDetailsDoc(self, detailsDoc):
84 print "[Todic] MovieDialog setDetailsDoc:"
85 self.imdbrating = getText(detailsDoc.getElementsByTagName("imdbrating"))
86 self.moviegroups = getText(detailsDoc.getElementsByTagName("moviegroups"))
87 self.playlength = getText(detailsDoc.getElementsByTagName("playlength"))
88 self.codecdetails = getText(detailsDoc.getElementsByTagName("codecdetails"))
89 self.position = int( getText(detailsDoc.getElementsByTagName("position")) )
90
91
92 def setName(self, name):
93 self.name = name
94
95 def setDescription(self, description):
96 self.description = description
97
98
99 class TodicPlayer(xbmc.Player):
100
101 def __init__(self, *args, **kwargs):
102 # xbmc.Player.__init__(selv,*args,**kwargs)
103 xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER)
104 self.stopped = False
105 self.started = False
106 self.playingPosition = 0.0
107 self.lastReport = 0
108 print "[TodicPlayer] init"
109
110 def onPlayBackStarted(self):
111 self.started = True
112 print "[TodicPlayer] : started"
113
114 #When user presses stop, we report back the the position registered in the last call to self.tick()
115 def onPlayBackStopped(self):
116 self.stopped = True
117 print "[TodicPlayer] : stopped"
118 self.reportPlaytime("stopped")
119
120
121
122 def onPlayBackEnded(self):
123 self.stopped = True
124 print "[TodicPlayer] : ended"
125 self.reportPlaytime("ended")
126
127 def tick(self):
128 if ( self.isPlaying() ):
129 self.playingPosition = self.getTime()
130 now = time()
131 #print "[Todic] tick " + str(now) + " " + str(self.lastReport) + " : " +str(now - self.lastReport)
132 if ( (now - self.lastReport) > 60.0):
133 self.lastReport = now
134 self.reportPlaytime("playing")
135
136 def reportPlaytime(self, subaction):
137 if (self.playingPosition > 60):
138 url = __backend__ + "&action=playbacktime&subaction=" + subaction + "&time=" + str( self.playingPosition )
139 print "[Todic] reportPlaytime:" + url
140 open_url_safe(url)
141
142
143
144
145 def getText2(nodelist):
146 rc = []
147 for node in nodelist:
148 if node.nodeType == node.TEXT_NODE:
149 rc.append(node.data)
150 else:
151 rc.append(getText(node.childNodes))
152 return ''.join(rc)
153
154
155 def getText(nodelist):
156 if nodelist.length == 0:
157 return ''
158 else:
159 if nodelist[0].childNodes.length == 0:
160 return ''
161 else:
162 return nodelist[0].childNodes[0].nodeValue
163
164
165
166 def SaveFile(path, data):
167 file = open(path, 'w')
168 file.write(data)
169 file.close()
170
171
172
173 def open_url(url):
174 req = urllib2.Request(url)
175 content = urllib2.urlopen(req)
176 data = content.read()
177 content.close()
178 return data
179
180
181 # wraps open url in a catch-all exception handler
182 # usefull for periodic back-reporting that should not interrupt the program flow
183 def open_url_safe(url):
184 try:
185 return open_url(url)
186 except:
187 print "[Todic ]Some error during open_url call to ", url
188
189
190
191 def rootMenu():
192
193 msg = open_url(__backend__ + "&action=messages")
194 msg = msg.strip()
195
196 if msg != "":
197 dialog = xbmcgui.Dialog()
198 dialog.ok('XBMC Todic', msg)
199
200 buildList(__backend__, "", False) # call default list
201
202 # Adde xtra items to root menu
203 listitem = xbmcgui.ListItem(
204 label="Søg film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
205 listitem.setProperty('Fanart_Image', fanartImage)
206
207 u = sys.argv[0] + "?mode=10&name="
208 xbmcplugin.addDirectoryItem(
209 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
210
211 # add search series
212 listitem = xbmcgui.ListItem(
213 label="Søg Serier ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
214 listitem.setProperty('Fanart_Image', fanartImage)
215
216 u = sys.argv[0] + "?mode=11&name="
217 xbmcplugin.addDirectoryItem(
218 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
219
220 xbmcplugin.endOfDirectory(int(sys.argv[1]))
221
222
223 def buildList(url, title, endlist=True):
224 print '[TODIC]:' + str(url)
225
226 link = open_url(url)
227 doc = parseString(link)
228 ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
229 print '[TODIC]' + str(ty)
230
231 if ty == 'clipList':
232 mode = '50'
233 folder = False
234 else:
235 mode = '1'
236 folder = True
237
238 entries = doc.getElementsByTagName("entry")
239 l = len(entries)
240 description = ''
241 for entry in entries:
242 name = getText(entry.getElementsByTagName("title"))
243 url = getText(entry.getElementsByTagName("url"))
244 thumb = getText(entry.getElementsByTagName("cover"))
245 description = getText(entry.getElementsByTagName("description"))
246 playcount = getText(entry.getElementsByTagName("playcount"))
247
248
249 if playcount == '':
250 playcount = '0'
251 playcount = int(playcount)
252
253 # print "name:" + name
254 # print "url:" + url
255 # print "thumb:" + thumb
256 # print "description:" + description
257 listitem = xbmcgui.ListItem(
258 label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
259 listitem.setProperty('Fanart_Image', fanartImage)
260 if mode == '50':
261 infoLabels = {}
262 infoLabels['title'] = name
263 infoLabels['plot'] = description
264 infoLabels['playcount'] = playcount
265 listitem.setInfo('video', infoLabels)
266
267 name = name.encode('UTF-8')
268 description = description.encode('UTF-8')
269
270 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
271 name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
272 xbmcplugin.addDirectoryItem(
273 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
274
275 if (endlist == True):
276 xbmcplugin.endOfDirectory(int(sys.argv[1]))
277
278
279 def play_video(url, name, description):
280 param1 = parse_parameter_string(url)
281 clipkey = param1["clipkey"]
282
283 print "[Todic] ClipKey:" + clipkey
284 detailurl = __backend__ + "&action=clipdetails&clipkey=" + clipkey
285 print "[Todic] detailURL = " + detailurl
286
287 xml = open_url(detailurl)
288
289 clipDetailsDoc = parseString(xml)
290 savedPosition = int( getText(clipDetailsDoc.getElementsByTagName("position")) )
291 playPosition = 0
292
293 if (description == None or description == ""):
294 if (savedPosition > 0):
295 dialog = xbmcgui.Dialog()
296 #yes / true -afspil fra position
297 answer = dialog.yesno(heading='Todic', line1='Afspil fra gemt', nolabel='Fra start', yeslabel='Fortsæt')
298 if (answer == True):
299 playPosition = savedPosition
300
301 play_real_video(url, name, playPosition)
302
303 else:
304 d = TodicMovieDialog()
305 d.setDetailsDoc(clipDetailsDoc)
306 d.setName(name)
307 d.setDescription(description)
308
309 d.doModal()
310
311
312 def play_real_video(url, name, position):
313 xml = open_url(url)
314 print '[Todic] url: ' + str(url)
315 print '[Todic] xml: ' + xml
316 print '[Todic] pos: ' + str(position)
317
318 doc = parseString(xml)
319 url = getText(doc.getElementsByTagName("url"))
320
321 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
322 subtitlesfile = os.path.join(datapath, 'temp.srt')
323
324 # if old srt file exists delete it first
325 if os.path.isfile(subtitlesfile):
326 os.unlink(subtitlesfile)
327
328 print '[TODIC] subs: ' + str(subtitleurl)
329 if len(subtitleurl) > 0:
330 subtitles = open_url(subtitleurl)
331 SaveFile(subtitlesfile, subtitles)
332 print 'TODIC downloaded subtitles'
333
334 image = xbmc.getInfoImage('ListItem.Thumb')
335 listitem = xbmcgui.ListItem(
336 label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
337 listitem.setInfo(type="Video", infoLabels={"Title": name})
338 listitem.setProperty('ResumeTime', '300')
339 listitem.setProperty('TotalTime', '3000')
340
341 player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
342 player.play(str(url), listitem)
343
344 # kan ikke loade subtitles hvis foerend playeren koerer
345 count = 0
346 while not xbmc.Player().isPlaying():
347 xbmc.sleep(250)
348 count += 1
349 if count > 10:
350 break
351
352
353
354 if xbmc.Player().isPlaying():
355 if os.path.isfile(subtitlesfile):
356 player.setSubtitles(subtitlesfile)
357 print 'TODIC started subtitles'
358 else:
359 player.disableSubtitles()
360
361
362 if (position > 0):
363 while (player.getTotalTime() == 0.0): #Vent indtil vi har beregnet hvor langt klippet er
364 xbmc.sleep(250)
365
366 print "[Todic] totalTime " + str( player.getTotalTime() )
367 player.seekTime(position)
368
369
370 #Holder python kørernde indtil at det bliver bedt om at stoppe
371 while (not xbmc.abortRequested):
372 player.tick()
373 xbmc.sleep(500)
374
375
376
377 def search():
378 search = getUserInput("Todic Søgning")
379
380 if (search != None and search != ""):
381 url = __backend__ + "&action=search&search=" + \
382 urllib.quote_plus(search)
383
384 # print "[TODIC] Search start: " + search
385 # print "[TODIC] Search url: " + url
386
387 buildList(url, "søgning")
388
389
390 def searchSeries():
391 search = getUserInput("Todic Serie Søgning")
392
393 if (search != None and search != ""):
394 url = __backend__ + "&action=searchseries&search=" + \
395 urllib.quote_plus(search)
396
397 # print "[TODIC] Search start: " + search
398 # print "[TODIC] Search url: " + url
399
400 buildList(url, "serie søgning")
401
402
403 #=================================== Tool Box =======================================
404 # shows a more userfriendly notification
405 def showMessage(heading, message):
406 duration = 15 * 1000
407 xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
408 (heading, message, duration))
409
410
411 # raise a keyboard for user input
412 def getUserInput(title="Input", default="", hidden=False):
413 result = None
414
415 # Fix for when this functions is called with default=None
416 if not default:
417 default = ""
418
419 keyboard = xbmc.Keyboard(default, title)
420 keyboard.setHiddenInput(hidden)
421 keyboard.doModal()
422
423 if keyboard.isConfirmed():
424 result = keyboard.getText()
425
426 return result
427
428
429 def get_params():
430 return parse_parameter_string(sys.argv[2])
431
432
433 def parse_parameter_string(paramstring):
434 param = []
435 if len(paramstring) >= 2:
436 params = paramstring
437 cleanedparams = params.replace('?', '')
438 if (params[len(params) - 1] == '/'):
439 params = params[0:len(params) - 2]
440 pairsofparams = cleanedparams.split('&')
441 param = {}
442 for i in range(len(pairsofparams)):
443 splitparams = {}
444 splitparams = pairsofparams[i].split('=')
445 if (len(splitparams)) == 2:
446 param[splitparams[0]] = splitparams[1]
447 return param
448
449
450 params = get_params()
451 url = None
452 name = None
453 mode = None
454 description = None
455
456
457 #print params
458
459 try:
460 url = urllib.unquote_plus(params["url"])
461 except:
462 pass
463 try:
464 name = urllib.unquote_plus(params["name"])
465 except:
466 pass
467 try:
468 mode = int(params["mode"])
469 except:
470 pass
471 try:
472 description = urllib.unquote_plus(params["description"])
473 except:
474 pass
475
476
477
478
479 try:
480 open_url("http://todic.dk")
481 except:
482 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
483 exit()
484
485
486 if url == 'refresh':
487 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
488 # ikke med XBMC12
489 xbmc.executebuiltin("Container.Refresh")
490
491
492 elif mode == None:
493 # build main menu
494 rootMenu()
495
496 elif mode == 1:
497 # build list of movie starting letters
498 buildList(url, name)
499
500 elif mode == 10:
501 search()
502
503 elif mode == 11:
504 searchSeries()
505
506
507 elif mode == 50:
508 play_video(url, name, description)

  ViewVC Help
Powered by ViewVC 1.1.20