Kodi plugin to to play various online streams (mostly Latvian)

context_download.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import sys, os, urllib2, re
  2. from kodiswift import xbmc, xbmcgui, CLI_MODE
  3. from kodiswift import Plugin, storage
  4. from resources.lib.content import util, ContentSources, Downloader
  5. from twisted.web import client
  6. from twisted.internet import reactor, defer
  7. #plugin = Plugin()
  8. #plugin.load_addon_settings()
  9. #playlist = plugin.get_setting("general_playlist",str)
  10. #proxy_url = plugin.get_setting("general_proxy_url",str)
  11. cunicode = lambda s: s.decode("utf8") if isinstance(s, str) else s
  12. cstr = lambda s: s.encode("utf8") if isinstance(s, unicode) else s
  13. cmd = sys.argv[1]
  14. title = sys.argv[2]
  15. data = sys.argv[3]
  16. download_dir = sys.argv[4]
  17. cur_directory = os.path.dirname(__file__)
  18. sources_directory = os.path.join(cur_directory,"resources","lib", "content", "sources")
  19. sources = ContentSources.ContentSources(sources_directory)
  20. if not sources.is_video(data):
  21. print "It is not video link"
  22. sys.exit(1)
  23. streams = sources.get_streams(data)
  24. if not CLI_MODE:
  25. ret = xbmcgui.Dialog().select("Select stream",streams)
  26. else:
  27. ret = 0
  28. stream = streams[ret]
  29. #output = stream["name"].replace("\\"," ").replace(":"," ").replace("|"," ")
  30. output = re.sub("[\\/\n\r\t,:\?\|'~\.]","_",title)
  31. for sub in stream["subs"]:
  32. suburl = sub["url"]
  33. slang = "_" + sub["lang"] if sub["lang"] else ""
  34. try:
  35. subs = urllib2.urlopen(suburl).read()
  36. except:
  37. subs = None
  38. if subs:
  39. if ".xml" in suburl:
  40. subs = util.ttaf2srt(subs)
  41. subext = ".srt"
  42. elif ".vtt" in suburl:
  43. subext = ".vtt"
  44. elif ".srt" in suburl:
  45. subext = ".srt"
  46. else:
  47. subext = ""
  48. if subext:
  49. subfile = cunicode(os.path.join(download_dir, output+slang+subext))
  50. with open(subfile,"w") as f:
  51. f.write(subs)
  52. else:
  53. print "\n Error downloading subtitle %s"%suburl
  54. d = Downloader.download_video(stream["url"], os.path.join(download_dir, output), stream["headers"])
  55. reactor.run()
  56. #xbmcgui.Dialog().ok("Info","Start download")
  57. #mode = "a" if os.path.exists("context_menu.log") else "w"
  58. #with open("context_menu.log", mode) as f:
  59. # f.write("%s %s %s %s", sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])