Plex plugin to to play various online streams (mostly Latvian).

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. VERSION = "0.1.1"
  2. CHANNEL_NAME = "PlayStream"
  3. PREFIX = '/video/playstream'
  4. DEV = True if Platform.MachineIdentifier == 'Unit testing' else False
  5. ART = "art-default.jpg"
  6. ICON = "icon-default.png"
  7. BACK = "back.png"
  8. FOLDER = "folder.png"
  9. VIDEO = "video.png"
  10. SEARCH = "folder_search.png"
  11. PREFS = "prefs.png"
  12. import sys, os, re
  13. import traceback, glob, inspect, urllib
  14. import content
  15. from content.ContentSources import ContentSources
  16. from content import util
  17. import Framework
  18. #from Framework.api.objectkit import *
  19. from DumbTools import DumbKeyboard, DumbPrefs
  20. sources_directory = os.path.join(os.path.dirname(inspect.getsourcefile(content)), "sources")
  21. sources = ContentSources(sources_directory)
  22. data0 = None
  23. title0 = None
  24. history = []
  25. view_modes = {
  26. "List": 65586, "InfoList": 65592, "MediaPreview": 458803, "Showcase": 458810, "Coverflow": 65591,
  27. "PanelStream": 131124, "WallStream": 131125, "Songs": 65593, "Seasons": 65593, "Albums": 131123,
  28. "Episodes": 65590,"ImageStream":458809,"Pictures":131123
  29. }
  30. '''
  31. for source in sources.plugins:
  32. if not ("options" in dir(sources.plugins[source]) and sources.plugins[source].options): continue
  33. options = sources.plugins[source].options
  34. if not options: continue
  35. for option in options:
  36. key="%s_%s"%(source,option)
  37. if key in ("viaplay_device"): continue # exception list,
  38. value = plugin.get_setting(key)
  39. options[option] = value
  40. sources.plugins[source].options_write(options)
  41. prefix = ""
  42. '''
  43. ###################################################################################################
  44. def Start():
  45. ObjectContainer.title1 = CHANNEL_NAME
  46. ObjectContainer.art = R(ART)
  47. Plugin.AddViewGroup("InfoList", viewMode="InfoList", mediaType="items")
  48. Plugin.AddViewGroup("List", viewMode="List", mediaType="items")
  49. #DirectoryObject.thumb = R(ICON)
  50. #DirectoryObject.art = R(ART)
  51. #EpisodeObject.thumb = R(ICON)
  52. #EpisodeObject.art = R(ART)
  53. #VideoClipObject.thumb = R(ICON)
  54. #VideoClipObject.art = R(ART)
  55. #HTTP.CacheTime = CACHE_1DAY
  56. #HTTP.Headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
  57. def ValidatePrefs():
  58. #preferences = Prefs._sandbox.preferences.get()
  59. for source in sources.plugins:
  60. if not ("options" in dir(sources.plugins[source]) and sources.plugins[source].options): continue
  61. options = sources.plugins[source].options
  62. if not options: continue
  63. for option in options:
  64. key="%s_%s"%(source,option)
  65. if key in ("viaplay_device"): continue # exception list,
  66. value = Prefs[key]
  67. options[option] = value
  68. if not DEV:
  69. sources.plugins[source].options_write(options)
  70. ####################################################################################################
  71. @handler(PREFIX, CHANNEL_NAME, art=ART, thumb=ICON)
  72. def Main():
  73. data = "config::home"
  74. oc = Menu(data, "Home")
  75. oc.view_group = "InfoList"
  76. ValidatePrefs()
  77. return oc
  78. ####################################################################################################
  79. @route(PREFIX+'/{data}/')
  80. def Menu2(data, title):
  81. return Menu(data, title)
  82. @route(PREFIX+'/search')
  83. def Search(query, data, title):
  84. #data = data.replace("%2F", "/")
  85. Log("Search data="+data)
  86. Log("Search query="+query)
  87. data2 = data.format(query)
  88. return Menu(data2, title)
  89. @route(PREFIX+'/{data}')
  90. def Menu(data, title, **kwargs):
  91. #includeBandwidths=1, checkFiles=0, includeConcerts=0, includeExtras=0, includeOnDeck=0, includePopularLeaves=1&includeChapters=1&checkFiles=1):
  92. global data0, title0, history
  93. #sources.plugins["config"].read_streams()
  94. Log("[playstream] ** Menu call: %s - %s" % ( data, title))
  95. if not data:
  96. data = u"config::home"
  97. # Process history
  98. if data == "back":
  99. data = history[-1][0].replace("/", "%2F") if history and history[-1][0] else "config::home"
  100. title = history[-1][1] if history and history[-1][0] else "Home"
  101. if data == data0 or (len(history) > 2 and data == history[-2]):
  102. # fake call, skipped
  103. Log("history skipped")
  104. else:
  105. if history and history[-1][0] and data == history[-1][0]:
  106. history.pop()
  107. Log("history poped")
  108. else:
  109. history.append((data0, title0))
  110. Log("history added %s" % data0)
  111. data0 = data
  112. title0 = title
  113. data = data.replace("%2F", "/")
  114. hst = ""
  115. for h in history: hst += "\n%s" % h[0]
  116. #Log("[playstream] history="+hst)
  117. ### Processig call ###
  118. Log("[playstream] processing data=%s"%data)
  119. try:
  120. is_video = sources.is_video(data)
  121. except:
  122. Log(traceback.format_exc())
  123. return ObjectContainer(header="Error", message=unicode(e))
  124. ### Video handling ###
  125. if is_video:
  126. try:
  127. streams = sources.get_streams(data)
  128. except Exception,e:
  129. Log(traceback.format_exc())
  130. return ObjectContainer(header="Error", message=unicode(e))
  131. if streams:
  132. data2 = data.replace("/", "%2F")
  133. title = streams[0]["name"] if isinstance(streams[0]["name"] , unicode) else streams[0]["name"].decode("utf8")
  134. if not title: title = "Title"
  135. desc = streams[0]["desc"] if isinstance(streams[0]["desc"] , unicode) else streams[0]["desc"] .decode("utf8")
  136. img = streams[0]["img"] if isinstance(streams[0]["img"] , unicode) else streams[0]["img"] .decode("utf8")
  137. vco = VideoClipObject(
  138. key=Callback(Menu, data=data2, title=title ),
  139. rating_key=data2,
  140. title=title,
  141. summary=desc,
  142. thumb=thumb_data(img, video=True)
  143. )
  144. for stream in streams:
  145. vco.add(MediaObject(
  146. #bitraate=0,
  147. #container=Container.MPEGTS
  148. optimized_for_streaming = True,
  149. parts=[PartObject(key=HTTPLiveStreamURL(stream["url"]))]
  150. ))
  151. include_container = True
  152. return ObjectContainer(objects = [vco]) if include_container else vco
  153. else:
  154. return ObjectContainer(header="Error", message="No streams found!")
  155. ### List handling ###
  156. oc = ObjectContainer(title2=title)
  157. oc.view_group = "InfoList"
  158. try:
  159. content = sources.get_content(data)
  160. except Exception,e:
  161. Log(traceback.format_exc())
  162. return ObjectContainer(header="Error", message=unicode(e))
  163. Log( "[playstream] %s items returned"%len(content))
  164. for item in content:
  165. data2 = item[1]
  166. #data2 = urllib.quote(data2, safe="")
  167. data2 = data2.replace("/", "%2F")
  168. title = item[0] if isinstance(item[0], unicode) else item[0].decode("utf8")
  169. if not title: title = "Title"
  170. img = item[2] #if isinstance(item[2], unicode) else item[2].decode("utf8")
  171. desc = item[3] if isinstance(item[3], unicode) else item[3].decode("utf8")
  172. # Search item #
  173. if "{0}" in data2:
  174. #q = "aaa" # TODO InputDirectoryObject
  175. ##data = data.format(q)
  176. if Client.Product in DumbKeyboard.clients:
  177. DumbKeyboard(PREFIX, oc, Search,
  178. dktitle = title,
  179. dkthumb = thumb_data(SEARCH),
  180. title=title,
  181. data=data2
  182. )
  183. else:
  184. oc.add(InputDirectoryObject(
  185. key=Callback(Search, data=data2, title=title),
  186. title = title,
  187. thumb = thumb_data(img), #Resource.ContentsOfURLWithFallback(img, fallback=R(ICON)),
  188. prompt=desc,
  189. summary =desc
  190. ))
  191. # Video item #
  192. elif sources.is_video(item[1]):
  193. oc.add(VideoClipObject(
  194. key=Callback(Menu, data=data2, title=title),
  195. title = title,
  196. thumb = thumb_data(img, video=True), #Resource.ContentsOfURLWithFallback(img, fallback=R(ICON)),
  197. summary = desc,
  198. rating_key=data2
  199. ))
  200. # List object #
  201. else:
  202. oc.add(DirectoryObject(
  203. key=Callback(Menu, data=data2, title=title),
  204. title = title,
  205. thumb = thumb_data(img) if not data2=="back" else R(BACK),
  206. summary =desc
  207. ))
  208. if data == "config::home":
  209. if Client.Product in DumbPrefs.clients:
  210. DumbPrefs(PREFIX, oc,
  211. title = "Plugin options",
  212. thumb = R(PREFS))
  213. else:
  214. oc.add(PrefsObject(
  215. title="Plugin options",
  216. summary="Update plugin options",
  217. thumb=R(PREFS),
  218. art=R(ART)
  219. ))
  220. ValidatePrefs()
  221. return oc
  222. def thumb_data(img, video=False):
  223. default = R(VIDEO) if video else R(FOLDER)
  224. if img.startswith('http'):
  225. img2 = Resource.ContentsOfURLWithFallback(img, default)
  226. elif img in ("default", ""):
  227. img2 = default
  228. else:
  229. img2 = R(img)
  230. return img2