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

addon.py 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. import os,os.path,sys, glob, shutil, re
  3. import urllib, traceback
  4. try:
  5. import cPickle as pickle
  6. except:
  7. import pickle
  8. import pickle
  9. from kodiswift import Plugin, ListItem, storage
  10. from kodiswift import xbmc, xbmcgui, xbmcplugin, xbmcvfs, xbmcaddon, CLI_MODE
  11. from resources.lib.content import ContentSources, util
  12. cur_directory = os.path.dirname(__file__)
  13. icon_folder = os.path.join(cur_directory, "resources", "picons")
  14. icong_url = xbmcaddon.Addon().getAddonInfo("path") + "/resources/picons/"
  15. sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),"resources","lib", "content", "sources"))
  16. plugin = Plugin()
  17. plugin.load_addon_settings()
  18. use_storage = plugin.get_setting("general_use_storage",bool) # TODO vajag nočekot vai nav labāk lietot pickle
  19. storage_ttl = plugin.get_setting("general_ttl",int)
  20. use_proxy = plugin.get_setting("general_proxy_use",bool)
  21. proxy_url = plugin.get_setting("general_proxy_url",str)
  22. playlist = plugin.get_setting("general_playlist",str)
  23. #storage_path = os.path.join(plugin.storage_path,"sources.p")
  24. if use_storage:
  25. try:
  26. storage = plugin.get_storage("playstream","pickle",ttl=30)
  27. except Exception as e:
  28. os.remove(os.path.join(plugin.storage_path, "playstream"))
  29. try:
  30. storage = plugin.get_storage("playstream", "pickle", ttl=30)
  31. except Exception as e:
  32. print "[playstream] error opening storage", plugin.storage_path
  33. print "Got Exception: ", str(e)
  34. import traceback
  35. traceback.print_exc()
  36. plugin.notify("Error opening permament storage", "Info", 10000, xbmcgui.NOTIFICATION_INFO)
  37. storage = None
  38. os.remove(os.path.join(plugin.storage_path, "playstream"))
  39. cur_directory = os.path.dirname(__file__)
  40. sources_directory = os.path.join(cur_directory,"resources","lib", "content", "sources")
  41. cfg_list = glob.glob(os.path.join(sources_directory,"*.cfg"))
  42. for cf in cfg_list:
  43. cf2 = os.path.join(plugin.storage_path,os.path.split(cf)[1])
  44. if not os.path.exists(cf2):
  45. shutil.copyfile(cf,cf2)
  46. if use_storage and storage is not None and "sources" in storage:
  47. print "[playstream] Restore sources from storage"
  48. sources = storage["sources"]
  49. # if use_storage and os.path.exists(storage_path): #"sources" in storage:
  50. #sources = pickle.load(open(storage_path,"rb"))
  51. else:
  52. print "[playstream] Create sources objects"
  53. sources = ContentSources.ContentSources(sources_directory)
  54. for source in sources.plugins:
  55. if not ("options" in dir(sources.plugins[source]) and sources.plugins[source].options): continue
  56. options = sources.plugins[source].options
  57. if not options: continue
  58. for option in options:
  59. key="%s_%s"%(source,option)
  60. if key in ("viaplay_device"): continue # exception list,
  61. value = plugin.get_setting(key)
  62. options[option] = value
  63. sources.plugins[source].options_write(options)
  64. prefix = ""
  65. @plugin.route(".+" )
  66. def main():
  67. global prefix
  68. prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc)
  69. plugin.set_content("movies")
  70. data = plugin.request.url.replace(prefix,"")
  71. data = urllib.unquote(data)
  72. sources.plugins["config"].read_streams()
  73. if not data:
  74. data = u"config::home"
  75. print "[playstream] processing data=%s"%data
  76. if sources.is_video(data):
  77. try:
  78. streams = sources.get_streams(data)
  79. except Exception,e:
  80. #xbmcgui.Dialog().ok("Error",unicode(e))
  81. plugin.notify(unicode(e),"Error",10000, xbmcgui.NOTIFICATION_ERROR)
  82. traceback.print_exc()
  83. return plugin.set_resolved_url(None)
  84. if streams:
  85. return play_video(streams)
  86. else:
  87. plugin.notify("No streams found!","Error",10000,xbmcgui.NOTIFICATION_ERROR)
  88. return plugin.set_resolved_url(None)
  89. else:
  90. if "{0}" in data:
  91. q = plugin.keyboard(default=None, heading="Search for", hidden=False)
  92. if isinstance(q,str):
  93. q = q.decode("utf8")
  94. if isinstance(data,str):
  95. data = data.decode("utf8")
  96. data = data.format(q)
  97. try:
  98. items = get_list(data)
  99. except Exception,e:
  100. plugin.notify(unicode(e),"Error",10000,xbmcgui.NOTIFICATION_ERROR)
  101. traceback.print_exc()
  102. return []
  103. #xbmc.executebuiltin('Container.SetViewMode(500)')
  104. plugin.set_view_mode(50)
  105. if use_storage and storage is not None:
  106. print "[playstream] Save sources to storage"
  107. storage["sources"] = sources
  108. storage.sync()
  109. return items
  110. def get_list(data):
  111. if isinstance(data,unicode):
  112. data = data.encode("utf8")
  113. content = sources.get_content(data)
  114. print "[playstream] %s items returned"%len(content)
  115. items = []
  116. for item in content:
  117. if item[1] == "back": continue
  118. title = item[0].decode("utf8") if isinstance(item[0],str) else item[0]
  119. data2 = item[1].decode("utf8") if isinstance(item[1],str) else item[1]
  120. is_playable = True if sources.is_video(item[1]) else False
  121. img = item[2].decode("utf8") if isinstance(item[2],str) else item[2]
  122. desc = item[3].decode("utf8") if isinstance(item[3],str) else item[3]
  123. context_menu = [
  124. ("Add to PlayStream playlist",
  125. u'RunScript(special://home/addons/%s/context_menu.py,"playlist","%s","%s","%s","%s")' % (
  126. plugin.id, title, data2, playlist, proxy_url)),
  127. ("Add to PlayStream favorites",
  128. u'RunScript(special://home/addons/%s/context_menu.py,"add","%s","%s","%s","%s")'%(
  129. plugin.id, title, data2 ,img, desc)),
  130. ("Delete from PlayStream favorites",
  131. u'RunScript(special://home/addons/%s/context_menu.py,"delete","%s","%s","%s","%s")' % (
  132. plugin.id, title, data2, img, desc)),
  133. ("Download",
  134. u'RunScript(special://home/addons/%s/context_menu.py,"download","%s","%s","%s","%s")' % (
  135. plugin.id, title, data2, img, desc)),
  136. ]
  137. items.append({
  138. "label": title,
  139. "path": prefix+data2,
  140. "thumbnail":thumb_data(img, is_playable),
  141. "info":{"plot":desc},
  142. "is_playable":is_playable,
  143. "context_menu": context_menu,
  144. })
  145. return items
  146. def play_video(streams):
  147. if len(streams)>1:
  148. slist = []
  149. for s in streams:
  150. slist.append("%s [%s,%s]"%(s["name"],s["quality"],s["lang"]))
  151. res = xbmcgui.Dialog().select("Select stream",slist) if not CLI_MODE else 0
  152. #res = xbmcgui.Dialog().contextmenu(slist) if not CLI_MODE else 0
  153. stream = streams[res]
  154. else:
  155. stream = streams[0]
  156. subfiles = []
  157. #stream = util.stream_chamge(stream)
  158. if use_proxy:
  159. if "resolver" in stream and stream["resolver"] in ("hqq","filmas") or \
  160. "surl" in stream and re.search("http*://(hqq|goo\.gl)",stream["surl"]) or \
  161. "lattelecom.tv/mobile-vod/" in stream["url"]: # TODO
  162. #re.search(r"http*://.+?lattelecom\.tv/.+?auth_token=[^=]+=", stream["url"]):
  163. stream["url"] = util.streamproxy_encode(stream["url"],stream["headers"],proxy_url)
  164. stream["headers"] = {}
  165. if stream["headers"]:
  166. hh = []
  167. for k in stream["headers"]:
  168. h = "%s=%s"%(k,urllib.quote(stream["headers"][k]))
  169. hh.append(h)
  170. hh = "&".join(hh)
  171. stream["url"] = stream["url"] +"|"+hh
  172. print "[playstream] play_video ", stream["url"]
  173. if "subs" in stream and stream["subs"]:
  174. for sub in stream["subs"]:
  175. suburl = sub["url"]
  176. subs = util.Captions(suburl)
  177. srt = subs.get_srt()
  178. #subfile = plugin.temp_fn("subtitles.srt")
  179. subfile = os.path.join(os.path.dirname(__file__),sub["lang"]+".srt")
  180. f = open(subfile, "w")
  181. f.write(srt)
  182. f.close()
  183. subfiles.append(subfile)
  184. item = ListItem(label=stream["name"], thumbnail=thumb_data(stream["img"], True), path=stream["url"])
  185. item.set_info("video",{"plot":stream["desc"]})
  186. item.set_is_playable(True)
  187. return plugin.set_resolved_url(item,subfiles)
  188. #return plugin.play_video(item)
  189. def thumb_data(img, video=False):
  190. default = "video.png" if video else "folder.png"
  191. if img in ("default", ""):
  192. img = default
  193. if not img.startswith("http"):
  194. img = icong_url + img
  195. return img
  196. if __name__ == '__main__':
  197. if CLI_MODE:
  198. from kodiswift.cli.cli import main as start
  199. start()
  200. else:
  201. plugin.run()
  202. if use_storage and storage is not None:
  203. print "[playstream] Save sources to storage"
  204. storage["sources"] = sources
  205. storage.sync()
  206. print "Save sources to storage"
  207. #pickle.dump(sources,open(storage_path,"wb"),pickle.HIGHEST_PROTOCOL)