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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3152 - (hide annotations) (download) (as text)
Fri Nov 25 16:17:45 2016 UTC (7 years, 6 months ago) by torben
File MIME type: text/x-python
File size: 14295 byte(s)
implement series resume play
1 torben 1678
2 torben 1677 # This Python file uses the following encoding: utf-8
3    
4 torben 1629 '''
5     Todic plugin for XBMC
6 torben 3152 Version 0.1.0
7 torben 1629 '''
8    
9     import sys
10 torben 1648 import os
11 torben 1629
12 torben 1648
13 torben 1629 import xbmc
14     import xbmcaddon
15     import xbmcgui
16     import xbmcplugin
17     import urllib
18 torben 1678 import urllib2
19 torben 1629
20 torben 2601 # import pprint
21 torben 2595
22 torben 1678 from xml.dom.minidom import parseString
23 torben 3142 from time import time
24 torben 1678
25 torben 1629 __addon__ = xbmcaddon.Addon(id='plugin.video.todic')
26     __key__ = __addon__.getSetting('xbmckey').lower()
27 torben 3037 __backend__ = "https://todic.dk/xbmc.php?xbmckey=" + __key__
28 torben 2594 fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg')
29 torben 2601 datapath = xbmc.translatePath(
30     'special://profile/addon_data/plugin.video.todic/')
31 torben 1629
32 torben 2592 ADDON_PATH = __addon__.getAddonInfo('path')
33 torben 2601 SkinMasterPath = os.path.join(ADDON_PATH, 'skins') + '/'
34 torben 2592 MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/'
35     MySkin = 'main.xml'
36 torben 1676
37 torben 2592
38     class TodicMovieDialog(xbmcgui.WindowXMLDialog):
39    
40 torben 2601 def __new__(cls):
41     return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH)
42 torben 2592
43 torben 2601 def __init__(self):
44     super(TodicMovieDialog, self).__init__()
45 torben 3147 self.position = 0
46 torben 2592
47 torben 2601 def onClick(self, controlId):
48 torben 3147 print "[Todic] MovieDialog OnClick: " + str(controlId)
49 torben 2592
50 torben 2601 if (controlId == 50):
51     self.close()
52 torben 3147 play_real_video(self.url, self.name, 0)
53 torben 2592
54 torben 3147 if (controlId == 51):
55     self.close()
56     play_real_video(self.url, self.name, self.position)
57    
58 torben 2601 if (controlId == 98):
59     self.close()
60 torben 2592
61 torben 2601 def onInit(self):
62 torben 2592
63 torben 3152 print "[Todic] MovieDialog onInit"
64 torben 2601 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 torben 2596
70 torben 3147 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 torben 2596
79 torben 3147 #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 torben 3152 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 torben 2592
91 torben 3147
92 torben 2601 def setName(self, name):
93     self.name = name
94 torben 2595
95 torben 2601 def setDescription(self, description):
96     self.description = description
97 torben 2595
98 torben 2596
99 torben 2601 class TodicPlayer(xbmc.Player):
100 torben 2596
101 torben 2601 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 torben 3142 self.playingPosition = 0.0
107     self.lastReport = 0
108 torben 2601 print "[TodicPlayer] init"
109 torben 2592
110 torben 2601 def onPlayBackStarted(self):
111     self.started = True
112     print "[TodicPlayer] : started"
113 torben 2592
114 torben 3142 #When user presses stop, we report back the the position registered in the last call to self.tick()
115 torben 2601 def onPlayBackStopped(self):
116     self.stopped = True
117     print "[TodicPlayer] : stopped"
118 torben 3149 self.reportPlaytime("stopped")
119 torben 2592
120 torben 3142
121 torben 3152
122 torben 2601 def onPlayBackEnded(self):
123     self.stopped = True
124     print "[TodicPlayer] : ended"
125 torben 3149 self.reportPlaytime("ended")
126 torben 1676
127 torben 3142 def tick(self):
128 torben 3144 if ( self.isPlaying() ):
129 torben 3142 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 torben 3147 self.lastReport = now
134 torben 3149 self.reportPlaytime("playing")
135 torben 3147
136 torben 3149 def reportPlaytime(self, subaction):
137 torben 3151 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 torben 3142
143    
144    
145 torben 2601 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 torben 1676
154    
155 torben 1678 def getText(nodelist):
156 torben 2601 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 torben 1676
164 torben 2601
165 torben 3143
166 torben 1914 def SaveFile(path, data):
167 torben 2601 file = open(path, 'w')
168     file.write(data)
169     file.close()
170 torben 1678
171 torben 1914
172 torben 3143
173 torben 1629 def open_url(url):
174 torben 2601 req = urllib2.Request(url)
175     content = urllib2.urlopen(req)
176     data = content.read()
177     content.close()
178     return data
179 torben 1629
180 torben 2601
181 torben 3143 # 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 torben 3147 print "[Todic ]Some error during open_url call to ", url
188 torben 3143
189    
190    
191 torben 1629 def rootMenu():
192 torben 1648
193 torben 2601 msg = open_url(__backend__ + "&action=messages")
194     msg = msg.strip()
195 torben 1799
196 torben 2601 if msg != "":
197     dialog = xbmcgui.Dialog()
198     dialog.ok('XBMC Todic', msg)
199 torben 1799
200 torben 2601 buildList(__backend__, "", False) # call default list
201 torben 1631
202 torben 2601 # 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 torben 1648
207 torben 2601 u = sys.argv[0] + "?mode=10&name="
208 torben 2602 xbmcplugin.addDirectoryItem(
209 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
210 torben 1631
211 torben 2601 # 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 torben 2097
216 torben 2601 u = sys.argv[0] + "?mode=11&name="
217 torben 2602 xbmcplugin.addDirectoryItem(
218 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
219 torben 2097
220 torben 2601 xbmcplugin.endOfDirectory(int(sys.argv[1]))
221 torben 1629
222    
223 torben 2601 def buildList(url, title, endlist=True):
224     print '[TODIC]:' + str(url)
225 torben 1678
226 torben 2601 link = open_url(url)
227     doc = parseString(link)
228     ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
229     print '[TODIC]' + str(ty)
230 torben 1646
231 torben 2601 if ty == 'clipList':
232     mode = '50'
233     folder = False
234     else:
235     mode = '1'
236     folder = True
237 torben 1646
238 torben 2601 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 torben 1829
248 torben 3148
249 torben 2601 if playcount == '':
250     playcount = '0'
251     playcount = int(playcount)
252 torben 1647
253 torben 2601 # print "name:" + name
254 torben 1678 # print "url:" + url
255     # print "thumb:" + thumb
256     # print "description:" + description
257 torben 2601 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 torben 1678
267 torben 2601 name = name.encode('UTF-8')
268     description = description.encode('UTF-8')
269 torben 1678
270 torben 2601 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
271 torben 3149 name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
272 torben 2602 xbmcplugin.addDirectoryItem(
273 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
274 torben 1648
275 torben 2601 if (endlist == True):
276     xbmcplugin.endOfDirectory(int(sys.argv[1]))
277 torben 1923
278    
279 torben 2601 def play_video(url, name, description):
280 torben 3152 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 torben 2601 if (description == None or description == ""):
294 torben 3152 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 torben 2601 else:
304     d = TodicMovieDialog()
305 torben 3152 d.setDetailsDoc(clipDetailsDoc)
306 torben 2601 d.setName(name)
307     d.setDescription(description)
308 torben 1629
309 torben 2601 d.doModal()
310 torben 1629
311    
312 torben 3147 def play_real_video(url, name, position):
313 torben 2601 xml = open_url(url)
314 torben 3147 print '[Todic] url: ' + str(url)
315     print '[Todic] xml: ' + xml
316     print '[Todic] pos: ' + str(position)
317 torben 1678
318 torben 2601 doc = parseString(xml)
319     url = getText(doc.getElementsByTagName("url"))
320 torben 1678
321 torben 2601 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
322     subtitlesfile = os.path.join(datapath, 'temp.srt')
323 torben 1914
324 torben 2601 # if old srt file exists delete it first
325     if os.path.isfile(subtitlesfile):
326     os.unlink(subtitlesfile)
327 torben 1917
328 torben 2601 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 torben 1914
334 torben 2601 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 torben 3147 listitem.setProperty('ResumeTime', '300')
339     listitem.setProperty('TotalTime', '3000')
340 torben 1914
341 torben 2601 player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
342     player.play(str(url), listitem)
343 torben 1914
344 torben 2601 # kan ikke loade subtitles hvis foerend playeren koerer
345     count = 0
346     while not xbmc.Player().isPlaying():
347 torben 3146 xbmc.sleep(250)
348 torben 2601 count += 1
349     if count > 10:
350     break
351 torben 1629
352 torben 3147
353    
354 torben 2601 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 torben 1914
361 torben 3142
362 torben 3147 if (position > 0):
363     while (player.getTotalTime() == 0.0): #Vent indtil vi har beregnet hvor langt klippet er
364     xbmc.sleep(250)
365 torben 3142
366 torben 3147 print "[Todic] totalTime " + str( player.getTotalTime() )
367     player.seekTime(position)
368 torben 3142
369 torben 1676
370 torben 3147 #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 torben 1676
375 torben 3147
376    
377 torben 1631 def search():
378 torben 2601 search = getUserInput("Todic Søgning")
379 torben 1629
380 torben 2601 if (search != None and search != ""):
381     url = __backend__ + "&action=search&search=" + \
382     urllib.quote_plus(search)
383 torben 1631
384 torben 2601 # print "[TODIC] Search start: " + search
385     # print "[TODIC] Search url: " + url
386 torben 1631
387 torben 2601 buildList(url, "søgning")
388 torben 1631
389 torben 2601
390 torben 2097 def searchSeries():
391 torben 2601 search = getUserInput("Todic Serie Søgning")
392 torben 2097
393 torben 2601 if (search != None and search != ""):
394     url = __backend__ + "&action=searchseries&search=" + \
395     urllib.quote_plus(search)
396 torben 2097
397 torben 2601 # print "[TODIC] Search start: " + search
398     # print "[TODIC] Search url: " + url
399 torben 2097
400 torben 2601 buildList(url, "serie søgning")
401 torben 2097
402 torben 1631
403 torben 2601 #=================================== Tool Box =======================================
404 torben 1631 # shows a more userfriendly notification
405     def showMessage(heading, message):
406 torben 2601 duration = 15 * 1000
407     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
408     (heading, message, duration))
409 torben 1631
410    
411     # raise a keyboard for user input
412 torben 2601 def getUserInput(title="Input", default="", hidden=False):
413     result = None
414 torben 1631
415 torben 2601 # Fix for when this functions is called with default=None
416     if not default:
417     default = ""
418 torben 1631
419 torben 2601 keyboard = xbmc.Keyboard(default, title)
420     keyboard.setHiddenInput(hidden)
421     keyboard.doModal()
422 torben 1631
423 torben 2601 if keyboard.isConfirmed():
424     result = keyboard.getText()
425    
426     return result
427    
428    
429 torben 1629 def get_params():
430 torben 2601 return parse_parameter_string(sys.argv[2])
431 torben 2595
432 torben 1629
433 torben 2601 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 torben 1629
449 torben 2601
450 torben 1629 params = get_params()
451     url = None
452     name = None
453     mode = None
454 torben 2592 description = None
455 torben 1629
456 torben 3149
457 torben 3148 #print params
458    
459 torben 1629 try:
460 torben 2601 url = urllib.unquote_plus(params["url"])
461 torben 1629 except:
462 torben 2601 pass
463 torben 1629 try:
464 torben 2601 name = urllib.unquote_plus(params["name"])
465 torben 1629 except:
466 torben 2601 pass
467 torben 1629 try:
468 torben 2601 mode = int(params["mode"])
469 torben 1629 except:
470 torben 2601 pass
471 torben 2592 try:
472 torben 2601 description = urllib.unquote_plus(params["description"])
473 torben 2592 except:
474 torben 2601 pass
475 torben 1629
476 torben 3148
477    
478 torben 3147
479     try:
480 torben 2601 open_url("http://todic.dk")
481 torben 2440 except:
482 torben 2601 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
483     exit()
484 torben 2440
485 torben 2601
486 torben 1814 if url == 'refresh':
487 torben 2601 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
488     # ikke med XBMC12
489     xbmc.executebuiltin("Container.Refresh")
490 torben 1629
491 torben 2601
492 torben 1814 elif mode == None:
493 torben 2601 # build main menu
494     rootMenu()
495    
496 torben 1629 elif mode == 1:
497 torben 2601 # build list of movie starting letters
498     buildList(url, name)
499 torben 1629
500 torben 1631 elif mode == 10:
501 torben 2601 search()
502 torben 2097
503     elif mode == 11:
504 torben 2601 searchSeries()
505 torben 1631
506 torben 2601
507 torben 1629 elif mode == 50:
508 torben 2601 play_video(url, name, description)

  ViewVC Help
Powered by ViewVC 1.1.20