Merge pull request #177 from angelblue05/develop

Adjust client api
This commit is contained in:
angelblue05 2019-01-22 21:36:13 -06:00 committed by GitHub
commit abf4cad69e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 254 additions and 251 deletions

View file

@ -17,6 +17,7 @@ LOG = logging.getLogger('Emby.'+__name__)
################################################################################################# #################################################################################################
def callback(message, data): def callback(message, data):
''' Callback function should received message, data ''' Callback function should received message, data
message: string message: string
data: json dictionary data: json dictionary
@ -35,8 +36,7 @@ class EmbyClient(object):
self.http = HTTP(self) self.http = HTTP(self)
self.wsc = WSClient(self) self.wsc = WSClient(self)
self.auth = ConnectionManager(self) self.auth = ConnectionManager(self)
self.emby = api self.emby = api.API(self.http)
self.emby.client = self.http
self.callback_ws = callback self.callback_ws = callback
self.callback = callback self.callback = callback
@ -100,8 +100,6 @@ class EmbyClient(object):
return self.auth.__shortcuts__(key.replace('auth/', "", 1)) return self.auth.__shortcuts__(key.replace('auth/', "", 1))
elif key.startswith('api'): elif key.startswith('api'):
self.emby.client = self.http # Since api is not a class, re-assign global var to correct http adapter
return self.emby return self.emby
elif key == 'connected': elif key == 'connected':

View file

@ -2,25 +2,7 @@
################################################################################################# #################################################################################################
client = None def emby_url(client, handler):
#################################################################################################
def _http(action, url, request={}):
request.update({'type': action, 'handler': url})
return client.request(request)
def _get(handler, params=None):
return _http("GET", handler, {'params': params})
def _post(handler, json=None, params=None):
return _http("POST", handler, {'params': params, 'json': json})
def _delete(handler, params=None):
return _http("DELETE", handler, {'params': params})
def emby_url(handler):
return "%s/emby/%s" % (client.config['auth.server'], handler) return "%s/emby/%s" % (client.config['auth.server'], handler)
def basic_info(): def basic_info():
@ -45,106 +27,129 @@ def music_info():
################################################################################################# #################################################################################################
# Bigger section of the Emby api class API(object):
################################################################################################# ''' All the api calls to the server.
'''
def __init__(self, client, *args, **kwargs):
self.client = client
def try_server(): def _http(self, action, url, request={}):
return _get("System/Info/Public") request.update({'type': action, 'handler': url})
def sessions(handler="", action="GET", params=None, json=None): return self.client.request(request)
def _get(self, handler, params=None):
return self._http("GET", handler, {'params': params})
def _post(self, handler, json=None, params=None):
return self._http("POST", handler, {'params': params, 'json': json})
def _delete(self, handler, params=None):
return self._http("DELETE", handler, {'params': params})
#################################################################################################
# Bigger section of the Emby api
#################################################################################################
def try_server(self):
return self._get("System/Info/Public")
def sessions(self, handler="", action="GET", params=None, json=None):
if action == "POST": if action == "POST":
return _post("Sessions%s" % handler, json, params) return self._post("Sessions%s" % handler, json, params)
elif action == "DELETE": elif action == "DELETE":
return _delete("Sessions%s" % handler, params) return self._delete("Sessions%s" % handler, params)
else: else:
return _get("Sessions%s" % handler, params) return self._get("Sessions%s" % handler, params)
def users(handler="", action="GET", params=None, json=None): def users(self, handler="", action="GET", params=None, json=None):
if action == "POST": if action == "POST":
return _post("Users/{UserId}%s" % handler, json, params) return self._post("Users/{UserId}%s" % handler, json, params)
elif action == "DELETE": elif action == "DELETE":
return _delete("Users/{UserId}%s" % handler, params) return self._delete("Users/{UserId}%s" % handler, params)
else: else:
return _get("Users/{UserId}%s" % handler, params) return self._get("Users/{UserId}%s" % handler, params)
def items(handler="", action="GET", params=None, json=None): def items(self, handler="", action="GET", params=None, json=None):
if action == "POST": if action == "POST":
return _post("Items%s" % handler, json, params) return self._post("Items%s" % handler, json, params)
elif action == "DELETE": elif action == "DELETE":
return _delete("Items%s" % handler, params) return self._delete("Items%s" % handler, params)
else: else:
return _get("Items%s" % handler, params) return self._get("Items%s" % handler, params)
def user_items(handler="", params=None): def user_items(self, handler="", params=None):
return users("/Items%s" % handler, params=params) return self.users("/Items%s" % handler, params=params)
def shows(handler, params): def shows(self, handler, params):
return _get("Shows%s" % handler, params) return self._get("Shows%s" % handler, params)
def videos(handler): def videos(self, handler):
return _get("Videos%s" % handler) return self._get("Videos%s" % handler)
def artwork(item_id, art, max_width, ext="jpg", index=None): def artwork(self, item_id, art, max_width, ext="jpg", index=None):
if index is None: if index is None:
return emby_url("Items/%s/Images/%s?MaxWidth=%s&format=%s" % (item_id, art, max_width, ext)) return emby_url(self.client, "Items/%s/Images/%s?MaxWidth=%s&format=%s" % (item_id, art, max_width, ext))
return emby_url("Items/%s/Images/%s/%s?MaxWidth=%s&format=%s" % (item_id, art, index, max_width, ext)) return emby_url(self.client, "Items/%s/Images/%s/%s?MaxWidth=%s&format=%s" % (item_id, art, index, max_width, ext))
################################################################################################# #################################################################################################
# More granular api # More granular api
################################################################################################# #################################################################################################
def get_users(): def get_users(self):
return _get("Users") return self._get("Users")
def get_public_users(): def get_public_users(self):
return _get("Users/Public") return self._get("Users/Public")
def get_user(user_id=None): def get_user(self, user_id=None):
return users() if user_id is None else _get("Users/%s" % user_id) return self.users() if user_id is None else self._get("Users/%s" % user_id)
def get_views(): def get_views(self):
return users("/Views") return self.users("/Views")
def get_media_folders(): def get_media_folders(self):
return users("/Items") return self.users("/Items")
def get_item(item_id): def get_item(self, item_id):
return users("/Items/%s" % item_id) return self.users("/Items/%s" % item_id)
def get_items(item_ids): def get_items(self, item_ids):
return users("/Items", params={ return self.users("/Items", params={
'Ids': ','.join(str(x) for x in item_ids), 'Ids': ','.join(str(x) for x in item_ids),
'Fields': info() 'Fields': info()
}) })
def get_sessions(): def get_sessions(self):
return sessions(params={'ControllableByUserId': "{UserId}"}) return self.sessions(params={'ControllableByUserId': "{UserId}"})
def get_device(device_id): def get_device(self, device_id):
return sessions(params={'DeviceId': device_id}) return self.sessions(params={'DeviceId': device_id})
def post_session(session_id, url, params=None, data=None): def post_session(self, session_id, url, params=None, data=None):
return sessions("/%s/%s" % (session_id, url), "POST", params, data) return self.sessions("/%s/%s" % (session_id, url), "POST", params, data)
def get_images(item_id): def get_images(self, item_id):
return items("/%s/Images" % item_id) return self.items("/%s/Images" % item_id)
def get_suggestion(media="Movie,Episode", limit=1): def get_suggestion(self, media="Movie,Episode", limit=1):
return users("/Suggestions", { return self.users("/Suggestions", {
'Type': media, 'Type': media,
'Limit': limit 'Limit': limit
}) })
def get_recently_added(media=None, parent_id=None, limit=20): def get_recently_added(self, media=None, parent_id=None, limit=20):
return user_items("/Latest", { return self.user_items("/Latest", {
'Limit': limit, 'Limit': limit,
'UserId': "{UserId}", 'UserId': "{UserId}",
'IncludeItemTypes': media, 'IncludeItemTypes': media,
@ -152,37 +157,37 @@ def get_recently_added(media=None, parent_id=None, limit=20):
'Fields': info() 'Fields': info()
}) })
def get_next(index=None, limit=1): def get_next(self, index=None, limit=1):
return shows("/NextUp", { return self.shows("/NextUp", {
'Limit': limit, 'Limit': limit,
'UserId': "{UserId}", 'UserId': "{UserId}",
'StartIndex': None if index is None else int(index) 'StartIndex': None if index is None else int(index)
}) })
def get_adjacent_episodes(show_id, item_id): def get_adjacent_episodes(self, show_id, item_id):
return shows("/%s/Episodes" % show_id, { return self.shows("/%s/Episodes" % show_id, {
'UserId': "{UserId}", 'UserId': "{UserId}",
'AdjacentTo': item_id, 'AdjacentTo': item_id,
'Fields': "Overview" 'Fields': "Overview"
}) })
def get_genres(parent_id=None): def get_genres(self, parent_id=None):
return _get("Genres", { return self._get("Genres", {
'ParentId': parent_id, 'ParentId': parent_id,
'UserId': "{UserId}", 'UserId': "{UserId}",
'Fields': info() 'Fields': info()
}) })
def get_recommendation(parent_id=None, limit=20): def get_recommendation(self, parent_id=None, limit=20):
return _get("Movies/Recommendations", { return self._get("Movies/Recommendations", {
'ParentId': parent_id, 'ParentId': parent_id,
'UserId': "{UserId}", 'UserId': "{UserId}",
'Fields': info(), 'Fields': info(),
'Limit': limit 'Limit': limit
}) })
def get_items_by_letter(parent_id=None, media=None, letter=None): def get_items_by_letter(self, parent_id=None, media=None, letter=None):
return user_items(params={ return self.user_items(params={
'ParentId': parent_id, 'ParentId': parent_id,
'NameStartsWith': letter, 'NameStartsWith': letter,
'Fields': info(), 'Fields': info(),
@ -190,63 +195,63 @@ def get_items_by_letter(parent_id=None, media=None, letter=None):
'IncludeItemTypes': media 'IncludeItemTypes': media
}) })
def get_channels(): def get_channels(self):
return _get("LiveTv/Channels", { return self._get("LiveTv/Channels", {
'UserId': "{UserId}", 'UserId': "{UserId}",
'EnableImages': True, 'EnableImages': True,
'EnableUserData': True 'EnableUserData': True
}) })
def get_intros(item_id): def get_intros(self, item_id):
return user_items("/%s/Intros" % item_id) return self.user_items("/%s/Intros" % item_id)
def get_additional_parts(item_id): def get_additional_parts(self, item_id):
return videos("/%s/AdditionalParts" % item_id) return self.videos("/%s/AdditionalParts" % item_id)
def delete_item(item_id): def delete_item(self, item_id):
return items("/%s" % item_id, "DELETE") return self.items("/%s" % item_id, "DELETE")
def get_local_trailers(item_id): def get_local_trailers(self, item_id):
return user_items("/%s/LocalTrailers" % item_id) return self.user_items("/%s/LocalTrailers" % item_id)
def get_transcode_settings(): def get_transcode_settings(self):
return _get('System/Configuration/encoding') return self._get('System/Configuration/encoding')
def get_ancestors(item_id): def get_ancestors(self, item_id):
return items("/%s/Ancestors" % item_id, params={ return self.items("/%s/Ancestors" % item_id, params={
'UserId': "{UserId}" 'UserId': "{UserId}"
}) })
def get_items_theme_video(parent_id): def get_items_theme_video(self, parent_id):
return users("/Items", params={ return self.users("/Items", params={
'HasThemeVideo': True, 'HasThemeVideo': True,
'ParentId': parent_id 'ParentId': parent_id
}) })
def get_themes(item_id): def get_themes(self, item_id):
return items("/%s/ThemeMedia" % item_id, params={ return self.items("/%s/ThemeMedia" % item_id, params={
'UserId': "{UserId}", 'UserId': "{UserId}",
'InheritFromParent': True 'InheritFromParent': True
}) })
def get_items_theme_song(parent_id): def get_items_theme_song(self, parent_id):
return users("/Items", params={ return self.users("/Items", params={
'HasThemeSong': True, 'HasThemeSong': True,
'ParentId': parent_id 'ParentId': parent_id
}) })
def get_plugins(): def get_plugins(self):
return _get("Plugins") return self._get("Plugins")
def get_seasons(show_id): def get_seasons(self, show_id):
return shows("/%s/Seasons" % show_id, params={ return self.shows("/%s/Seasons" % show_id, params={
'UserId': "{UserId}", 'UserId': "{UserId}",
'EnableImages': True, 'EnableImages': True,
'Fields': info() 'Fields': info()
}) })
def get_date_modified(date, parent_id, media=None): def get_date_modified(self, date, parent_id, media=None):
return users("/Items", params={ return self.users("/Items", params={
'ParentId': parent_id, 'ParentId': parent_id,
'Recursive': False, 'Recursive': False,
'IsMissing': False, 'IsMissing': False,
@ -256,8 +261,8 @@ def get_date_modified(date, parent_id, media=None):
'Fields': info() 'Fields': info()
}) })
def get_userdata_date_modified(date, parent_id, media=None): def get_userdata_date_modified(self, date, parent_id, media=None):
return users("/Items", params={ return self.users("/Items", params={
'ParentId': parent_id, 'ParentId': parent_id,
'Recursive': True, 'Recursive': True,
'IsMissing': False, 'IsMissing': False,
@ -267,8 +272,8 @@ def get_userdata_date_modified(date, parent_id, media=None):
'Fields': info() 'Fields': info()
}) })
def refresh_item(item_id): def refresh_item(self, item_id):
return items("/%s/Refresh" % item_id, "POST", json={ return self.items("/%s/Refresh" % item_id, "POST", json={
'Recursive': True, 'Recursive': True,
'ImageRefreshMode': "FullRefresh", 'ImageRefreshMode': "FullRefresh",
'MetadataRefreshMode': "FullRefresh", 'MetadataRefreshMode': "FullRefresh",
@ -276,48 +281,48 @@ def refresh_item(item_id):
'ReplaceAllMetadata': True 'ReplaceAllMetadata': True
}) })
def favorite(item_id, option=True): def favorite(self, item_id, option=True):
return users("/FavoriteItems/%s" % item_id, "POST" if option else "DELETE") return self.users("/FavoriteItems/%s" % item_id, "POST" if option else "DELETE")
def get_system_info(): def get_system_info(self):
return _get("System/Configuration") return self._get("System/Configuration")
def post_capabilities(data): def post_capabilities(self, data):
return sessions("/Capabilities/Full", "POST", json=data) return self.sessions("/Capabilities/Full", "POST", json=data)
def session_add_user(session_id, user_id, option=True): def session_add_user(self, session_id, user_id, option=True):
return sessions("/%s/Users/%s" % (session_id, user_id), "POST" if option else "DELETE") return self.sessions("/%s/Users/%s" % (session_id, user_id), "POST" if option else "DELETE")
def session_playing(data): def session_playing(self, data):
return sessions("/Playing", "POST", json=data) return self.sessions("/Playing", "POST", json=data)
def session_progress(data): def session_progress(self, data):
return sessions("/Playing/Progress", "POST", json=data) return self.sessions("/Playing/Progress", "POST", json=data)
def session_stop(data): def session_stop(self, data):
return sessions("/Playing/Stopped", "POST", json=data) return self.sessions("/Playing/Stopped", "POST", json=data)
def item_played(item_id, watched): def item_played(self, item_id, watched):
return users("/PlayedItems/%s" % item_id, "POST" if watched else "DELETE") return self.users("/PlayedItems/%s" % item_id, "POST" if watched else "DELETE")
def get_sync_queue(date, filters=None): def get_sync_queue(self, date, filters=None):
return _get("Emby.Kodi.SyncQueue/{UserId}/GetItems", params={ return self._get("Emby.Kodi.SyncQueue/{UserId}/GetItems", params={
'LastUpdateDT': date, 'LastUpdateDT': date,
'filter': filters or None 'filter': filters or None
}) })
def get_server_time(): def get_server_time(self):
return _get("Emby.Kodi.SyncQueue/GetServerDateTime") return self._get("Emby.Kodi.SyncQueue/GetServerDateTime")
def get_play_info(item_id, profile): def get_play_info(self, item_id, profile):
return items("/%s/PlaybackInfo" % item_id, "POST", json={ return self.items("/%s/PlaybackInfo" % item_id, "POST", json={
'UserId': "{UserId}", 'UserId': "{UserId}",
'DeviceProfile': profile, 'DeviceProfile': profile,
'AutoOpenLiveStream': True 'AutoOpenLiveStream': True
}) })
def get_live_stream(item_id, play_id, token, profile): def get_live_stream(self, item_id, play_id, token, profile):
return _post("LiveStreams/Open", json={ return self._post("LiveStreams/Open", json={
'UserId': "{UserId}", 'UserId': "{UserId}",
'DeviceProfile': profile, 'DeviceProfile': profile,
'OpenToken': token, 'OpenToken': token,
@ -325,15 +330,15 @@ def get_live_stream(item_id, play_id, token, profile):
'ItemId': item_id 'ItemId': item_id
}) })
def close_live_stream(live_id): def close_live_stream(self, live_id):
return _post("LiveStreams/Close", json={ return self._post("LiveStreams/Close", json={
'LiveStreamId': live_id 'LiveStreamId': live_id
}) })
def close_transcode(device_id): def close_transcode(self, device_id):
return _delete("Videos/ActiveEncodings", params={ return self._delete("Videos/ActiveEncodings", params={
'DeviceId': device_id 'DeviceId': device_id
}) })
def delete_item(item_id): def delete_item(self, item_id):
return items("/%s" % item_id, "DELETE") return self.items("/%s" % item_id, "DELETE")