import os,os.path,sys, urllib from xbmcswift2 import Plugin, ListItem from xbmcswift2 import xbmc, xbmcgui, xbmcplugin from resources.lib import ContentSources, util plugin = Plugin() sources = ContentSources.ContentSources(os.path.join(os.path.dirname(__file__),"resources","lib","sources")) prefix = "" @plugin.route(".+" ) def main(): global prefix prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc) data = plugin.request.url.replace(prefix,"") data = urllib.unquote(data) if not data: data = "config::home" if sources.is_video(data): try: streams = sources.get_streams(data) except Exception,e: plugin.notify(str(e),"",10000) return None if streams: return play_video(streams) else: plugin.notify("No streams found!",10000) return None else: if "{0}" in data: q = plugin.keyboard(default=None, heading=None, hidden=False) data = data.format(q) try: items = get_list(data) except Exception,e: plugin.notify(str(e),"") return None return items def get_list(data): content = sources.get_content(data) items = [] for item in content: if item[1] == "back": continue title = item[0].decode("utf8") data2 = prefix+item[1] is_playable = True if sources.is_video(item[1]) else False img = item[2] desc = item[3].decode("utf8") items.append({ "label": title, "path": data2, "thumbnail":img, "info":{"plot":desc}, "is_playable":is_playable }) return items def play_video(streams): stream = streams[0] subfiles = [] print "play_video ",stream["url"] if "subs" in stream and stream["subs"]: for sub in stream["subs"]: suburl = sub["url"] subs = util.Captions(suburl) srt = subs.get_srt() subfile = plugin.temp_fn("subtitles.srt") subfile = os.path.join(os.path.dirname(__file__),sub["lang"]+".srt") f = open(subfile, "w") f.write(srt) # TODO nestrada uz ltc.lv subtititliem! # import codecs #f = codecs.open(subfile, 'w', "utf-8-sig") #f.write(srt.decode("utf8")) f.close() subfiles.append(subfile) item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"]) item.set_info("video",{"plot":stream["desc"]}) item.set_is_playable(True) item = plugin._listitemify(item) item.set_played(True) succeeded = True xbmcplugin.setResolvedUrl(plugin.handle, succeeded, item.as_xbmc_listitem()) #plugin.play_video(item) for subfile in subfiles: print "Add subtitle file - ", subfile plugin._add_subtitles(subfile) #break # TODO multipe subtitles? return [item] if __name__ == '__main__': plugin.run()