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

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