def BuildDirectStreamUrl(self, mediaType, itemId, container): if not itemId: raise ValueError('Invalid itemId') embyMediaType = None if mediaType == 'Video': embyMediaType = constants.URL_PLAYBACK_MEDIA_TYPE_VIDEO elif mediaType == 'Audio': embyMediaType = constants.URL_PLAYBACK_MEDIA_TYPE_AUDIO else: raise ValueError('Invalid mediaType "{}"'.format(mediaType)) url = self.BuildUrl(embyMediaType) url = Url.append(url, itemId, constants.URL_PLAYBACK_STREAM) if container: containers = container.split(',') # TODO(Montellese): for now pick the first container but maybe we # need some sanity checking / priorization url = '{}.{}'.format(url, containers[0]) url = Url.addOptions( url, { constants.URL_PLAYBACK_OPTION_STATIC: constants.URL_PLAYBACK_OPTION_STATIC_TRUE, constants.URL_QUERY_API_KEY: self.AccessToken() }) return url
def checkLogin(self): if self.finished: return not self.expired url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_PIN) url = Url.addOptions(url, { constants.URL_QUERY_DEVICE_ID: self.deviceId, constants.URL_QUERY_PIN: self.pin, }) resultObj = Request.GetAsJson(url) if not resultObj or \ constants.PROPERTY_EMBY_CONNECT_PIN_IS_CONFIRMED not in resultObj or \ constants.PROPERTY_EMBY_CONNECT_PIN_IS_EXPIRED not in resultObj: log('failed to check status of PIN {} at {}: {}'.format(self.pin, url, resultObj), xbmc.LOGWARNING) self.finished = True self.expired = True return False self.finished = resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_IS_CONFIRMED) self.expired = resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_IS_EXPIRED) if self.expired: self.finished = True return self.finished
def Authenticate(baseUrl, authenticationMethod, username=None, userId=None, password=None, deviceId=None): if not password: raise ValueError('invalid password') # prepare the authentication URL authUrl = baseUrl authUrl = Url.append(authUrl, constants.URL_USERS) body = {constants.PROPERTY_USER_AUTHENTICATION_PASSWORD: password} if authenticationMethod == Authentication.Method.UserId: if not userId: raise ValueError('invalid userId') authUrl = Url.append(authUrl, userId, constants.URL_AUTHENTICATE) elif authenticationMethod == Authentication.Method.Username: if not username: raise ValueError('invalid username') authUrl = Url.append(authUrl, constants.URL_AUTHENTICATE_BY_NAME) body[constants.PROPERTY_USER_AUTHENTICATION_USERNAME] = username else: raise ValueError('invalid authenticationMethod') headers = Request.PrepareApiCallHeaders(deviceId=deviceId, userId=userId) headers['Content-Type'] = constants.EMBY_CONTENT_TYPE resultObj = Request.PostAsJson( authUrl, headers=headers, json=body, timeout=Authentication.REQUEST_TIMEOUT_S) if not resultObj: return Authentication.Result() if constants.PROPERTY_USER_AUTHENTICATION_ACCESS_TOKEN not in resultObj: return Authentication.Result() accessToken = \ resultObj[constants.PROPERTY_USER_AUTHENTICATION_ACCESS_TOKEN] if constants.PROPERTY_USER_AUTHENTICATION_USER not in resultObj: return Authentication.Result() userObj = resultObj[constants.PROPERTY_USER_AUTHENTICATION_USER] if constants.PROPERTY_USER_AUTHENTICATION_USER_ID not in userObj: return Authentication.Result() userId = userObj[constants.PROPERTY_USER_AUTHENTICATION_USER_ID] return Authentication.Result(result=True, accessToken=accessToken, userId=userId)
def BuildImageUrl(self, itemId, imageType, imageTag=''): if not itemId: raise ValueError('Invalid itemId') if not imageType: raise ValueError('Invalid imageType') url = self.BuildItemUrl(itemId) url = Url.append(url, constants.URL_IMAGES, imageType) if imageTag: url = Url.addOptions(url, {constants.URL_QUERY_TAG: imageTag}) return url
def BuildUserUrl(self, endpoint): if not endpoint: raise ValueError('Invalid endpoint') if not self._authenticate(): raise RuntimeError('media provider {} has not yet been authenticated'.format(self._id)) url = self._url userId = self.UserId() if not userId: raise RuntimeError('No valid user authentication available to access endpoint "{}"'.format(endpoint)) url = Url.append(url, constants.URL_USERS, userId) return Url.append(url, endpoint)
def BuildConnectExchangeUrl(baseUrl, userId): if not baseUrl: raise ValueError('Invalid baseUrl') if not userId: raise ValueError('Invalid userId') url = Url.append(Server._buildBaseUrl(baseUrl), constants.URL_CONNECT, constants.URL_CONNECT_EXCHANGE) url = Url.addOptions(url, { constants.URL_QUERY_CONNECT_EXCHANGE_FORMAT: constants.URL_QUERY_CONNECT_EXCHANGE_FORMAT_JSON, constants.URL_QUERY_CONNECT_EXCHANGE_USER_ID: userId, }) return url
def BuildUserUrl(self, endpoint): if not endpoint: raise ValueError('Invalid endpoint') self._assertAuthentication() url = self._url userId = self.UserId() if not userId: raise RuntimeError( 'No valid user authentication available to access endpoint "{}"' .format(endpoint)) url = Url.append(url, constants.URL_USERS, userId) return Url.append(url, endpoint)
def GetServers(accessToken, userId): if not accessToken: raise ValueError('invalid accessToken') if not userId: raise ValueError('invalid userId') url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_SERVERS) url = Url.addOptions(url, { constants.URL_QUERY_USER_ID: userId, }) headers = EmbyConnect._getApplicationHeader() headers.update({ constants.EMBY_CONNECT_USER_TOKEN_HEADER: accessToken, }) resultObj = Request.GetAsJson(url, headers=headers) if not resultObj: log('invalid response from {}: {}'.format(url, resultObj)) return None servers = [] for server in resultObj: id = server.get(constants.PROPERTY_EMBY_CONNECT_SERVER_ID, None) systemId = server.get( constants.PROPERTY_EMBY_CONNECT_SERVER_SYSTEM_ID, None) accessKey = server.get( constants.PROPERTY_EMBY_CONNECT_SERVER_ACCESS_KEY, None) name = server.get(constants.PROPERTY_EMBY_CONNECT_SERVER_NAME, None) remoteUrl = server.get( constants.PROPERTY_EMBY_CONNECT_SERVER_REMOTE_URL, None) localUrl = server.get( constants.PROPERTY_EMBY_CONNECT_SERVER_LOCAL_URL, None) if None in (id, accessKey, name, remoteUrl, localUrl): log('invalid Emby server received from {}: {}'.format( url, server)) continue servers.append( EmbyConnect.Server(id=id, systemId=systemId, accessKey=accessKey, name=name, remoteUrl=remoteUrl, localUrl=localUrl)) return servers
def BuildSubtitleStreamUrl(self, itemId, sourceId, index, codec): if not itemId: raise ValueError('invalid itemId') if not sourceId: raise ValueError('invalid sourceId') if not index: raise ValueError('invalid index') if not codec: raise ValueError('invalid codec') # <url>/Videos/<itemId>/<sourceId>/Subtitles/<index>/Stream.<codec>?api_key=<token> url = Url.append(self._url, constants.URL_VIDEOS, itemId, sourceId, constants.URL_VIDEOS_SUBTITLES, str(index), constants.URL_VIDEOS_SUBTITLES_STREAM) url = '{}.{}'.format(url, codec) return Url.addOptions(url, {constants.URL_QUERY_API_KEY: self._authenticator.AccessToken()})
def GetItems(embyServer, date, filters=None): if not embyServer: raise ValueError('invalid embyServer') if not date: raise ValueError('invalid date') # determine the endpoint based on whether we are talking to an Emby or Jellyfin server endpoint = KodiCompanion.SyncQueue.ENDPOINT_EMBY serverInfo = Server.GetInfo(embyServer.Url()) if serverInfo and serverInfo.isJellyfinServer(): endpoint = KodiCompanion.SyncQueue.ENDPOINT_JELLYFIN # build the URL to retrieve the items from the sync queue url = embyServer.BuildUrl(endpoint) url = Url.append(url, embyServer.UserId(), KodiCompanion.SyncQueue.ENDPOINT_GET_ITEMS) url = Url.addOptions( url, { KodiCompanion.SyncQueue.QUERY_GET_ITEMS_LAST_UPDATE: date, KodiCompanion.SyncQueue.QUERY_GET_ITEMS_FILTER: filters }) itemsObj = embyServer.ApiGet(url) if not itemsObj: return [] itemsAdded = [] itemsUpdated = [] itemsRemoved = [] userDataChanged = [] if KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_ADDED in itemsObj: itemsAdded = itemsObj[ KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_ADDED] if KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_UPDATED in itemsObj: itemsUpdated = itemsObj[ KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_UPDATED] if KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_REMOVED in itemsObj: itemsRemoved = itemsObj[ KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_REMOVED] if KodiCompanion.SyncQueue.PROPERTY_GET_ITEMS_USER_DATA_CHANGED in itemsObj: userDataChanged = itemsObj[ KodiCompanion.SyncQueue. PROPERTY_GET_ITEMS_USER_DATA_CHANGED] return KodiCompanion.SyncQueue(itemsAdded=itemsAdded, itemsUpdated=itemsUpdated, itemsRemoved=itemsRemoved, userDataChanged=userDataChanged)
def exchange(self): if not self.pin: return None if not self.finished or self.expired: return None if self._authenticationResult: return self._authenticationResult url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_PIN, constants.URL_EMBY_CONNECT_PIN_AUTHENTICATE) body = { constants.URL_QUERY_DEVICE_ID: self.deviceId, constants.URL_QUERY_PIN: self.pin, } resultObj = Request.PostAsJson(url, json=body) if not resultObj or \ constants.PROPERTY_EMBY_CONNECT_PIN_USER_ID not in resultObj or \ constants.PROPERTY_EMBY_CONNECT_PIN_ACCESS_TOKEN not in resultObj: log('failed to authenticate with PIN {} at {}: {}'.format(self.pin, url, resultObj)) return None self._authenticationResult = EmbyConnect.AuthenticationResult( accessToken=resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_ACCESS_TOKEN), userId=resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN_USER_ID)) return self._authenticationResult
def BuildPublicInfoUrl(baseUrl): if not baseUrl: raise ValueError('Invalid baseUrl') return Url.append(baseUrl, constants.EMBY_PROTOCOL, constants.URL_SYSTEM, constants.URL_SYSTEM_INFO, constants.URL_SYSTEM_INFO_PUBLIC)
def GetPublicUsers(baseUrl, deviceId=None): users = [] usersUrl = Url.append(baseUrl, constants.EMBY_PROTOCOL, constants.URL_USERS, constants.URL_USERS_PUBLIC) headers = Request.PrepareApiCallHeaders(deviceId=deviceId) resultObj = Request.GetAsJson(usersUrl, headers=headers) if not resultObj: return users for userObj in resultObj: # make sure the 'Name' and 'Id' properties are available if not constants.PROPERTY_USER_NAME in userObj or not constants.PROPERTY_USER_ID in userObj: continue # make sure the name and id properties are valid user = User(userObj[constants.PROPERTY_USER_NAME], userObj[constants.PROPERTY_USER_ID]) if not user.name or not user.id: continue # check if the user is disabled if constants.PROPERTY_USER_POLICY in userObj and \ constants.PROPERTY_USER_IS_DISABLED in userObj[constants.PROPERTY_USER_POLICY] and \ userObj[constants.PROPERTY_USER_POLICY][constants.PROPERTY_USER_IS_DISABLED]: continue users.append(user) return users
def Authenticate(username, password): if not username: raise ValueError('invalid username') if not password: raise ValueError('invalid password') url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_AUTHENTICATE) headers = EmbyConnect._getApplicationHeader() body = { constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_NAME_OR_EMAIL: username, constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_PASSWORD: hashlib.md5(password), # nosec } resultObj = Request.PostAsJson(url, headers=headers, json=body) if not resultObj or \ constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_ACCESS_TOKEN not in resultObj or \ constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER not in resultObj: log('invalid response from {}: {}'.format(url, resultObj)) return None userObj = resultObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER) if constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER_ID not in userObj: log('invalid response from {}: {}'.format(url, resultObj)) return None return EmbyConnect.AuthenticationResult( accessToken=resultObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_ACCESS_TOKEN), userId=userObj.get(constants.PROPERTY_EMBY_CONNECT_AUTHENTICATION_USER_ID) )
def BuildPublicInfoUrl(baseUrl): if not baseUrl: raise ValueError('Invalid baseUrl') return Url.append(Server._buildBaseUrl(baseUrl), constants.URL_SYSTEM, constants.URL_SYSTEM_INFO, constants.URL_SYSTEM_INFO_PUBLIC)
def importItems(handle, embyServer, url, mediaType, viewId, embyMediaType=None, viewName=None, raw=False, allowDirectPlay=True): items = [] viewUrl = url viewUrl = Url.addOptions(viewUrl, { 'ParentId': viewId }) # retrieve all items matching the current media type totalCount = 0 startIndex = 0 while True: if xbmcmediaimport.shouldCancel(handle, startIndex, max(totalCount, 1)): return # put together a paged URL pagedUrlOptions = { 'StartIndex': startIndex } pagedUrl = Url.addOptions(viewUrl, pagedUrlOptions) resultObj = embyServer.ApiGet(pagedUrl) if not resultObj or not emby.constants.PROPERTY_ITEM_ITEMS in resultObj or not emby.constants.PROPERTY_ITEM_TOTAL_RECORD_COUNT in resultObj: log('invalid response for items of media type "{}" from {}'.format(mediaType, pagedUrl), xbmc.LOGERROR) return # retrieve the total number of items totalCount = int(resultObj[emby.constants.PROPERTY_ITEM_TOTAL_RECORD_COUNT]) # parse all items itemsObj = resultObj[emby.constants.PROPERTY_ITEM_ITEMS] for itemObj in itemsObj: startIndex = startIndex + 1 if xbmcmediaimport.shouldCancel(handle, startIndex, totalCount): return if raw: items.append(itemObj) else: item = kodi.Api.toFileItem(embyServer, itemObj, mediaType, embyMediaType, viewName, allowDirectPlay=allowDirectPlay) if not item: continue items.append(item) # check if we have retrieved all available items if startIndex >= totalCount: break return items
def UpdateResumePoint(embyServer, itemId, positionInTicks): if not embyServer: raise ValueError('invalid embyServer') if not itemId: raise ValueError('invalid itemId') url = embyServer.BuildUserPlayingItemUrl(itemId) url = Url.addOptions(url, {'PositionTicks': positionInTicks}) embyServer.ApiDelete(url) return True
def BuildDirectStreamUrl(self, mediaType, itemId): if not itemId: raise ValueError('Invalid itemId') embyMediaType = None if mediaType == 'Video': embyMediaType = constants.URL_PLAYBACK_MEDIA_TYPE_VIDEO elif mediaType == 'Audio': embyMediaType = constants.URL_PLAYBACK_MEDIA_TYPE_AUDIO else: raise ValueError('Invalid mediaType "{}"'.format(mediaType)) url = self.BuildUrl(embyMediaType) url = Url.append(url, itemId, constants.URL_PLAYBACK_STREAM) url = Url.addOptions(url, { constants.URL_PLAYBACK_OPTION_STATIC: constants.URL_PLAYBACK_OPTION_STATIC_TRUE, constants.URL_QUERY_API_KEY: self.AccessToken() }) return url
def getRawItemsChunked(embyServer, url, mediaType, viewId, startIndex, count): viewUrl = url viewUrl = Url.addOptions(viewUrl, {emby.constants.URL_QUERY_ITEMS_PARENT_ID: viewId}) # put together a paged URL pagedUrlOptions = { emby.constants.URL_QUERY_ITEMS_LIMIT: count, emby.constants.URL_QUERY_ITEMS_START_INDEX: startIndex } pagedUrl = Url.addOptions(viewUrl, pagedUrlOptions) # retrieve all items matching the current media type resultObj = embyServer.ApiGet(pagedUrl) if not resultObj or emby.constants.PROPERTY_ITEM_ITEMS not in resultObj or \ emby.constants.PROPERTY_ITEM_TOTAL_RECORD_COUNT not in resultObj: raise RuntimeError('invalid response for items of media type "{}" from {}'.format(mediaType, pagedUrl)) # retrieve the total number of items totalCount = int(resultObj[emby.constants.PROPERTY_ITEM_TOTAL_RECORD_COUNT]) # parse all items itemsObj = resultObj[emby.constants.PROPERTY_ITEM_ITEMS] return (totalCount, itemsObj)
def MarkAsWatched(embyServer, itemId, lastPlayed): if not embyServer: raise ValueError('invalid embyServer') if not itemId: raise ValueError('invalid itemId') lastPlayedDate = UserData.PreprocessLastPlayed(lastPlayed) url = embyServer.BuildUserPlayedItemUrl(itemId) url = Url.addOptions( url, {'DatePlayed': lastPlayedDate.strftime('%Y%m%d%H%M%S')}) if not embyServer.ApiPost(url): return False return True
def _getPin(self): if self.pin: return self.pin url = Url.append(constants.URL_EMBY_CONNECT_BASE, constants.URL_EMBY_CONNECT_PIN) body = {constants.URL_QUERY_DEVICE_ID: self.deviceId} resultObj = Request.PostAsJson(url, json=body) if not resultObj or \ not constants.PROPERTY_EMBY_CONNECT_PIN in resultObj: log('failed to get a PIN from {}: {}'.format(url, resultObj)) return None self.pin = resultObj.get(constants.PROPERTY_EMBY_CONNECT_PIN) return self.pin
def _makeDir(path): # make sure the path ends with a slash path = Url.addTrailingSlash(path) path = xbmc.translatePath(path) if xbmcvfs.exists(path): return True try: _ = xbmcvfs.mkdirs(path) except: # noqa: E722 # nosec pass if xbmcvfs.exists(path): return True try: os.makedirs(path) except: # noqa: E722 # nosec pass return xbmcvfs.exists(path)
def __init__(self, provider): if not provider: raise ValueError('Invalid provider') self._baseUrl = provider.getBasePath() self._url = Url.append(self._baseUrl, constants.EMBY_PROTOCOL) self._id = provider.getIdentifier() settings = provider.getSettings() if not settings: raise ValueError('Invalid provider without settings') self._devideId = settings.getString( constants.SETTING_PROVIDER_DEVICEID) userId = settings.getString(constants.SETTING_PROVIDER_USER) username = settings.getString(constants.SETTING_PROVIDER_USERNAME) password = settings.getString(constants.SETTING_PROVIDER_PASSWORD) if userId == constants.SETTING_PROVIDER_USER_OPTION_MANUAL: self._authenticator = Authenticator.WithUsername( self._url, self._devideId, username, password) else: self._authenticator = Authenticator.WithUserId( self._url, self._devideId, userId, password)
def _StartAction(self, mediaProvider): if not mediaProvider: raise RuntimeError('invalid mediaProvider') # if we are already connected check if something important changed in the media provider if self._connected: if kodi.Api.compareMediaProviders(self._mediaProvider, mediaProvider): # update the media provider and settings anyway self._mediaProvider = mediaProvider self._settings = self._mediaProvider.prepareSettings() return True self._StopAction(restart=True) self._mediaProvider = mediaProvider self._settings = self._mediaProvider.prepareSettings() if not self._settings: raise RuntimeError('cannot prepare media provider settings') try: # create emby server instance self._server = Server(self._mediaProvider) # authenticate with the Emby server authenticated = self._server.Authenticate(force=True) except: authenticated = False if not authenticated: ProviderObserver.log( 'failed to authenticate with {}'.format( mediaProvider2str(self._mediaProvider)), xbmc.LOGERROR) self._Reset() return False # analyze the media provider's URL urlParts = urlparse(self._mediaProvider.getBasePath()) # determine the proper scheme (ws:// or wss://) and whether or not to verify the HTTPS certificate websocketScheme = 'wss' if urlParts.scheme == 'https' else 'ws' # put the urL back together url = urlunparse( urlParts._replace(scheme=websocketScheme, path='embywebsocket')) url = Url.addOptions( url, { URL_QUERY_API_KEY: self._server.AccessToken(), URL_QUERY_DEVICE_ID: self._server.DeviceId() }) # create the websocket self._websocket = websocket.WebSocket() # connect the websocket try: self._websocket.connect(url) except Exception as err: ProviderObserver.log( 'failed to connect to {} using a websocket. {}'.format( url, err), xbmc.LOGERROR) self._Reset() return False # reduce the timeout self._websocket.settimeout(1.0) ProviderObserver.log( 'successfully connected to {} to observe media imports'.format( mediaProvider2str(self._mediaProvider))) self._connected = True return True
def BuildUrl(self, endpoint): if not endpoint: raise ValueError('Invalid endpoint') url = self._url return Url.append(url, endpoint)
def _buildBaseUrl(baseUrl): return Url.append(baseUrl, constants.EMBY_PROTOCOL)
def BuildSessionsPlayingUrl(self): url = self._url return Url.append(url, constants.URL_SESSIONS, constants.URL_SESSIONS_PLAYING)
def BuildIconUrl(baseUrl): if not baseUrl: raise ValueError('Invalid baseUrl') return Url.append(Server._buildBaseUrl(baseUrl), 'web', 'touchicon144.png')
def BuildSessionsPlayingStoppedUrl(self): url = self.BuildSessionsPlayingUrl() return Url.append(url, constants.URL_SESSIONS_PLAYING_STOPPED)
def BuildSessionsPlayingProgressUrl(self): url = self.BuildSessionsPlayingUrl() return Url.append(url, constants.URL_SESSIONS_PLAYING_PROGRESS)