diff options
Diffstat (limited to 'plugin.video.embycon/resources/lib/utils.py')
-rw-r--r-- | plugin.video.embycon/resources/lib/utils.py | 186 |
1 files changed, 182 insertions, 4 deletions
diff --git a/plugin.video.embycon/resources/lib/utils.py b/plugin.video.embycon/resources/lib/utils.py index 9c6a8d3..b91aa59 100644 --- a/plugin.video.embycon/resources/lib/utils.py +++ b/plugin.video.embycon/resources/lib/utils.py @@ -1,11 +1,23 @@ # Gnu General Public License - see LICENSE.TXT import xbmcaddon +import xbmcgui +import xbmcplugin +import xbmc import re import encodings +import string +import random +import urllib +import json +import httplib +import base64 +import sys from downloadutils import DownloadUtils from simple_logging import SimpleLogging from clientinfo import ClientInformation +from json_rpc import json_rpc +from translation import i18n # define our global download utils downloadUtils = DownloadUtils() @@ -14,7 +26,7 @@ log = SimpleLogging(__name__) ########################################################################### class PlayUtils(): - def getPlayUrl(self, id, result, force_transcode): + def getPlayUrl(self, id, result, force_transcode, play_session_id): log.debug("getPlayUrl") addonSettings = xbmcaddon.Addon(id='plugin.video.embycon') playback_type = addonSettings.getSetting("playback_type") @@ -23,6 +35,7 @@ class PlayUtils(): if force_transcode: log.debug("playback_type: FORCED_TRANSCODE") playurl = None + log.debug("play_session_id: " + play_session_id) is_h265 = False streams = result.get("MediaStreams", []) @@ -58,8 +71,8 @@ class PlayUtils(): user_token = downloadUtils.authenticate() playurl = ( - "%s/emby/Videos/%s/master.m3u8?MediaSourceId=%s&VideoCodec=h264&AudioCodec=ac3&MaxAudioChannels=6&deviceId=%s&VideoBitrate=%s" - % (server, id, id, deviceId, bitrate)) + "%s/emby/Videos/%s/master.m3u8?MediaSourceId=%s&PlaySessionId=%s&VideoCodec=h264&AudioCodec=ac3&MaxAudioChannels=6&deviceId=%s&VideoBitrate=%s" + % (server, id, id, play_session_id, deviceId, bitrate)) playurl = playurl + "&maxWidth=" + playback_max_width @@ -91,7 +104,7 @@ class PlayUtils(): # do direct http streaming playback elif playback_type == "1": - playurl = "%s/emby/Videos/%s/stream?static=true" % (server, id) + playurl = "%s/emby/Videos/%s/stream?static=true&PlaySessionId=%s" % (server, id, play_session_id) user_token = downloadUtils.authenticate() playurl = playurl + "&api_key=" + user_token @@ -220,3 +233,168 @@ def getArt(item, server, widget=False): art['discart'] = downloadUtils.getArtwork(item, "Disc", server=server) return art + + +def id_generator(size=6, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for _ in range(size)) + + +def cache_artwork(): + log.debug("cache_artwork") + + xbmcplugin.endOfDirectory(int(sys.argv[1]), cacheToDisc=False) + + # is the web server enabled + web_query = {"setting": "services.webserver"} + result = json_rpc('Settings.GetSettingValue').execute(web_query) + xbmc_webserver_enabled = result['result']['value'] + if not xbmc_webserver_enabled: + xbmcgui.Dialog().ok(i18n('notice'), i18n('http_control')) + return + + # get the port + web_port = {"setting": "services.webserverport"} + result = json_rpc('Settings.GetSettingValue').execute(web_port) + xbmc_port = result['result']['value'] + log.debug("xbmc_port: " + str(xbmc_port)) + + # get the user + web_user = {"setting": "services.webserverusername"} + result = json_rpc('Settings.GetSettingValue').execute(web_user) + xbmc_username = result['result']['value'] + log.debug("xbmc_username: " + str(xbmc_username)) + + # get the password + web_pass = {"setting": "services.webserverpassword"} + result = json_rpc('Settings.GetSettingValue').execute(web_pass) + xbmc_password = result['result']['value'] + + # ask to delete all textures + question_result = xbmcgui.Dialog().yesno(i18n('delete'), i18n('delete_existing')) + if question_result: + pdialog = xbmcgui.DialogProgress() + pdialog.create(i18n('deleting_textures'), "") + index = 0 + + json_result = json_rpc('Textures.GetTextures').execute() + textures = json_result.get("result", {}).get("textures", []) + log.debug("texture ids: " + str(textures)) + total = len(textures) + for texture in textures: + texture_id = texture["textureid"] + params = {"textureid": int(texture_id)} + json_result = json_rpc('Textures.RemoveTexture').execute(params) + percentage = int((float(index) / float(total)) * 100) + message = "%s of %s" % (index, total) + pdialog.update(percentage, "%s" % (message)) + + index += 1 + if pdialog.iscanceled(): + break + + del textures + del pdialog + + question_result = xbmcgui.Dialog().yesno(i18n('cache_all_textures_title'), i18n('cache_all_textures')) + if not question_result: + return + + params = {"properties": ["url"]} + json_result = json_rpc('Textures.GetTextures').execute(params) + textures = json_result.get("result", {}).get("textures", []) + + texture_urls = set() + for texture in textures: + url = texture.get("url") + url = urllib.unquote(url) + url = url.replace("image://", "") + url = url[0:-1] + texture_urls.add(url) + + del textures + del json_result + + url = ('{server}/emby/Users/{userid}/Items?' + + '&Recursive=true' + + '&IncludeItemTypes=Movie,Series,Episode,BoxSet' + + '&ImageTypeLimit=1' + + '&format=json') + + results = downloadUtils.downloadUrl(url, method="GET") + if results is None: + results = [] + else: + results = json.loads(results) + + if isinstance(results, dict): + results = results.get("Items") + + server = downloadUtils.getServer() + missing_texture_urls = set() + + image_types = ["thumb", "poster", "banner", "clearlogo", "tvshow.poster", "tvshow.banner", "tvshow.landscape"] + for item in results: + art = getArt(item, server) + for image_type in image_types: + image_url = art[image_type] + if image_url not in texture_urls and not image_url.endswith("&Tag=") and len(image_url) > 0: + missing_texture_urls.add(image_url) + + log.debug("texture_urls:" + str(texture_urls)) + log.debug("missing_texture_urls: " + str(missing_texture_urls)) + log.debug("Number of existing textures: %s" % len(texture_urls)) + log.debug("Number of missing textures: %s" % len(missing_texture_urls)) + + kodi_http_server = "localhost:" + str(xbmc_port) + headers = {} + if xbmc_password: + auth = "%s:%s" % (xbmc_username, xbmc_password) + headers = {'Authorization': 'Basic %s' % base64.b64encode(auth)} + + pdialog = xbmcgui.DialogProgress() + pdialog.create(i18n('caching_textures'), "") + total = len(missing_texture_urls) + index = 1 + + count_done = 0 + for get_url in missing_texture_urls: + log.debug("texture_url:" + get_url) + url = double_urlencode(get_url) + kodi_texture_url = ("/image/image://%s" % url) + log.debug("kodi_texture_url: " + kodi_texture_url) + + percentage = int((float(index) / float(total)) * 100) + message = "%s of %s" % (index, total) + pdialog.update(percentage, "%s" % (message)) + + conn = httplib.HTTPConnection(kodi_http_server, timeout=20) + conn.request(method="GET", url=kodi_texture_url, headers=headers) + data = conn.getresponse() + if data.status == 200: + count_done += 1 + log.debug("Get Image Result: " + str(data.status)) + + index += 1 + if pdialog.iscanceled(): + break + + pdialog.close() + del pdialog + + report_text = i18n('existing_textures') + str(len(texture_urls)) + "\n" + report_text += i18n('missing_textures') + str(len(missing_texture_urls)) + "\n" + report_text += i18n('loaded_textures') + str(count_done) + xbmcgui.Dialog().ok(i18n('done'), report_text) + + +def double_urlencode(text): + text = single_urlencode(text) + text = single_urlencode(text) + return text + + +def single_urlencode(text): + # urlencode needs a utf- string + text = urllib.urlencode({'blahblahblah': text.encode('utf-8')}) + text = text[13:] + return text.decode('utf-8') #return the result again as unicode |