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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. import os,os.path,sys, urllib, traceback
  3. from kodiswift import Plugin, ListItem
  4. from kodiswift import xbmc, xbmcgui, xbmcplugin, xbmcvfs, CLI_MODE
  5. from resources.lib import ContentSources, util
  6. plugin = Plugin()
  7. plugin.load_addon_settings()
  8. sources = ContentSources.ContentSources(os.path.join(os.path.dirname(__file__),"resources","lib","sources"))
  9. for source in sources.plugins:
  10. if not ("options" in dir(sources.plugins[source]) and sources.plugins[source].options): continue
  11. options = sources.plugins[source].options
  12. if not options: continue
  13. for option in options:
  14. key="%s_%s"%(source,option)
  15. if key in ("viaplay_device"): continue # exception list,
  16. value = plugin.get_setting(key)
  17. options[option] = value
  18. sources.plugins[source].options_write(options)
  19. prefix = ""
  20. @plugin.route(".+" )
  21. def main():
  22. global prefix
  23. prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc)
  24. plugin.set_content("movies")
  25. data = plugin.request.url.replace(prefix,"")
  26. data = urllib.unquote(data)
  27. if not data:
  28. data = "config::home"
  29. if sources.is_video(data):
  30. try:
  31. streams = sources.get_streams(data)
  32. except Exception,e:
  33. #xbmcgui.Dialog().ok("Error",unicode(e))
  34. plugin.notify(unicode(e),"Error",10000, xbmcgui.NOTIFICATION_ERROR)
  35. traceback.print_exc()
  36. return plugin.set_resolved_url(None)
  37. if streams:
  38. return play_video(streams)
  39. else:
  40. plugin.notify("No streams found!","Error",10000,xbmcgui.NOTIFICATION_ERROR)
  41. return plugin.set_resolved_url(None)
  42. else:
  43. if "{0}" in data:
  44. q = plugin.keyboard(default=None, heading="Search for", hidden=False)
  45. data = data.format(q)
  46. try:
  47. items = get_list(data)
  48. except Exception,e:
  49. plugin.notify(unicode(e),"Error",10000,xbmcgui.NOTIFICATION_ERROR)
  50. traceback.print_exc()
  51. return []
  52. #xbmc.executebuiltin('Container.SetViewMode(500)')
  53. plugin.set_view_mode(50)
  54. return items
  55. def get_list(data):
  56. content = sources.get_content(data)
  57. items = []
  58. for item in content:
  59. if item[1] == "back": continue
  60. title = item[0].decode("utf8")
  61. data2 = prefix+item[1]
  62. is_playable = True if sources.is_video(item[1]) else False
  63. img = item[2]
  64. desc = item[3].decode("utf8")
  65. context_menu = [("Add to PlayStream favorites",
  66. 'RunScript(special://home/addons/%s/context_menu.py,"add","%s","%s","%s","%s")'%(plugin.id, title, data2,img, desc))]
  67. items.append({
  68. "label": title,
  69. "path": data2,
  70. "thumbnail":img,
  71. "info":{"plot":desc},
  72. "is_playable":is_playable,
  73. "context_menu": context_menu,
  74. })
  75. return items
  76. def play_video(streams):
  77. if len(streams)>1:
  78. slist = []
  79. for s in streams:
  80. slist.append("%s [%s,%s]"%(s["name"],s["quality"],s["lang"]))
  81. res = xbmcgui.Dialog().select("Select stream",slist) if not CLI_MODE else 0
  82. #res = xbmcgui.Dialog().contextmenu(slist) if not CLI_MODE else 0
  83. stream = streams[res]
  84. else:
  85. stream = streams[0]
  86. subfiles = []
  87. stream = util.stream_change(stream)
  88. print "play_video ", stream["url"]
  89. if "subs" in stream and stream["subs"]:
  90. for sub in stream["subs"]:
  91. suburl = sub["url"]
  92. subs = util.Captions(suburl)
  93. srt = subs.get_srt()
  94. #subfile = plugin.temp_fn("subtitles.srt")
  95. subfile = os.path.join(os.path.dirname(__file__),sub["lang"]+".srt")
  96. f = open(subfile, "w")
  97. f.write(srt)
  98. f.close()
  99. subfiles.append(subfile)
  100. item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"])
  101. item.set_info("video",{"plot":stream["desc"]})
  102. item.set_is_playable(True)
  103. return plugin.set_resolved_url(item,subfiles)
  104. #return plugin.play_video(item)
  105. if __name__ == '__main__':
  106. if CLI_MODE:
  107. from kodiswift.cli.cli import main as start
  108. start()
  109. else:
  110. plugin.run()