/[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 3142 - (hide annotations) (download) (as text)
Mon Nov 21 22:18:33 2016 UTC (7 years, 6 months ago) by torben
File MIME type: text/x-python
File size: 12570 byte(s)
todic/kodi 0.0.17 - with progress reporting
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 3142 Version 0.0.17
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     open_url(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     open_url(url)
126 torben 1676
127 torben 3142 def tick(self):
128     if ( self.isPlaying() ):
129     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     open_url(url)
141     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 1914 def SaveFile(path, data):
166 torben 2601 file = open(path, 'w')
167     file.write(data)
168     file.close()
169 torben 1678
170 torben 1914
171 torben 1629 def open_url(url):
172 torben 2601 req = urllib2.Request(url)
173     content = urllib2.urlopen(req)
174     data = content.read()
175     content.close()
176     return data
177 torben 1629
178 torben 2601
179 torben 1629 def rootMenu():
180 torben 1648
181 torben 2601 msg = open_url(__backend__ + "&action=messages")
182     msg = msg.strip()
183 torben 1799
184 torben 2601 if msg != "":
185     dialog = xbmcgui.Dialog()
186     dialog.ok('XBMC Todic', msg)
187 torben 1799
188 torben 2601 buildList(__backend__, "", False) # call default list
189 torben 1631
190 torben 2601 # Adde xtra items to root menu
191     listitem = xbmcgui.ListItem(
192     label="Søg film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
193     listitem.setProperty('Fanart_Image', fanartImage)
194 torben 1648
195 torben 2601 u = sys.argv[0] + "?mode=10&name="
196 torben 2602 xbmcplugin.addDirectoryItem(
197 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
198 torben 1631
199 torben 2601 # add search series
200     listitem = xbmcgui.ListItem(
201     label="Søg Serier ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
202     listitem.setProperty('Fanart_Image', fanartImage)
203 torben 2097
204 torben 2601 u = sys.argv[0] + "?mode=11&name="
205 torben 2602 xbmcplugin.addDirectoryItem(
206 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
207 torben 2097
208 torben 2601 xbmcplugin.endOfDirectory(int(sys.argv[1]))
209 torben 1629
210    
211 torben 2601 def buildList(url, title, endlist=True):
212     print '[TODIC]:' + str(url)
213 torben 1678
214 torben 2601 link = open_url(url)
215     doc = parseString(link)
216     ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
217     print '[TODIC]' + str(ty)
218 torben 1646
219 torben 2601 if ty == 'clipList':
220     mode = '50'
221     folder = False
222     else:
223     mode = '1'
224     folder = True
225 torben 1646
226 torben 2601 entries = doc.getElementsByTagName("entry")
227     l = len(entries)
228     description = ''
229     for entry in entries:
230     name = getText(entry.getElementsByTagName("title"))
231     url = getText(entry.getElementsByTagName("url"))
232     thumb = getText(entry.getElementsByTagName("cover"))
233     description = getText(entry.getElementsByTagName("description"))
234     playcount = getText(entry.getElementsByTagName("playcount"))
235 torben 1829
236 torben 2601 if playcount == '':
237     playcount = '0'
238     playcount = int(playcount)
239 torben 1647
240 torben 2601 # print "name:" + name
241 torben 1678 # print "url:" + url
242     # print "thumb:" + thumb
243     # print "description:" + description
244 torben 2601 listitem = xbmcgui.ListItem(
245     label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
246     listitem.setProperty('Fanart_Image', fanartImage)
247     if mode == '50':
248     infoLabels = {}
249     infoLabels['title'] = name
250     infoLabels['plot'] = description
251     infoLabels['playcount'] = playcount
252     listitem.setInfo('video', infoLabels)
253 torben 1678
254 torben 2601 name = name.encode('UTF-8')
255     description = description.encode('UTF-8')
256 torben 1678
257 torben 2601 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
258     name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
259 torben 2602 xbmcplugin.addDirectoryItem(
260 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
261 torben 1648
262 torben 2601 if (endlist == True):
263     xbmcplugin.endOfDirectory(int(sys.argv[1]))
264 torben 1923
265    
266 torben 2601 def play_video(url, name, description):
267     if (description == None or description == ""):
268     play_real_video(url, name)
269     else:
270     d = TodicMovieDialog()
271     d.setUrl(url)
272     d.setName(name)
273     d.setDescription(description)
274 torben 1629
275 torben 2601 d.doModal()
276 torben 1629
277    
278 torben 2592 def play_real_video(url, name):
279 torben 2601 xml = open_url(url)
280     print 'TODIC url: ' + str(url)
281     print 'TODIC xml: ' + xml
282 torben 1678
283 torben 2601 doc = parseString(xml)
284     url = getText(doc.getElementsByTagName("url"))
285 torben 1678
286 torben 2601 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
287     subtitlesfile = os.path.join(datapath, 'temp.srt')
288 torben 1914
289 torben 2601 # if old srt file exists delete it first
290     if os.path.isfile(subtitlesfile):
291     os.unlink(subtitlesfile)
292 torben 1917
293 torben 2601 print '[TODIC] subs: ' + str(subtitleurl)
294     if len(subtitleurl) > 0:
295     subtitles = open_url(subtitleurl)
296     SaveFile(subtitlesfile, subtitles)
297     print 'TODIC downloaded subtitles'
298 torben 1914
299 torben 2601 image = xbmc.getInfoImage('ListItem.Thumb')
300     listitem = xbmcgui.ListItem(
301     label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
302     listitem.setInfo(type="Video", infoLabels={"Title": name})
303 torben 1914
304 torben 2601 player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
305     player.play(str(url), listitem)
306 torben 1914
307 torben 2601 # kan ikke loade subtitles hvis foerend playeren koerer
308     count = 0
309     while not xbmc.Player().isPlaying():
310     xbmc.sleep(500)
311     count += 1
312     if count > 10:
313     break
314 torben 1629
315 torben 2601 if xbmc.Player().isPlaying():
316     if os.path.isfile(subtitlesfile):
317     player.setSubtitles(subtitlesfile)
318     print 'TODIC started subtitles'
319     else:
320     player.disableSubtitles()
321 torben 1914
322 torben 3142 #Holder python kørernde indtil at det bliver bedt om at stoppe
323     while (not xbmc.abortRequested):
324     player.tick()
325     xbmc.sleep(100)
326    
327    
328    
329 torben 1678 # player.callbackLoop()
330 torben 1676
331    
332 torben 1631 def search():
333 torben 2601 search = getUserInput("Todic Søgning")
334 torben 1629
335 torben 2601 if (search != None and search != ""):
336     url = __backend__ + "&action=search&search=" + \
337     urllib.quote_plus(search)
338 torben 1631
339 torben 2601 # print "[TODIC] Search start: " + search
340     # print "[TODIC] Search url: " + url
341 torben 1631
342 torben 2601 buildList(url, "søgning")
343 torben 1631
344 torben 2601
345 torben 2097 def searchSeries():
346 torben 2601 search = getUserInput("Todic Serie Søgning")
347 torben 2097
348 torben 2601 if (search != None and search != ""):
349     url = __backend__ + "&action=searchseries&search=" + \
350     urllib.quote_plus(search)
351 torben 2097
352 torben 2601 # print "[TODIC] Search start: " + search
353     # print "[TODIC] Search url: " + url
354 torben 2097
355 torben 2601 buildList(url, "serie søgning")
356 torben 2097
357 torben 1631
358 torben 2601 #=================================== Tool Box =======================================
359 torben 1631 # shows a more userfriendly notification
360     def showMessage(heading, message):
361 torben 2601 duration = 15 * 1000
362     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
363     (heading, message, duration))
364 torben 1631
365    
366     # raise a keyboard for user input
367 torben 2601 def getUserInput(title="Input", default="", hidden=False):
368     result = None
369 torben 1631
370 torben 2601 # Fix for when this functions is called with default=None
371     if not default:
372     default = ""
373 torben 1631
374 torben 2601 keyboard = xbmc.Keyboard(default, title)
375     keyboard.setHiddenInput(hidden)
376     keyboard.doModal()
377 torben 1631
378 torben 2601 if keyboard.isConfirmed():
379     result = keyboard.getText()
380    
381     return result
382    
383    
384 torben 1629 def get_params():
385 torben 2601 return parse_parameter_string(sys.argv[2])
386 torben 2595
387 torben 1629
388 torben 2601 def parse_parameter_string(paramstring):
389     param = []
390     if len(paramstring) >= 2:
391     params = paramstring
392     cleanedparams = params.replace('?', '')
393     if (params[len(params) - 1] == '/'):
394     params = params[0:len(params) - 2]
395     pairsofparams = cleanedparams.split('&')
396     param = {}
397     for i in range(len(pairsofparams)):
398     splitparams = {}
399     splitparams = pairsofparams[i].split('=')
400     if (len(splitparams)) == 2:
401     param[splitparams[0]] = splitparams[1]
402     return param
403 torben 1629
404 torben 2601
405 torben 1629 params = get_params()
406     url = None
407     name = None
408     mode = None
409 torben 2592 description = None
410 torben 1629
411     try:
412 torben 2601 url = urllib.unquote_plus(params["url"])
413 torben 1629 except:
414 torben 2601 pass
415 torben 1629 try:
416 torben 2601 name = urllib.unquote_plus(params["name"])
417 torben 1629 except:
418 torben 2601 pass
419 torben 1629 try:
420 torben 2601 mode = int(params["mode"])
421 torben 1629 except:
422 torben 2601 pass
423 torben 2592 try:
424 torben 2601 description = urllib.unquote_plus(params["description"])
425 torben 2592 except:
426 torben 2601 pass
427 torben 1629
428 torben 2440 try:
429 torben 2601 open_url("http://todic.dk")
430 torben 2440 except:
431 torben 2601 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
432     exit()
433 torben 2440
434 torben 2601
435 torben 1814 if url == 'refresh':
436 torben 2601 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
437     # ikke med XBMC12
438     xbmc.executebuiltin("Container.Refresh")
439 torben 1629
440 torben 2601
441 torben 1814 elif mode == None:
442 torben 2601 # build main menu
443     rootMenu()
444    
445 torben 1629 elif mode == 1:
446 torben 2601 # build list of movie starting letters
447     buildList(url, name)
448 torben 1629
449 torben 1631 elif mode == 10:
450 torben 2601 search()
451 torben 2097
452     elif mode == 11:
453 torben 2601 searchSeries()
454 torben 1631
455 torben 2601
456 torben 1629 elif mode == 50:
457 torben 2601 play_video(url, name, description)

  ViewVC Help
Powered by ViewVC 1.1.20