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

  ViewVC Help
Powered by ViewVC 1.1.20