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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import os,os.path,sys, urllib
  2. from xbmcswift2 import Plugin, ListItem
  3. from xbmcswift2 import xbmc, xbmcgui, xbmcplugin
  4. from resources.lib import ContentSources, util
  5. plugin = Plugin()
  6. sources = ContentSources.ContentSources(os.path.join(os.path.dirname(__file__),"resources","lib","sources"))
  7. prefix = ""
  8. @plugin.route(".+" )
  9. def main():
  10. global prefix
  11. prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc)
  12. data = plugin.request.url.replace(prefix,"")
  13. data = urllib.unquote(data)
  14. if not data:
  15. data = "config::home"
  16. if sources.is_video(data):
  17. try:
  18. streams = sources.get_streams(data)
  19. except Exception,e:
  20. plugin.notify(str(e),"",10000)
  21. return None
  22. if streams:
  23. return play_video(streams)
  24. else:
  25. plugin.notify("No streams found!",10000)
  26. return None
  27. else:
  28. if "{0}" in data:
  29. q = plugin.keyboard(default=None, heading=None, hidden=False)
  30. data = data.format(q)
  31. try:
  32. items = get_list(data)
  33. except Exception,e:
  34. plugin.notify(str(e),"")
  35. return None
  36. return items
  37. def get_list(data):
  38. content = sources.get_content(data)
  39. items = []
  40. for item in content:
  41. if item[1] == "back": continue
  42. title = item[0].decode("utf8")
  43. data2 = prefix+item[1]
  44. is_playable = True if sources.is_video(item[1]) else False
  45. img = item[2]
  46. desc = item[3].decode("utf8")
  47. items.append({
  48. "label": title,
  49. "path": data2,
  50. "thumbnail":img,
  51. "info":{"plot":desc},
  52. "is_playable":is_playable
  53. })
  54. return items
  55. def play_video(streams):
  56. stream = streams[0]
  57. subfiles = []
  58. print "play_video ",stream["url"]
  59. if "subs" in stream and stream["subs"]:
  60. for sub in stream["subs"]:
  61. suburl = sub["url"]
  62. subs = util.Captions(suburl)
  63. srt = subs.get_srt()
  64. subfile = plugin.temp_fn("subtitles.srt")
  65. subfile = os.path.join(os.path.dirname(__file__),sub["lang"]+".srt")
  66. f = open(subfile, "w")
  67. f.write(srt)
  68. # TODO nestrada uz ltc.lv subtititliem!
  69. # import codecs
  70. #f = codecs.open(subfile, 'w', "utf-8-sig")
  71. #f.write(srt.decode("utf8"))
  72. f.close()
  73. subfiles.append(subfile)
  74. item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"])
  75. item.set_info("video",{"plot":stream["desc"]})
  76. item.set_is_playable(True)
  77. item = plugin._listitemify(item)
  78. item.set_played(True)
  79. succeeded = True
  80. xbmcplugin.setResolvedUrl(plugin.handle, succeeded, item.as_xbmc_listitem())
  81. #plugin.play_video(item)
  82. for subfile in subfiles:
  83. print "Add subtitle file - ", subfile
  84. plugin._add_subtitles(subfile)
  85. #break # TODO multipe subtitles?
  86. return [item]
  87. if __name__ == '__main__':
  88. plugin.run()