jellyfin-kodi/resources/lib/emby/core/credentials.py

138 lines
4.3 KiB
Python
Raw Normal View History

2016-10-28 05:02:47 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
import json
import logging
import os
import time
from datetime import datetime
#################################################################################################
2018-09-06 08:36:32 +00:00
LOG = logging.getLogger('Emby.'+__name__)
2016-10-28 05:02:47 +00:00
#################################################################################################
class Credentials(object):
credentials = None
def __init__(self):
2018-09-06 08:36:32 +00:00
LOG.debug("Credentials initializing...")
def set_credentials(self, credentials):
self.credentials = credentials
def get_credentials(self, data=None):
2016-10-28 05:02:47 +00:00
2018-09-06 08:36:32 +00:00
if data is not None:
self._set(data)
return self._get()
2016-10-28 05:02:47 +00:00
def _ensure(self):
2018-09-06 08:36:32 +00:00
if not self.credentials:
try:
LOG.info(self.credentials)
if not isinstance(self.credentials, dict):
raise ValueError("invalid credentials format")
2016-10-28 05:02:47 +00:00
except Exception as e: # File is either empty or missing
2018-09-06 08:36:32 +00:00
LOG.warn(e)
2016-10-28 05:02:47 +00:00
self.credentials = {}
2018-09-06 08:36:32 +00:00
LOG.debug("credentials initialized with: %s", self.credentials)
2016-10-28 05:02:47 +00:00
self.credentials['Servers'] = self.credentials.setdefault('Servers', [])
def _get(self):
self._ensure()
2018-09-06 08:36:32 +00:00
2016-10-28 05:02:47 +00:00
return self.credentials
def _set(self, data):
if data:
2018-09-06 08:36:32 +00:00
self.credentials.update(data)
2016-10-28 05:02:47 +00:00
else:
self._clear()
2018-09-06 08:36:32 +00:00
LOG.debug("credentialsupdated")
2016-10-28 05:02:47 +00:00
def _clear(self):
2018-09-06 08:36:32 +00:00
self.credentials.clear()
2016-10-28 05:02:47 +00:00
2018-09-06 08:36:32 +00:00
def add_update_user(self, server, user):
2016-10-28 05:02:47 +00:00
2018-09-06 08:36:32 +00:00
for existing in server.setdefault('Users', []):
if existing['Id'] == user['Id']:
# Merge the data
existing['IsSignedInOffline'] = True
break
else:
server['Users'].append(user)
2016-10-28 05:02:47 +00:00
2018-09-06 08:36:32 +00:00
def add_update_server(self, servers, server):
2016-10-28 05:02:47 +00:00
if server.get('Id') is None:
raise KeyError("Server['Id'] cannot be null or empty")
# Add default DateLastAccessed if doesn't exist.
server.setdefault('DateLastAccessed', "2001-01-01T00:00:00Z")
2018-09-06 08:36:32 +00:00
for existing in servers:
2016-10-28 05:02:47 +00:00
if existing['Id'] == server['Id']:
# Merge the data
if server.get('DateLastAccessed'):
2018-09-06 08:36:32 +00:00
if self._date_object(server['DateLastAccessed']) > self._date_object(existing['DateLastAccessed']):
2016-10-28 05:02:47 +00:00
existing['DateLastAccessed'] = server['DateLastAccessed']
if server.get('UserLinkType'):
existing['UserLinkType'] = server['UserLinkType']
if server.get('AccessToken'):
existing['AccessToken'] = server['AccessToken']
existing['UserId'] = server['UserId']
if server.get('ExchangeToken'):
existing['ExchangeToken'] = server['ExchangeToken']
if server.get('RemoteAddress'):
existing['RemoteAddress'] = server['RemoteAddress']
if server.get('ManualAddress'):
existing['ManualAddress'] = server['ManualAddress']
if server.get('LocalAddress'):
existing['LocalAddress'] = server['LocalAddress']
if server.get('Name'):
existing['Name'] = server['Name']
if server.get('WakeOnLanInfos'):
existing['WakeOnLanInfos'] = server['WakeOnLanInfos']
if server.get('LastConnectionMode') is not None:
existing['LastConnectionMode'] = server['LastConnectionMode']
if server.get('ConnectServerId'):
existing['ConnectServerId'] = server['ConnectServerId']
return existing
else:
2018-09-06 08:36:32 +00:00
servers.append(server)
2016-10-28 05:02:47 +00:00
return server
2018-09-06 08:36:32 +00:00
def _date_object(self, date):
2016-10-28 05:02:47 +00:00
# Convert string to date
try:
2017-10-10 05:40:20 +00:00
date_obj = time.strptime(date, "%Y-%m-%dT%H:%M:%SZ")
2016-10-28 05:02:47 +00:00
except (ImportError, TypeError):
# TypeError: attribute of type 'NoneType' is not callable
# Known Kodi/python error
date_obj = datetime(*(time.strptime(date, "%Y-%m-%dT%H:%M:%SZ")[0:6]))
2018-09-06 08:36:32 +00:00
return date_obj