# -*- coding: utf-8 -*- import os,os.path,sys, urllib, traceback try: import cPickle as pickle2 except: import pickle import pickle from kodiswift import Plugin, ListItem, storage from kodiswift import xbmc, xbmcgui, xbmcplugin, xbmcvfs, CLI_MODE from resources.lib import ContentSources, util sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),"resources","lib","sources")) plugin = Plugin() plugin.load_addon_settings() use_storage = False # TODO parametrs settingos + vajag nočekot vai nav labāk lietot pickle #storage_path = os.path.join(plugin.storage_path,"sources.p") if use_storage: storage = plugin.get_storage("playstream","pickle",ttl=30) if use_storage and "sources" in storage: print "Restore sources from storage" sources = storage["sources"] # if use_storage and os.path.exists(storage_path): #"sources" in storage: #sources = pickle.load(open(storage_path,"rb")) else: sources = ContentSources.ContentSources(os.path.join(os.path.dirname(__file__),"resources","lib","sources")) for source in sources.plugins: if not ("options" in dir(sources.plugins[source]) and sources.plugins[source].options): continue options = sources.plugins[source].options if not options: continue for option in options: key="%s_%s"%(source,option) if key in ("viaplay_device"): continue # exception list, value = plugin.get_setting(key) options[option] = value sources.plugins[source].options_write(options) prefix = "" @plugin.route(".+" ) def main(): global prefix prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc) plugin.set_content("movies") data = plugin.request.url.replace(prefix,"") data = urllib.unquote(data) sources.plugins["config"].read_streams() if not data: data = "config::home" if sources.is_video(data): try: streams = sources.get_streams(data) except Exception,e: #xbmcgui.Dialog().ok("Error",unicode(e)) plugin.notify(unicode(e),"Error",10000, xbmcgui.NOTIFICATION_ERROR) traceback.print_exc() return plugin.set_resolved_url(None) if streams: return play_video(streams) else: plugin.notify("No streams found!","Error",10000,xbmcgui.NOTIFICATION_ERROR) return plugin.set_resolved_url(None) else: if "{0}" in data: q = plugin.keyboard(default=None, heading="Search for", hidden=False) data = data.format(q) try: items = get_list(data) except Exception,e: plugin.notify(unicode(e),"Error",10000,xbmcgui.NOTIFICATION_ERROR) traceback.print_exc() return [] #xbmc.executebuiltin('Container.SetViewMode(500)') plugin.set_view_mode(50) if use_storage: print "Save sources to storage" storage["sources"] = sources storage.sync() 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") context_menu = [ ("Add to PlayStream favorites", 'RunScript(special://home/addons/%s/context_menu.py,"add","%s","%s","%s","%s")'%(plugin.id, title, item[1],img, desc)), ("Delete from PlayStream favorites", 'RunScript(special://home/addons/%s/context_menu.py,"delete","%s","%s","%s","%s")' % ( plugin.id, title, item[1], img, desc)), ("Download", 'RunScript(special://home/addons/%s/context_menu.py,"download","%s","%s","%s","%s")' % ( plugin.id, title, item[1], img, desc)), ] items.append({ "label": title, "path": data2, "thumbnail":img, "info":{"plot":desc}, "is_playable":is_playable, "context_menu": context_menu, }) return items def play_video(streams): if len(streams)>1: slist = [] for s in streams: slist.append("%s [%s,%s]"%(s["name"],s["quality"],s["lang"])) res = xbmcgui.Dialog().select("Select stream",slist) if not CLI_MODE else 0 #res = xbmcgui.Dialog().contextmenu(slist) if not CLI_MODE else 0 stream = streams[res] else: stream = streams[0] subfiles = [] stream = util.stream_change(stream) 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) 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) return plugin.set_resolved_url(item,subfiles) #return plugin.play_video(item) if __name__ == '__main__': if CLI_MODE: from kodiswift.cli.cli import main as start start() else: plugin.run() if use_storage: storage["sources"] = sources storage.sync() print "Save sources to storage" #pickle.dump(sources,open(storage_path,"wb"),pickle.HIGHEST_PROTOCOL)