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

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