/[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 3145 - (hide annotations) (download) (as text)
Wed Nov 23 09:36:57 2016 UTC (7 years, 6 months ago) by torben
File MIME type: text/x-python
File size: 12859 byte(s)
bump version to 0.0.18
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 3145 Version 0.0.18
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 2592
46 torben 2601 def onClick(self, controlId):
47     print "OnClick: " + str(controlId)
48 torben 2592
49 torben 2601 if (controlId == 50):
50     self.close()
51     play_real_video(self.url, self.name)
52 torben 2592
53 torben 2601 if (controlId == 98):
54     self.close()
55 torben 2592
56 torben 2601 def onInit(self):
57 torben 2592
58 torben 2601 print "ONINIT"
59     self.getControl(1).setLabel(self.name)
60     self.getControl(2).setLabel(self.moviegroups)
61     self.getControl(3).setLabel(self.description)
62     self.getControl(10).setLabel(self.playlength)
63     self.getControl(11).setLabel(self.codecdetails)
64 torben 2596
65 torben 2601 orig_img_width = self.getControl(40).getWidth()
66     self.starwidth = (float(self.imdbrating) / 10.0) * orig_img_width
67     self.getControl(40).setWidth(int(self.starwidth))
68 torben 2596
69 torben 2601 def setUrl(self, url):
70     print "SETURL:" + url
71     self.url = url
72     self.fetchClipDetails()
73 torben 2592
74 torben 2601 def fetchClipDetails(self):
75     param1 = parse_parameter_string(self.url)
76 torben 2595
77 torben 2601 self.clipkey = param1["clipkey"]
78     print "CLIPKEY:" + self.clipkey
79     detailurl = __backend__ + "&action=clipdetails&clipkey=" + self.clipkey
80 torben 2595
81 torben 2601 xml = open_url(detailurl)
82 torben 2595
83 torben 2601 doc = parseString(xml)
84     self.imdbrating = getText(doc.getElementsByTagName("imdbrating"))
85     self.moviegroups = getText(doc.getElementsByTagName("moviegroups"))
86     self.playlength = getText(doc.getElementsByTagName("playlength"))
87     self.codecdetails = getText(doc.getElementsByTagName("codecdetails"))
88 torben 2595
89 torben 2601 def setName(self, name):
90     self.name = name
91 torben 2595
92 torben 2601 def setDescription(self, description):
93     self.description = description
94 torben 2595
95 torben 2596
96 torben 2601 class TodicPlayer(xbmc.Player):
97 torben 2596
98 torben 2601 def __init__(self, *args, **kwargs):
99     # xbmc.Player.__init__(selv,*args,**kwargs)
100     xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER)
101     self.stopped = False
102     self.started = False
103 torben 3142 self.playingPosition = 0.0
104     self.lastReport = 0
105 torben 2601 print "[TodicPlayer] init"
106 torben 2592
107 torben 2601 # @catchall
108     def onPlayBackStarted(self):
109     self.started = True
110     print "[TodicPlayer] : started"
111     # super.onPlayBackStarted()
112 torben 2592
113 torben 3142 #When user presses stop, we report back the the position registered in the last call to self.tick()
114 torben 2601 def onPlayBackStopped(self):
115     self.stopped = True
116     print "[TodicPlayer] : stopped"
117 torben 3142 url = __backend__ + "&action=playbacktime&subaction=stopped&time=" + str( self.playingPosition )
118 torben 3143 open_url_safe(url)
119 torben 2592
120 torben 3142
121 torben 2601 def onPlayBackEnded(self):
122     self.stopped = True
123     print "[TodicPlayer] : ended"
124 torben 3142 url = __backend__ + "&action=playbacktime&subaction=ended&time="
125 torben 3143 open_url_safe(url)
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     self.reportPlaytime()
134     self.lastReport = now
135    
136 torben 1676
137    
138 torben 3142 def reportPlaytime(self):
139     url = __backend__ + "&action=playbacktime&subaction=playing&time=" + str( self.playingPosition )
140 torben 3143 open_url_safe(url)
141 torben 3142 print "[Todic] reportPlaytime:" + url
142    
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     print "Some error during open_url call to ", url
188    
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 2601 if playcount == '':
249     playcount = '0'
250     playcount = int(playcount)
251 torben 1647
252 torben 2601 # print "name:" + name
253 torben 1678 # print "url:" + url
254     # print "thumb:" + thumb
255     # print "description:" + description
256 torben 2601 listitem = xbmcgui.ListItem(
257     label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
258     listitem.setProperty('Fanart_Image', fanartImage)
259     if mode == '50':
260     infoLabels = {}
261     infoLabels['title'] = name
262     infoLabels['plot'] = description
263     infoLabels['playcount'] = playcount
264     listitem.setInfo('video', infoLabels)
265 torben 1678
266 torben 2601 name = name.encode('UTF-8')
267     description = description.encode('UTF-8')
268 torben 1678
269 torben 2601 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
270     name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
271 torben 2602 xbmcplugin.addDirectoryItem(
272 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
273 torben 1648
274 torben 2601 if (endlist == True):
275     xbmcplugin.endOfDirectory(int(sys.argv[1]))
276 torben 1923
277    
278 torben 2601 def play_video(url, name, description):
279     if (description == None or description == ""):
280     play_real_video(url, name)
281     else:
282     d = TodicMovieDialog()
283     d.setUrl(url)
284     d.setName(name)
285     d.setDescription(description)
286 torben 1629
287 torben 2601 d.doModal()
288 torben 1629
289    
290 torben 2592 def play_real_video(url, name):
291 torben 2601 xml = open_url(url)
292     print 'TODIC url: ' + str(url)
293     print 'TODIC xml: ' + xml
294 torben 1678
295 torben 2601 doc = parseString(xml)
296     url = getText(doc.getElementsByTagName("url"))
297 torben 1678
298 torben 2601 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
299     subtitlesfile = os.path.join(datapath, 'temp.srt')
300 torben 1914
301 torben 2601 # if old srt file exists delete it first
302     if os.path.isfile(subtitlesfile):
303     os.unlink(subtitlesfile)
304 torben 1917
305 torben 2601 print '[TODIC] subs: ' + str(subtitleurl)
306     if len(subtitleurl) > 0:
307     subtitles = open_url(subtitleurl)
308     SaveFile(subtitlesfile, subtitles)
309     print 'TODIC downloaded subtitles'
310 torben 1914
311 torben 2601 image = xbmc.getInfoImage('ListItem.Thumb')
312     listitem = xbmcgui.ListItem(
313     label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
314     listitem.setInfo(type="Video", infoLabels={"Title": name})
315 torben 1914
316 torben 2601 player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
317     player.play(str(url), listitem)
318 torben 1914
319 torben 2601 # kan ikke loade subtitles hvis foerend playeren koerer
320     count = 0
321     while not xbmc.Player().isPlaying():
322     xbmc.sleep(500)
323     count += 1
324     if count > 10:
325     break
326 torben 1629
327 torben 2601 if xbmc.Player().isPlaying():
328     if os.path.isfile(subtitlesfile):
329     player.setSubtitles(subtitlesfile)
330     print 'TODIC started subtitles'
331     else:
332     player.disableSubtitles()
333 torben 1914
334 torben 3142 #Holder python kørernde indtil at det bliver bedt om at stoppe
335     while (not xbmc.abortRequested):
336     player.tick()
337     xbmc.sleep(100)
338    
339    
340    
341 torben 1678 # player.callbackLoop()
342 torben 1676
343    
344 torben 1631 def search():
345 torben 2601 search = getUserInput("Todic Søgning")
346 torben 1629
347 torben 2601 if (search != None and search != ""):
348     url = __backend__ + "&action=search&search=" + \
349     urllib.quote_plus(search)
350 torben 1631
351 torben 2601 # print "[TODIC] Search start: " + search
352     # print "[TODIC] Search url: " + url
353 torben 1631
354 torben 2601 buildList(url, "søgning")
355 torben 1631
356 torben 2601
357 torben 2097 def searchSeries():
358 torben 2601 search = getUserInput("Todic Serie Søgning")
359 torben 2097
360 torben 2601 if (search != None and search != ""):
361     url = __backend__ + "&action=searchseries&search=" + \
362     urllib.quote_plus(search)
363 torben 2097
364 torben 2601 # print "[TODIC] Search start: " + search
365     # print "[TODIC] Search url: " + url
366 torben 2097
367 torben 2601 buildList(url, "serie søgning")
368 torben 2097
369 torben 1631
370 torben 2601 #=================================== Tool Box =======================================
371 torben 1631 # shows a more userfriendly notification
372     def showMessage(heading, message):
373 torben 2601 duration = 15 * 1000
374     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
375     (heading, message, duration))
376 torben 1631
377    
378     # raise a keyboard for user input
379 torben 2601 def getUserInput(title="Input", default="", hidden=False):
380     result = None
381 torben 1631
382 torben 2601 # Fix for when this functions is called with default=None
383     if not default:
384     default = ""
385 torben 1631
386 torben 2601 keyboard = xbmc.Keyboard(default, title)
387     keyboard.setHiddenInput(hidden)
388     keyboard.doModal()
389 torben 1631
390 torben 2601 if keyboard.isConfirmed():
391     result = keyboard.getText()
392    
393     return result
394    
395    
396 torben 1629 def get_params():
397 torben 2601 return parse_parameter_string(sys.argv[2])
398 torben 2595
399 torben 1629
400 torben 2601 def parse_parameter_string(paramstring):
401     param = []
402     if len(paramstring) >= 2:
403     params = paramstring
404     cleanedparams = params.replace('?', '')
405     if (params[len(params) - 1] == '/'):
406     params = params[0:len(params) - 2]
407     pairsofparams = cleanedparams.split('&')
408     param = {}
409     for i in range(len(pairsofparams)):
410     splitparams = {}
411     splitparams = pairsofparams[i].split('=')
412     if (len(splitparams)) == 2:
413     param[splitparams[0]] = splitparams[1]
414     return param
415 torben 1629
416 torben 2601
417 torben 1629 params = get_params()
418     url = None
419     name = None
420     mode = None
421 torben 2592 description = None
422 torben 1629
423     try:
424 torben 2601 url = urllib.unquote_plus(params["url"])
425 torben 1629 except:
426 torben 2601 pass
427 torben 1629 try:
428 torben 2601 name = urllib.unquote_plus(params["name"])
429 torben 1629 except:
430 torben 2601 pass
431 torben 1629 try:
432 torben 2601 mode = int(params["mode"])
433 torben 1629 except:
434 torben 2601 pass
435 torben 2592 try:
436 torben 2601 description = urllib.unquote_plus(params["description"])
437 torben 2592 except:
438 torben 2601 pass
439 torben 1629
440 torben 2440 try:
441 torben 2601 open_url("http://todic.dk")
442 torben 2440 except:
443 torben 2601 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
444     exit()
445 torben 2440
446 torben 2601
447 torben 1814 if url == 'refresh':
448 torben 2601 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
449     # ikke med XBMC12
450     xbmc.executebuiltin("Container.Refresh")
451 torben 1629
452 torben 2601
453 torben 1814 elif mode == None:
454 torben 2601 # build main menu
455     rootMenu()
456    
457 torben 1629 elif mode == 1:
458 torben 2601 # build list of movie starting letters
459     buildList(url, name)
460 torben 1629
461 torben 1631 elif mode == 10:
462 torben 2601 search()
463 torben 2097
464     elif mode == 11:
465 torben 2601 searchSeries()
466 torben 1631
467 torben 2601
468 torben 1629 elif mode == 50:
469 torben 2601 play_video(url, name, description)

  ViewVC Help
Powered by ViewVC 1.1.20