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

  ViewVC Help
Powered by ViewVC 1.1.20