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

addon.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. sources.plugins["config"].read_streams()
  28. if not data:
  29. data = "config::home"
  30. if sources.is_video(data):
  31. try:
  32. streams = sources.get_streams(data)
  33. except Exception,e:
  34. #xbmcgui.Dialog().ok("Error",unicode(e))
  35. plugin.notify(unicode(e),"Error",10000, xbmcgui.NOTIFICATION_ERROR)
  36. traceback.print_exc()
  37. return plugin.set_resolved_url(None)
  38. if streams:
  39. return play_video(streams)
  40. else:
  41. plugin.notify("No streams found!","Error",10000,xbmcgui.NOTIFICATION_ERROR)
  42. return plugin.set_resolved_url(None)
  43. else:
  44. if "{0}" in data:
  45. q = plugin.keyboard(default=None, heading="Search for", hidden=False)
  46. data = data.format(q)
  47. try:
  48. items = get_list(data)
  49. except Exception,e:
  50. plugin.notify(unicode(e),"Error",10000,xbmcgui.NOTIFICATION_ERROR)
  51. traceback.print_exc()
  52. return []
  53. #xbmc.executebuiltin('Container.SetViewMode(500)')
  54. plugin.set_view_mode(50)
  55. return items
  56. def get_list(data):
  57. content = sources.get_content(data)
  58. items = []
  59. for item in content:
  60. if item[1] == "back": continue
  61. title = item[0].decode("utf8")
  62. data2 = prefix+item[1]
  63. is_playable = True if sources.is_video(item[1]) else False
  64. img = item[2]
  65. desc = item[3].decode("utf8")
  66. context_menu = [
  67. ("Add to PlayStream favorites", 'RunScript(special://home/addons/%s/context_menu.py,"add","%s","%s","%s","%s")'%(plugin.id, title, item[1],img, desc)),
  68. ("Delete from PlayStream favorites",
  69. 'RunScript(special://home/addons/%s/context_menu.py,"delete","%s","%s","%s","%s")' % (
  70. plugin.id, title, item[1], img, desc)),
  71. ("Download",
  72. 'RunScript(special://home/addons/%s/context_menu.py,"download","%s","%s","%s","%s")' % (
  73. plugin.id, title, item[1], img, desc)),
  74. ]
  75. items.append({
  76. "label": title,
  77. "path": data2,
  78. "thumbnail":img,
  79. "info":{"plot":desc},
  80. "is_playable":is_playable,
  81. "context_menu": context_menu,
  82. })
  83. return items
  84. def play_video(streams):
  85. if len(streams)>1:
  86. slist = []
  87. for s in streams:
  88. slist.append("%s [%s,%s]"%(s["name"],s["quality"],s["lang"]))
  89. res = xbmcgui.Dialog().select("Select stream",slist) if not CLI_MODE else 0
  90. #res = xbmcgui.Dialog().contextmenu(slist) if not CLI_MODE else 0
  91. stream = streams[res]
  92. else:
  93. stream = streams[0]
  94. subfiles = []
  95. stream = util.stream_change(stream)
  96. print "play_video ", stream["url"]
  97. if "subs" in stream and stream["subs"]:
  98. for sub in stream["subs"]:
  99. suburl = sub["url"]
  100. subs = util.Captions(suburl)
  101. srt = subs.get_srt()
  102. #subfile = plugin.temp_fn("subtitles.srt")
  103. subfile = os.path.join(os.path.dirname(__file__),sub["lang"]+".srt")
  104. f = open(subfile, "w")
  105. f.write(srt)
  106. f.close()
  107. subfiles.append(subfile)
  108. item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"])
  109. item.set_info("video",{"plot":stream["desc"]})
  110. item.set_is_playable(True)
  111. return plugin.set_resolved_url(item,subfiles)
  112. #return plugin.play_video(item)
  113. if __name__ == '__main__':
  114. if CLI_MODE:
  115. from kodiswift.cli.cli import main as start
  116. start()
  117. else:
  118. plugin.run()