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

  ViewVC Help
Powered by ViewVC 1.1.20