# -*- coding: utf-8 -*- try: import wingdbstub except: pass import os,os.path,sys, urllib, urlparse #import xbmc, xbmcgui, xbmcplugin, xbmcvfs, xbmcaddon from kodiswift import Plugin, ListItem, storage from kodiswift import xbmc, xbmcgui, xbmcplugin, xbmcvfs, xbmcaddon, CLI_MODE, SortMethod from resources.lib import photostation_api plugin = Plugin() handle = sys.argv[1] view_mode = plugin.get_setting("view_mode",str) server = plugin.get_setting("server", str) user = plugin.get_setting("user", str) password = plugin.get_setting("password", str) page_size = plugin.get_setting("page_size", str) page_size = -1 if page_size == "All" else int(page_size) sorting = plugin.get_setting("sorting", str) order = plugin.get_setting("order", str) video_quality = plugin.get_setting("video_quality", str) picture_quality = plugin.get_setting("picture_quality", str) thumbnail_quality = plugin.get_setting("thumbnail_quality", str) play_next = plugin.get_setting("play_next", bool) start_page = plugin.get_setting("start_page", str) ps = photostation_api.PhotoStationAPI("http://home.blue.lv/photo") try: ps.login(user, password) except Exception as e: print "Error while logging %s/%s"%(user,password) plugin.notify(str(e),"",10000) prefix = "" @plugin.route(".+" ) def main(): # plugin://xxx/ global prefix prefix = "%s://%s/"%(plugin.request.scheme,plugin.request.netloc) data = plugin.request.url.replace(prefix,"") data = urllib.unquote(data) #default_oid = "album_323031372f323031372d30312d313320536c69646f73616e61" default_oid = "" #start_page if not data: data = default_oid oid = plugin.request.path[1:] if not oid: oid = default_oid qs = dict(urlparse.parse_qsl(plugin.request.query_string)) if not "page" in qs: page = 1 else: page = int(qs["page"]) if not oid or oid.startswith("album") or oid.startswith("category"): plugin.set_content("images") content = [] if not oid: categories = ps.get_categories()["categories"] for cat in categories: if cat["hidden"]: continue data2 = prefix + cat["id"] label = cat["name"] item = xbmcgui.ListItem(label, "", "", "", data2) item.setInfo("Image", {"title":label}) item.setProperty("is_folder", "true") item.setProperty("url", data2) #items.append(item) xbmcplugin.addDirectoryItem(handle=plugin.handle, url=data2, listitem=item, isFolder=True) offset = 0 if page_size == -1 else (page - 1) * page_size limit = -1 if page_size == -1 else page_size if "category" in oid: content = ps.get_category_list(oid) else: content = ps.get_album_list(oid, offset=offset, limit=limit, sort_by=sorting, sort_direction=order ) items = [] for f in content["items"]: label = f["info"]["title"] label2 = f["additional"]["file_location"] if "file_location" in f["additional"] else "" type = f["type"] oid2 = f["id"] is_playable = "true" if type in ("video","photo") else "" is_folder = "" if type in ("video","photo") else "true" thumb_quality = thumbnail_quality if thumbnail_quality in f["thumbnail_status"] else "small" thumb_url = ps.get_thumb_url(f["id"],thumb_quality) #thumb_url = urllib.quote(thumb_url, ":/") data2 = prefix+f["id"] if type=="album": nfiles = int(f["additional"]["item_count"]["photo"]) + int(f["additional"]["item_count"]["video"]) if "item_count" in f["additional"] else None suf = "\n(%s)" % nfiles if nfiles else "" label2 = "%s photos, %s videos"%(f["additional"]["item_count"]["photo"],f["additional"]["item_count"]["video"]) if "item_count" in f["additional"] else "" label = label + suf li_type = "video" # pictures tags = { #"title":f["info"]["title"], "title": label, #"picturepath":f["additional"]["file_location"] "label": label, "plotline":f["additional"]["file_location"] + "\n" + label2 if "file_location" in f["additional"] else "", "plot":f["additional"]["file_location"] + "\n" + label2 if "file_location" in f["additional"] else "", "path": f["info"]["sharepath"] if "sharepath" in f["additional"] else "", } elif type == "photo": label = label + "\n" + f["info"]["takendate"] label2 = f["info"]["takendate"] image_url = ps.get_photo_url(oid2) if picture_quality == "original" else ps.get_thumb_url(oid2, picture_quality) data2 = image_url #print "image_url=", data2 #data2 = urllib.quote(data2, ":/") li_type = "pictures" tags = { "title": label2, #f["info"]["title"], "date": f["info"]["takendate"], "picturepath": f["additional"]["file_location"], "exif:resolution": "%s,%s" % (f["info"]["resolutionx"], f["info"]["resolutiony"]), #"exif:path": f["additional"]["file_location"], #"exif:name": f["info"]["name"], #"exif:filename": f["additional"]["file_location"], #"exif:" # TODO pārejie EXIF u.c. dati } elif type=="video": label = label + "\n" + f["info"]["takendate"] label2 = f["info"]["takendate"] li_type = 'video' tags = { "title": label , "plot": f["additional"]["file_location"], #TODO } if play_next: data2 = data2 + "&album=" + oid if "?" in data2 else data2 + "?album=" + oid #data2 = urllib.quote(data2, ":/") is_playable = "" else: if video_quality == "original": quality_id = "" else: quality_id = f["additional"]["video_quality"][0]["id"] data2 = ps.get_video_url(oid2, quality_id) #data2 = urllib.quote(data2, ":/") item = xbmcgui.ListItem(label=label, label2=label2, path=data2) item.setArt({ "thumb": thumb_url, "icon": thumb_url, "fanart": thumb_url, }) item.setInfo(li_type, tags) item.setProperty("is_folder", is_folder) item.setProperty('IsPlayable', is_playable) item.setProperty("url", data2) #items.append(item) xbmcplugin.addDirectoryItem(handle=plugin.handle, url=data2, listitem=item, isFolder=True if is_folder=="true" else False, totalItems=len(content["items"])) if page_size <> -1 and len(content["items"]) == page_size: data2 = prefix+oid+"?page=%s" % (page+1) #data2 = urllib.quote(data2, ":/") item = xbmcgui.ListItem("Next", "", "", "", data2) item.setInfo("Image", {"title":"Next"}) item.setProperty("is_folder", "false") item.setProperty("url", data2) #items.append(item) xbmcplugin.addDirectoryItem(handle=plugin.handle, url=data2, listitem=item, isFolder=True, totalItems=len(content["items"])) view_mode_id = get_view_mode(view_mode) xbmc.executebuiltin('Container.SetViewMode(%s)' % view_mode_id) #sort_methods = [SortMethod.FILE, SortMethod.DATE, SortMethod.DATE_TAKEN] #xbmcplugin.addSortMethod( plugin.handle, sortMethod=xbmcplugin.SORT_METHOD_GENRE ) xbmcplugin.endOfDirectory(plugin.handle, succeeded=True, updateListing=False, cacheToDisc=True) #return plugin.finish(items, sort_methods=None, view_mode=view_mode_id, cache_to_disc=True, update_listing=False) elif oid.startswith("photo"): print "play_photo ", oid try: url = ps.get_thumb_url(oid,"large") #thumb_url = ps.get_thumb_url(oid,"smalll") #js = ps.get_photo_info(oid) #title = js["info"]["name"] except Exception,e: plugin.notify(str(e),"",10000) return None print "set_resolved_url", url plugin.set_resolved_url(url) elif oid.startswith("video"): if play_next and "album" in qs: # play playlist print "play_playlist", data, qs album_id = qs["album"] try: items = ps.get_album_list(album_id) except Exception,e: plugin.notify(str(e),"",10000) return None if items: play_playlist(items["items"], oid) return None else: plugin.notify("No items found!",10000) return None else: # play one video print "play_video" try: streams = ps.get_video_streams(oid) js = ps.get_photo_info(oid) except Exception,e: plugin.notify(str(e),"",10000) return None if streams: play_video(streams) return None else: plugin.notify("No streams found!",10000) return None def get_view_mode(vm): modes = { "skin.estuary": { "None": None, "List": 50, "Poster": 51, "IconWall":52 , "Shift": 53, "InfoWall": 54, "WideList": 55, "Wall": 500, "Banner": 501, "FanArt": 502 } } skin = xbmc.getSkinDir() if skin in modes and vm in modes[skin]: view_mode = modes[skin][vm] else: view_mode = 500 return view_mode def play_playlist(items, oid): playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO) playlist.clear() pos = -1 i = 0 for f in items: if f["type"] in ("album","photo"): continue label = f["info"]["name"] + "\n" + f["info"]["takendate"] label2 = f["additional"]["file_location"] type = f["type"] oid2 = f["id"] if oid == oid2: pos = i #is_playable = True if type in ("video","photo") else False is_folder = False if type in ("video","photo") else True thumb_quality = thumbnail_quality if thumbnail_quality in f["thumbnail_status"] else "small" thumb_url = ps.get_thumb_url(f["id"],thumb_quality) data2 = prefix+f["id"] item = xbmcgui.ListItem(label, label2, path=data2) item.setArt({ "thumb": thumb_url, "icon": thumb_url, "fanart": thumb_url, }) item.setLabel2(label2) item.setInfo( 'video', { "title": label, "plot": f["additional"]["file_location"], #TODO } ) if video_quality == "original": quality_id = "" else: quality_id = f["additional"]["video_quality"][0]["id"] data2 = ps.get_video_url(oid2, quality_id) #data2 = urllib.quote(data2, ":/") item.setProperty("is_folder", "") item.setProperty('IsPlayable', 'true') #item.setPath(data2) item.setProperty("url", data2) playlist.add(data2, item) i += 1 xbmc.Player().play(playlist, startpos=pos) def play_video(streams): stream = streams[0] subfiles = [] print "play_video ",stream["url"] item = ListItem(label=stream["name"], thumbnail=stream["img"], path=stream["url"]) item.set_info("video",{"plot":stream["desc"]}) item.is_folder = False #item.set_is_playable(True) #plugin.play_video(item) item2 = xbmcgui.ListItem("label", "label2", path=stream["url"]) item2.setProperty('IsPlayable', 'true') xbmc.Player().play(item=stream["url"], listitem=item2) #xbmcplugin.setResolvedUrl(plugin.handle, True, listitem=item2) if __name__ == '__main__': if CLI_MODE: from kodiswift.cli.cli import main as start start() else: plugin.run()