/[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 2602 - (hide annotations) (download) (as text)
Mon Jul 13 14:51:49 2015 UTC (8 years, 10 months ago) by torben
File MIME type: text/x-python
File size: 11666 byte(s)
pyflakes
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 2596 Version 0.0.15
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    
24 torben 1629 __addon__ = xbmcaddon.Addon(id='plugin.video.todic')
25     __key__ = __addon__.getSetting('xbmckey').lower()
26     __backend__ = "http://todic.dk/xbmc.php?xbmckey=" + __key__
27 torben 2594 fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg')
28 torben 2601 datapath = xbmc.translatePath(
29     'special://profile/addon_data/plugin.video.todic/')
30 torben 1629
31 torben 2592 ADDON_PATH = __addon__.getAddonInfo('path')
32 torben 2601 SkinMasterPath = os.path.join(ADDON_PATH, 'skins') + '/'
33 torben 2592 MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/'
34     MySkin = 'main.xml'
35 torben 1676
36 torben 2592
37     class TodicMovieDialog(xbmcgui.WindowXMLDialog):
38    
39 torben 2601 def __new__(cls):
40     return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH)
41 torben 2592
42 torben 2601 def __init__(self):
43     super(TodicMovieDialog, self).__init__()
44 torben 2592
45 torben 2601 def onClick(self, controlId):
46     print "OnClick: " + str(controlId)
47 torben 2592
48 torben 2601 if (controlId == 50):
49     self.close()
50     play_real_video(self.url, self.name)
51 torben 2592
52 torben 2601 if (controlId == 98):
53     self.close()
54 torben 2592
55 torben 2601 def onInit(self):
56 torben 2592
57 torben 2601 print "ONINIT"
58     self.getControl(1).setLabel(self.name)
59     self.getControl(2).setLabel(self.moviegroups)
60     self.getControl(3).setLabel(self.description)
61     self.getControl(10).setLabel(self.playlength)
62     self.getControl(11).setLabel(self.codecdetails)
63 torben 2596
64 torben 2601 orig_img_width = self.getControl(40).getWidth()
65     self.starwidth = (float(self.imdbrating) / 10.0) * orig_img_width
66     self.getControl(40).setWidth(int(self.starwidth))
67 torben 2596
68 torben 2601 def setUrl(self, url):
69     print "SETURL:" + url
70     self.url = url
71     self.fetchClipDetails()
72 torben 2592
73 torben 2601 def fetchClipDetails(self):
74     param1 = parse_parameter_string(self.url)
75 torben 2595
76 torben 2601 self.clipkey = param1["clipkey"]
77     print "CLIPKEY:" + self.clipkey
78     detailurl = __backend__ + "&action=clipdetails&clipkey=" + self.clipkey
79 torben 2595
80 torben 2601 xml = open_url(detailurl)
81 torben 2595
82 torben 2601 doc = parseString(xml)
83     self.imdbrating = getText(doc.getElementsByTagName("imdbrating"))
84     self.moviegroups = getText(doc.getElementsByTagName("moviegroups"))
85     self.playlength = getText(doc.getElementsByTagName("playlength"))
86     self.codecdetails = getText(doc.getElementsByTagName("codecdetails"))
87 torben 2595
88 torben 2601 def setName(self, name):
89     self.name = name
90 torben 2595
91 torben 2601 def setDescription(self, description):
92     self.description = description
93 torben 2595
94 torben 2596
95 torben 2601 class TodicPlayer(xbmc.Player):
96 torben 2596
97 torben 2601 def __init__(self, *args, **kwargs):
98     # xbmc.Player.__init__(selv,*args,**kwargs)
99     xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER)
100     self.stopped = False
101     self.started = False
102     print "[TodicPlayer] init"
103 torben 2592
104 torben 2601 # @catchall
105     def onPlayBackStarted(self):
106     self.started = True
107     print "[TodicPlayer] : started"
108     # super.onPlayBackStarted()
109 torben 2592
110 torben 2601 def onPlayBackStopped(self):
111     self.stopped = True
112     print "[TodicPlayer] : stopped"
113 torben 2592
114 torben 2601 def onPlayBackEnded(self):
115     self.stopped = True
116     print "[TodicPlayer] : ended"
117 torben 1676
118 torben 2601 def callbackLoop(self):
119     print "[Todic] startLoop"
120     while (self.stopped == False):
121     if (self.started == True):
122     print "[todic] " + str(self.getTime())
123     xbmc.sleep(5000)
124 torben 1676
125    
126 torben 2601 def getText2(nodelist):
127     rc = []
128     for node in nodelist:
129     if node.nodeType == node.TEXT_NODE:
130     rc.append(node.data)
131     else:
132     rc.append(getText(node.childNodes))
133     return ''.join(rc)
134 torben 1676
135    
136 torben 1678 def getText(nodelist):
137 torben 2601 if nodelist.length == 0:
138     return ''
139     else:
140     if nodelist[0].childNodes.length == 0:
141     return ''
142     else:
143     return nodelist[0].childNodes[0].nodeValue
144 torben 1676
145 torben 2601
146 torben 1914 def SaveFile(path, data):
147 torben 2601 file = open(path, 'w')
148     file.write(data)
149     file.close()
150 torben 1678
151 torben 1914
152 torben 1629 def open_url(url):
153 torben 2601 req = urllib2.Request(url)
154     content = urllib2.urlopen(req)
155     data = content.read()
156     content.close()
157     return data
158 torben 1629
159 torben 2601
160 torben 1629 def rootMenu():
161 torben 1648
162 torben 2601 msg = open_url(__backend__ + "&action=messages")
163     msg = msg.strip()
164 torben 1799
165 torben 2601 if msg != "":
166     dialog = xbmcgui.Dialog()
167     dialog.ok('XBMC Todic', msg)
168 torben 1799
169 torben 2601 buildList(__backend__, "", False) # call default list
170 torben 1631
171 torben 2601 # Adde xtra items to root menu
172     listitem = xbmcgui.ListItem(
173     label="Søg film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
174     listitem.setProperty('Fanart_Image', fanartImage)
175 torben 1648
176 torben 2601 u = sys.argv[0] + "?mode=10&name="
177 torben 2602 xbmcplugin.addDirectoryItem(
178 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
179 torben 1631
180 torben 2601 # add search series
181     listitem = xbmcgui.ListItem(
182     label="Søg Serier ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
183     listitem.setProperty('Fanart_Image', fanartImage)
184 torben 2097
185 torben 2601 u = sys.argv[0] + "?mode=11&name="
186 torben 2602 xbmcplugin.addDirectoryItem(
187 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
188 torben 2097
189 torben 2601 xbmcplugin.endOfDirectory(int(sys.argv[1]))
190 torben 1629
191    
192 torben 2601 def buildList(url, title, endlist=True):
193     print '[TODIC]:' + str(url)
194 torben 1678
195 torben 2601 link = open_url(url)
196     doc = parseString(link)
197     ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
198     print '[TODIC]' + str(ty)
199 torben 1646
200 torben 2601 if ty == 'clipList':
201     mode = '50'
202     folder = False
203     else:
204     mode = '1'
205     folder = True
206 torben 1646
207 torben 2601 entries = doc.getElementsByTagName("entry")
208     l = len(entries)
209     description = ''
210     for entry in entries:
211     name = getText(entry.getElementsByTagName("title"))
212     url = getText(entry.getElementsByTagName("url"))
213     thumb = getText(entry.getElementsByTagName("cover"))
214     description = getText(entry.getElementsByTagName("description"))
215     playcount = getText(entry.getElementsByTagName("playcount"))
216 torben 1829
217 torben 2601 if playcount == '':
218     playcount = '0'
219     playcount = int(playcount)
220 torben 1647
221 torben 2601 # print "name:" + name
222 torben 1678 # print "url:" + url
223     # print "thumb:" + thumb
224     # print "description:" + description
225 torben 2601 listitem = xbmcgui.ListItem(
226     label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
227     listitem.setProperty('Fanart_Image', fanartImage)
228     if mode == '50':
229     infoLabels = {}
230     infoLabels['title'] = name
231     infoLabels['plot'] = description
232     infoLabels['playcount'] = playcount
233     listitem.setInfo('video', infoLabels)
234 torben 1678
235 torben 2601 name = name.encode('UTF-8')
236     description = description.encode('UTF-8')
237 torben 1678
238 torben 2601 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
239     name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
240 torben 2602 xbmcplugin.addDirectoryItem(
241 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
242 torben 1648
243 torben 2601 if (endlist == True):
244     xbmcplugin.endOfDirectory(int(sys.argv[1]))
245 torben 1923
246    
247 torben 2601 def play_video(url, name, description):
248     if (description == None or description == ""):
249     play_real_video(url, name)
250     else:
251     d = TodicMovieDialog()
252     d.setUrl(url)
253     d.setName(name)
254     d.setDescription(description)
255 torben 1629
256 torben 2601 d.doModal()
257 torben 1629
258    
259 torben 2592 def play_real_video(url, name):
260 torben 2601 xml = open_url(url)
261     print 'TODIC url: ' + str(url)
262     print 'TODIC xml: ' + xml
263 torben 1678
264 torben 2601 doc = parseString(xml)
265     url = getText(doc.getElementsByTagName("url"))
266 torben 1678
267 torben 2601 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
268     subtitlesfile = os.path.join(datapath, 'temp.srt')
269 torben 1914
270 torben 2601 # if old srt file exists delete it first
271     if os.path.isfile(subtitlesfile):
272     os.unlink(subtitlesfile)
273 torben 1917
274 torben 2601 print '[TODIC] subs: ' + str(subtitleurl)
275     if len(subtitleurl) > 0:
276     subtitles = open_url(subtitleurl)
277     SaveFile(subtitlesfile, subtitles)
278     print 'TODIC downloaded subtitles'
279 torben 1914
280 torben 2601 image = xbmc.getInfoImage('ListItem.Thumb')
281     listitem = xbmcgui.ListItem(
282     label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
283     listitem.setInfo(type="Video", infoLabels={"Title": name})
284 torben 1914
285 torben 2601 player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
286     player.play(str(url), listitem)
287 torben 1914
288 torben 2601 # kan ikke loade subtitles hvis foerend playeren koerer
289     count = 0
290     while not xbmc.Player().isPlaying():
291     xbmc.sleep(500)
292     count += 1
293     if count > 10:
294     break
295 torben 1629
296 torben 2601 if xbmc.Player().isPlaying():
297     if os.path.isfile(subtitlesfile):
298     player.setSubtitles(subtitlesfile)
299     print 'TODIC started subtitles'
300     else:
301     player.disableSubtitles()
302 torben 1914
303 torben 1678 # player.callbackLoop()
304 torben 1676
305    
306 torben 1631 def search():
307 torben 2601 search = getUserInput("Todic Søgning")
308 torben 1629
309 torben 2601 if (search != None and search != ""):
310     url = __backend__ + "&action=search&search=" + \
311     urllib.quote_plus(search)
312 torben 1631
313 torben 2601 # print "[TODIC] Search start: " + search
314     # print "[TODIC] Search url: " + url
315 torben 1631
316 torben 2601 buildList(url, "søgning")
317 torben 1631
318 torben 2601
319 torben 2097 def searchSeries():
320 torben 2601 search = getUserInput("Todic Serie Søgning")
321 torben 2097
322 torben 2601 if (search != None and search != ""):
323     url = __backend__ + "&action=searchseries&search=" + \
324     urllib.quote_plus(search)
325 torben 2097
326 torben 2601 # print "[TODIC] Search start: " + search
327     # print "[TODIC] Search url: " + url
328 torben 2097
329 torben 2601 buildList(url, "serie søgning")
330 torben 2097
331 torben 1631
332 torben 2601 #=================================== Tool Box =======================================
333 torben 1631 # shows a more userfriendly notification
334     def showMessage(heading, message):
335 torben 2601 duration = 15 * 1000
336     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
337     (heading, message, duration))
338 torben 1631
339    
340     # raise a keyboard for user input
341 torben 2601 def getUserInput(title="Input", default="", hidden=False):
342     result = None
343 torben 1631
344 torben 2601 # Fix for when this functions is called with default=None
345     if not default:
346     default = ""
347 torben 1631
348 torben 2601 keyboard = xbmc.Keyboard(default, title)
349     keyboard.setHiddenInput(hidden)
350     keyboard.doModal()
351 torben 1631
352 torben 2601 if keyboard.isConfirmed():
353     result = keyboard.getText()
354    
355     return result
356    
357    
358 torben 1629 def get_params():
359 torben 2601 return parse_parameter_string(sys.argv[2])
360 torben 2595
361 torben 1629
362 torben 2601 def parse_parameter_string(paramstring):
363     param = []
364     if len(paramstring) >= 2:
365     params = paramstring
366     cleanedparams = params.replace('?', '')
367     if (params[len(params) - 1] == '/'):
368     params = params[0:len(params) - 2]
369     pairsofparams = cleanedparams.split('&')
370     param = {}
371     for i in range(len(pairsofparams)):
372     splitparams = {}
373     splitparams = pairsofparams[i].split('=')
374     if (len(splitparams)) == 2:
375     param[splitparams[0]] = splitparams[1]
376     return param
377 torben 1629
378 torben 2601
379 torben 1629 params = get_params()
380     url = None
381     name = None
382     mode = None
383 torben 2592 description = None
384 torben 1629
385     try:
386 torben 2601 url = urllib.unquote_plus(params["url"])
387 torben 1629 except:
388 torben 2601 pass
389 torben 1629 try:
390 torben 2601 name = urllib.unquote_plus(params["name"])
391 torben 1629 except:
392 torben 2601 pass
393 torben 1629 try:
394 torben 2601 mode = int(params["mode"])
395 torben 1629 except:
396 torben 2601 pass
397 torben 2592 try:
398 torben 2601 description = urllib.unquote_plus(params["description"])
399 torben 2592 except:
400 torben 2601 pass
401 torben 1629
402 torben 2440 try:
403 torben 2601 open_url("http://todic.dk")
404 torben 2440 except:
405 torben 2601 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
406     exit()
407 torben 2440
408 torben 2601
409 torben 1814 if url == 'refresh':
410 torben 2601 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
411     # ikke med XBMC12
412     xbmc.executebuiltin("Container.Refresh")
413 torben 1629
414 torben 2601
415 torben 1814 elif mode == None:
416 torben 2601 # build main menu
417     rootMenu()
418    
419 torben 1629 elif mode == 1:
420 torben 2601 # build list of movie starting letters
421     buildList(url, name)
422 torben 1629
423 torben 1631 elif mode == 10:
424 torben 2601 search()
425 torben 2097
426     elif mode == 11:
427 torben 2601 searchSeries()
428 torben 1631
429 torben 2601
430 torben 1629 elif mode == 50:
431 torben 2601 play_video(url, name, description)

  ViewVC Help
Powered by ViewVC 1.1.20