Play images and video from Synology PhotoStation server

addon.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import os,os.path,sys, urllib, urlparse
  2. from kodiswift import Plugin, ListItem, storage
  3. from kodiswift import xbmc, xbmcgui, xbmcplugin, xbmcvfs, xbmcaddon, CLI_MODE, SortMethod
  4. from resources.lib import photostation_api
  5. try:
  6. import wingdbstub
  7. except:
  8. pass
  9. plugin = Plugin()
  10. view_mode = plugin.get_setting("view_mode",str)
  11. server = plugin.get_setting("server", str)
  12. user = plugin.get_setting("user", str)
  13. password = plugin.get_setting("password", str)
  14. page_size = plugin.get_setting("page_size", str)
  15. page_size = -1 if page_size == "All" else int(page_size)
  16. sorting = plugin.get_setting("sorting", str)
  17. order = plugin.get_setting("order", str)
  18. video_quality = plugin.get_setting("video_quality", str)
  19. picture_quality = plugin.get_setting("picture_quality", str)
  20. ps = photostation_api.PhotoStationAPI("http://home.blue.lv/photo")
  21. try:
  22. ps.login(user, password)
  23. except Exception as e:
  24. print "Error while logging %s/%s"%(user,password)
  25. plugin.notify(str(e),"",10000)
  26. prefix = ""
  27. @plugin.route(".+" )
  28. def main():
  29. # plugin://xxx/
  30. global prefix
  31. prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc)
  32. data = plugin.request.url.replace(prefix,"")
  33. data = urllib.unquote(data)
  34. #default_oid = "album_323031372f323031372d30312d313320536c69646f73616e61"
  35. default_oid = ""
  36. if not data:
  37. data = default_oid
  38. oid = plugin.request.path[1:]
  39. if not oid:
  40. oid = default_oid
  41. qs = dict(urlparse.parse_qsl(plugin.request.query_string))
  42. if not "page" in qs:
  43. page = 1
  44. else:
  45. page = int(qs["page"])
  46. if not oid or oid.startswith("album"):
  47. plugin.set_content("images")
  48. offset = 0 if page_size == -1 else (page - 1) * page_size
  49. limit = -1 if page_size == -1 else page_size
  50. content = ps.get_album_list(oid, offset=offset, limit=limit, sort_by=sorting, sort_direction=order )
  51. items = []
  52. for f in content["items"]:
  53. title = f["info"]["name"]
  54. type = f["type"]
  55. oid2 = f["id"]
  56. #is_playable = True if type in ("video","photo") else False
  57. is_folder = False if type in ("video","photo") else True
  58. thumb_url = ps.get_thumb_url(f["id"],"small")
  59. data2 = prefix+f["id"]
  60. #item = ListItem(title, icon=thumb_url, thumbnail=thumb_url, path=data2)
  61. #item.is_folder = False if type in ("video","photo") else True
  62. if type=="album":
  63. label2 = "%s photos, %s videos"%(f["additional"]["item_count"]["photo"],f["additional"]["item_count"]["video"])
  64. item = xbmcgui.ListItem(title, label2, thumb_url, thumb_url, path=data2)
  65. item.setInfo(
  66. "Image",
  67. {"title":f["info"]["title"],"picturepath":f["additional"]["file_location"]}
  68. )
  69. elif type == "photo":
  70. label2 = f["info"]["takendate"]
  71. image_url = ps.get_thumb_url(oid2,"large")
  72. data2 = image_url
  73. item = xbmcgui.ListItem(title, label2, thumb_url, thumb_url, path=data2)
  74. item.setInfo(
  75. "pictures",
  76. {"title":f["info"]["title"],
  77. "picturepath":f["additional"]["file_location"],
  78. "exif:resolution": "720,480" ,}
  79. )
  80. elif type=="video":
  81. label2 = f["info"]["takendate"]
  82. item = xbmcgui.ListItem(title, label2, thumb_url, thumb_url, path=data2)
  83. item.setInfo(
  84. 'video', {
  85. "title": title,
  86. "plot": f["additional"]["file_location"],
  87. }
  88. )
  89. item.setProperty("is_folder", "true" if is_folder else "")
  90. item.setProperty("url", data2)
  91. items.append(item)
  92. #xbmcplugin.addDirectoryItem(handle=plugin.handle, url=data2, listitem=item, isFolder=is_folder,
  93. #totalItems=len(content["items"]))
  94. if page_size <> -1 and len(content["items"]) == page_size:
  95. data2 = prefix+oid+"?page=%s" % (page+1)
  96. item = xbmcgui.ListItem("Next", "", "", "", data2)
  97. item.setInfo("Image", {"title":"Next"})
  98. item.setProperty("is_folder", "false")
  99. item.setProperty("url", data2)
  100. items.append(item)
  101. sort_methods = [SortMethod.FILE, SortMethod.DATE, SortMethod.DATE_TAKEN]
  102. view_mode_id = get_view_mode(view_mode)
  103. return plugin.finish(items, sort_methods=None, view_mode=view_mode_id, cache_to_disc=True, update_listing=False)
  104. #xbmcplugin.endOfDirectory(plugin.handle, succeeded=True, updateListing=False, cacheToDisc=True)
  105. elif oid.startswith("photo"):
  106. print "play_photo ", oid
  107. try:
  108. url = ps.get_thumb_url(oid,"large")
  109. #thumb_url = ps.get_thumb_url(oid,"smalll")
  110. #js = ps.get_photo_info(oid)
  111. #title = js["info"]["name"]
  112. except Exception,e:
  113. plugin.notify(str(e),"",10000)
  114. return None
  115. print "set_resolved_url", url
  116. plugin.set_resolved_url(url)
  117. elif oid.startswith("video"):
  118. try:
  119. streams = ps.get_video_streams(oid)
  120. js = ps.get_photo_info(oid)
  121. except Exception,e:
  122. plugin.notify(str(e),"",10000)
  123. return None
  124. if streams:
  125. return play_video(streams)
  126. else:
  127. plugin.notify("No streams found!",10000)
  128. return None
  129. def get_view_mode(vm):
  130. modes = {
  131. "skin.estuary": {
  132. "None": None,
  133. "List": 50,
  134. "Poster": 51,
  135. "IconWall":52 ,
  136. "Shift": 53,
  137. "InfoWall": 54,
  138. "WideList": 55,
  139. "Wall": 500,
  140. "Banner": 501,
  141. "FanArt": 502
  142. }
  143. }
  144. skin = xbmc.getSkinDir()
  145. if skin in modes and vm in modes[skin]:
  146. view_mode = modes[skin][vm]
  147. else:
  148. view_mode = 50
  149. return view_mode
  150. def play_video(streams):
  151. stream = streams[0]
  152. subfiles = []
  153. print "play_video ",stream["url"]
  154. item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"])
  155. item.set_info("video",{"plot":stream["desc"]})
  156. item.is_folder = False
  157. item.set_is_playable(True)
  158. plugin.play_video(item)
  159. if __name__ == '__main__':
  160. if CLI_MODE:
  161. from kodiswift.cli.cli import main as start
  162. start()
  163. else:
  164. plugin.run()