import sys, os, urllib2, re from kodiswift import xbmc, xbmcgui, CLI_MODE from kodiswift import Plugin, storage from resources.lib.content import util, ContentSources, Downloader from twisted.web import client from twisted.internet import reactor, defer #plugin = Plugin() #plugin.load_addon_settings() #playlist = plugin.get_setting("general_playlist",str) #proxy_url = plugin.get_setting("general_proxy_url",str) cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s cmd = sys.argv[1] title = sys.argv[2] data = sys.argv[3] download_dir = sys.argv[4] cur_directory = os.path.dirname(__file__) sources_directory = os.path.join(cur_directory,"resources","lib", "content", "sources") sources = ContentSources.ContentSources(sources_directory) if not sources.is_video(data): print "It is not video link" sys.exit(1) streams = sources.get_streams(data) if not CLI_MODE: ret = xbmcgui.Dialog().select("Select stream",streams) else: ret = 0 stream = streams[ret] #output = stream["name"].replace("\\"," ").replace(":"," ").replace("|"," ") output = re.sub("[\\/\n\r\t,:\?\|'~\.]","_",title) for sub in stream["subs"]: suburl = sub["url"] slang = "_" + sub["lang"] if sub["lang"] else "" try: subs = urllib2.urlopen(suburl).read() except: subs = None if subs: if ".xml" in suburl: subs = util.ttaf2srt(subs) subext = ".srt" elif ".vtt" in suburl: subext = ".vtt" elif ".srt" in suburl: subext = ".srt" else: subext = "" if subext: subfile = cunicode(os.path.join(download_dir, output+slang+subext)) with open(subfile,"w") as f: f.write(subs) else: print "\n Error downloading subtitle %s"%suburl d = Downloader.download_video(stream["url"], os.path.join(download_dir, output), stream["headers"]) reactor.run() #xbmcgui.Dialog().ok("Info","Start download") #mode = "a" if os.path.exists("context_menu.log") else "w" #with open("context_menu.log", mode) as f: # f.write("%s %s %s %s", sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])