/[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 3157 - (hide annotations) (download) (as text)
Tue Nov 29 18:53:13 2016 UTC (7 years, 5 months ago) by torben
File MIME type: text/x-python
File size: 14659 byte(s)
* correct print labels to [Todic]
* add onPause and onResume handlers



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

  ViewVC Help
Powered by ViewVC 1.1.20