| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import logging |
| 6 |
|
| 7 |
from . import api |
| 8 |
from .configuration import Config |
| 9 |
from .http import HTTP |
| 10 |
from .ws_client import WSClient |
| 11 |
from .connection_manager import ConnectionManager, CONNECTION_STATE |
| 12 |
from .timesync_manager import TimeSyncManager |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
LOG = logging.getLogger('JELLYFIN.' + __name__) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
def callback(message, data): |
| 22 |
|
| 23 |
''' Callback function should received message, data |
| 24 |
message: string |
| 25 |
data: json dictionary |
| 26 |
''' |
| 27 |
pass |
| 28 |
|
| 29 |
|
| 30 |
class JellyfinClient(object): |
| 31 |
|
| 32 |
logged_in = False |
| 33 |
|
| 34 |
def __init__(self, allow_multiple_clients=False): |
| 35 |
LOG.debug("JellyfinClient initializing...") |
| 36 |
|
| 37 |
self.config = Config() |
| 38 |
self.http = HTTP(self) |
| 39 |
self.wsc = WSClient(self, allow_multiple_clients) |
| 40 |
self.auth = ConnectionManager(self) |
| 41 |
self.jellyfin = api.API(self.http) |
| 42 |
self.callback_ws = callback |
| 43 |
self.callback = callback |
| 44 |
self.timesync = TimeSyncManager(self) |
| 45 |
|
| 46 |
def set_credentials(self, credentials=None): |
| 47 |
self.auth.credentials.set_credentials(credentials or {}) |
| 48 |
|
| 49 |
def get_credentials(self): |
| 50 |
return self.auth.credentials.get_credentials() |
| 51 |
|
| 52 |
def authenticate(self, credentials=None, options=None, discover=True): |
| 53 |
|
| 54 |
self.set_credentials(credentials or {}) |
| 55 |
state = self.auth.connect(options or {}, discover) |
| 56 |
|
| 57 |
if state['State'] == CONNECTION_STATE['SignedIn']: |
| 58 |
|
| 59 |
LOG.info("User is authenticated.") |
| 60 |
self.logged_in = True |
| 61 |
self.callback("ServerOnline", {'Id': self.auth.server_id}) |
| 62 |
|
| 63 |
state['Credentials'] = self.get_credentials() |
| 64 |
|
| 65 |
return state |
| 66 |
|
| 67 |
def start(self, websocket=False, keep_alive=True): |
| 68 |
|
| 69 |
if not self.logged_in: |
| 70 |
raise ValueError("User is not authenticated.") |
| 71 |
|
| 72 |
self.http.start_session() |
| 73 |
|
| 74 |
if keep_alive: |
| 75 |
self.http.keep_alive = True |
| 76 |
|
| 77 |
if websocket: |
| 78 |
self.start_wsc() |
| 79 |
|
| 80 |
def start_wsc(self): |
| 81 |
self.wsc.start() |
| 82 |
|
| 83 |
def stop(self): |
| 84 |
self.wsc.stop_client() |
| 85 |
self.http.stop_session() |
| 86 |
self.timesync.stop_ping() |