Esempio n. 1
0
    def downloadIcon(mediaProvider):
        if not mediaProvider:
            raise ValueError('invalid mediaProvider')

        try:
            basePath = xbmc.translatePath(__addon__.getAddonInfo('profile')).decode('utf-8')
        except AttributeError:
            basePath = xbmc.translatePath(__addon__.getAddonInfo('profile'))

        # determine the icon's URL on the media provider
        iconUrl = Server.BuildIconUrl(mediaProvider.getBasePath())

        # make sure the addon data directory exists
        if not xbmcvfs.exists(basePath):
            if not Api._makeDir(basePath):
                log('failed to create addon data directory at {}'.format(basePath), xbmc.LOGWARNING)
                return iconUrl

        # generate the icon's local path
        serverId = Server.GetServerId(mediaProvider.getIdentifier())
        iconPath = os.path.join(basePath, '{}.png'.format(serverId))

        # try to download the icon (since Emby's webserver doesn't support HEAD requests)
        try:
            urlretrieve(iconUrl, iconPath)
        except IOError as err:
                log('failed to download icon for {} from {}: {}'.format(mediaProvider2str(mediaProvider), iconUrl, err), xbmc.LOGWARNING)
                return iconUrl

        return iconPath
Esempio n. 2
0
def discoverProviderLocally(handle, options):
    baseUrl = xbmcgui.Dialog().input(localise(32050), 'http://')
    if not baseUrl:
        return None

    log('trying to discover an Emby server at {}...'.format(baseUrl))
    try:
        serverInfo = emby.api.server.Server.GetInfo(baseUrl)
        if not serverInfo:
            return None
    except:
        return None

    providerId = Server.BuildProviderId(serverInfo.id)
    providerIconUrl = Server.BuildIconUrl(baseUrl)
    provider = xbmcmediaimport.MediaProvider(
        providerId, baseUrl, serverInfo.name, providerIconUrl,
        emby.constants.SUPPORTED_MEDIA_TYPES)
    provider.setIconUrl(kodi.Api.downloadIcon(provider))

    # store local authentication in settings
    providerSettings = provider.prepareSettings()
    if not providerSettings:
        return None

    providerSettings.setString(
        emby.constants.SETTING_PROVIDER_AUTHENTICATION,
        emby.constants.SETTING_PROVIDER_AUTHENTICATION_OPTION_LOCAL)
    providerSettings.save()

    log('Local Emby server {} successfully discovered at {}'.format(
        mediaProvider2str(provider), baseUrl))

    return provider
Esempio n. 3
0
    def _addServer(self, server):
        registerServer = False

        # check if the server is already known
        if not server.id in self._servers:
            self._servers[server.id] = server
            registerServer = True
        else:
            # check if the server has already been registered or if some of its properties have changed
            if not self._servers[server.id].registered or self._servers[server.id].name != server.name or self._servers[server.id].address != server.address:
                self._servers[server.id] = server
                registerServer = True
            else:
                # simply update the server's last seen property
                self._servers[server.id].lastseen = server.lastseen

        # if the server doesn't need to be registered there's nothing else to do
        if not registerServer:
            return

        providerId = Server.BuildProviderId(server.id)
        providerIconUrl = Server.BuildIconUrl(server.address)
        mediaProvider = xbmcmediaimport.MediaProvider(providerId, server.address, server.name, providerIconUrl, emby.constants.SUPPORTED_MEDIA_TYPES)
        mediaProvider.setIconUrl(kodi.Api.downloadIcon(mediaProvider))

        if xbmcmediaimport.addAndActivateProvider(mediaProvider):
            self._servers[server.id].registered = True
            log('Emby server "{}" ({}) successfully added and activated'.format(server.name, server.id))
        else:
            self._servers[server.id].registered = False
            log('failed to add and/or activate Emby server "{}" ({})'.format(server.name, server.id))
Esempio n. 4
0
def discoverProvider(handle, options):
    baseUrl = xbmcgui.Dialog().input(localise(32050), 'http://')
    if not baseUrl:
        return

    log('trying to discover an Emby server at {}...'.format(baseUrl))
    try:
        serverInfo = emby.api.server.Server.GetInfo(baseUrl)
        if not serverInfo:
            return
    except:
        return

    providerId = Server.BuildProviderId(serverInfo.id)
    providerIconUrl = Server.BuildIconUrl(baseUrl)
    mediaProvider = xbmcmediaimport.MediaProvider(providerId, baseUrl, serverInfo.name, providerIconUrl, emby.constants.SUPPORTED_MEDIA_TYPES)
    mediaProvider.setIconUrl(kodi.Api.downloadIcon(mediaProvider))

    log('Emby server {} successfully discovered at {}'.format(mediaProvider2str(mediaProvider), baseUrl))

    xbmcmediaimport.setDiscoveredProvider(handle, True, mediaProvider)
Esempio n. 5
0
def discoverProviderWithEmbyConnect(handle, options):
    deviceId = Request.GenerateDeviceId()

    embyConnect = linkToEmbyConnect(deviceId)
    if not embyConnect:
        return None

    dialog = xbmcgui.Dialog()

    # get all connected servers
    servers = EmbyConnect.GetServers(embyConnect.accessToken,
                                     embyConnect.userId)
    if not servers:
        log(
            'no servers available for Emby Connect user id {}'.format(
                embyConnect.userId), xbmc.LOGWARNING)
        return None

    if len(servers) == 1:
        server = servers[0]
    else:
        # ask the user which server to use
        serverChoices = [server.name for server in servers]
        serverChoice = dialog.select(localise(32057), serverChoices)
        if serverChoice < 0 or serverChoice >= len(serverChoices):
            return None

        server = server[serverChoice]

    if not server:
        return None

    urls = []
    if server.localUrl:
        # ask the user whether to use a local or remote connection
        isLocal = dialog.yesno(localise(32058),
                               localise(32059).format(server.name))
        if isLocal:
            urls.append(server.localUrl)

    if server.remoteUrl:
        urls.append(server.remoteUrl)

    baseUrl = None
    # find a working connection / base URL
    for url in urls:
        try:
            _ = emby.api.server.Server.GetInfo(url)
        except:
            log('failed to connect to "{}" at {}'.format(server.name, url),
                xbmc.LOGDEBUG)
            continue

        baseUrl = url
        break

    if not baseUrl:
        dialog.ok(localise(32058), localise(32060).format(server.name))
        log(
            'failed to connect to Emby server "{}" with Emby Connect user ID {}'
            .format(server.name, embyConnect.userId), xbmc.LOGWARNING)
        return None

    providerId = Server.BuildProviderId(server.systemId)
    providerIconUrl = Server.BuildIconUrl(baseUrl)
    provider = xbmcmediaimport.MediaProvider(
        providerId, baseUrl, server.name, providerIconUrl,
        emby.constants.SUPPORTED_MEDIA_TYPES)
    provider.setIconUrl(kodi.Api.downloadIcon(provider))

    # store Emby connect authentication in settings
    providerSettings = provider.prepareSettings()
    if not providerSettings:
        return None

    providerSettings.setString(
        emby.constants.SETTING_PROVIDER_AUTHENTICATION,
        emby.constants.SETTING_PROVIDER_AUTHENTICATION_OPTION_EMBY_CONNECT)
    providerSettings.setString(
        emby.constants.SETTING_PROVIDER_EMBY_CONNECT_USER_ID,
        embyConnect.userId)
    providerSettings.setString(
        emby.constants.SETTING_PROVIDER_EMBY_CONNECT_ACCESS_KEY,
        server.accessKey)
    providerSettings.setString(emby.constants.SETTING_PROVIDER_DEVICEID,
                               deviceId)
    providerSettings.save()

    log('Emby Connect server {} successfully discovered at {}'.format(
        mediaProvider2str(provider), baseUrl))

    return provider