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

  ViewVC Help
Powered by ViewVC 1.1.20