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

addon.py 5.6KB

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