# Path: resources/lib/kodiutils.py
# def decode(s, encoding='utf-8', errors='strict'):
#     if PY2 and isinstance(s, str):
#         s = s.decode(encoding, errors)
#     return s
# 
# def sleep(sleep_time):
#     mon = xbmc.Monitor()
#     while not mon.abortRequested() and sleep_time > 0:
#         sleep_time -= 100
#         xbmc.sleep(99)
# 
# def get_uuid():
#     # pylint: disable=global-statement
#     global __CRYPT_KEY__
#     if not __CRYPT_KEY__:
#         __CRYPT_KEY__ = _get_system_uuid()
#     return __CRYPT_KEY__
# 
# Path: resources/lib/common/logger.py
# LOG = 2
# APP = 'SC'
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
# 
# Path: resources/lib/services/Monitor.py
# class Monitor(KMonitor):
#     SORT_ASC = 1
#     SORT_DESC = 2
#     SORT_METHOD_NONE = 0
#     SORT_METHOD_RATING = 17
#     SORT_METHOD_MPAA = 31
#     SORT_METHOD_TITLE = 29
#     SORT_METHOD_YEAR = 16
#     SORT_METHOD_DATE_ADDED = 40
#     def __init__(self):
#     def onSettingsChanged(self):
#     def onScreensaverActivated(self):
#     def onScreensaverDeactivated(self):
#     def onDPMSActivated(self):
#     def onDPMSDeactivated(self):
#     def onScanStarted(self, library):
#     def onScanFinished(self, library):
#     def onCleanStarted(self, library):
#     def onCleanFinished(self, library):
#     def onSleep(self, data):
#     def onWake(self, data):
#     def PlayerOnAVStart(self, data):
#     def onNotification(self, sender, method, data):
#     def periodical_check(self):
#     def can_check(self):


import select
import traceback
import websocket
    import thread
    import _thread as thread
from xbmc import Monitor
from resources.lib.kodiutils import decode, sleep, get_uuid
from resources.lib.common import logger
from resources.lib.services.Monitor import monitor
    def create_dispatcher(self, ping_timeout):
        timeout = ping_timeout or 1
        if self.sock.is_ssl():
            return SSLSCDispatcher(self, timeout)

        return SCDispatcher(self, timeout)


class WS(Monitor):
    def __init__(self):
        super().__init__()
        self.wsapp = None

    def run(self):
        thread.start_new_thread(self.run_ws, ())

    def run_ws(self):
        self.wsapp = SCWebSocketApp("wss://ws.stream-cinema.online",
                                    cookie='uid={}'.format(get_uuid()), on_message=self.on_message)
        try:
            self.wsapp.run_forever()
            self.reconnect()
        except:
            self.reconnect()

    def onNotification(self, sender, method, data):
        try:
            self.wsapp.send(method)
        except:
            pass

# Path: resources/lib/kodiutils.py
# def decode(s, encoding='utf-8', errors='strict'):
#     if PY2 and isinstance(s, str):
#         s = s.decode(encoding, errors)
#     return s
# 
# def sleep(sleep_time):
#     mon = xbmc.Monitor()
#     while not mon.abortRequested() and sleep_time > 0:
#         sleep_time -= 100
#         xbmc.sleep(99)
# 
# def get_uuid():
#     # pylint: disable=global-statement
#     global __CRYPT_KEY__
#     if not __CRYPT_KEY__:
#         __CRYPT_KEY__ = _get_system_uuid()
#     return __CRYPT_KEY__
# 
# Path: resources/lib/common/logger.py
# LOG = 2
# APP = 'SC'
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
# 
# Path: resources/lib/services/Monitor.py
# class Monitor(KMonitor):
#     SORT_ASC = 1
#     SORT_DESC = 2
#     SORT_METHOD_NONE = 0
#     SORT_METHOD_RATING = 17
#     SORT_METHOD_MPAA = 31
#     SORT_METHOD_TITLE = 29
#     SORT_METHOD_YEAR = 16
#     SORT_METHOD_DATE_ADDED = 40
#     def __init__(self):
#     def onSettingsChanged(self):
#     def onScreensaverActivated(self):
#     def onScreensaverDeactivated(self):
#     def onDPMSActivated(self):
#     def onDPMSDeactivated(self):
#     def onScanStarted(self, library):
#     def onScanFinished(self, library):
#     def onCleanStarted(self, library):
#     def onCleanFinished(self, library):
#     def onSleep(self, data):
#     def onWake(self, data):
#     def PlayerOnAVStart(self, data):
#     def onNotification(self, sender, method, data):
#     def periodical_check(self):
#     def can_check(self):


# Path: resources/lib/services/websocket.py
import select
import traceback
import websocket
    import thread
    import _thread as thread
from xbmc import Monitor
from resources.lib.kodiutils import decode, sleep, get_uuid
from resources.lib.common import logger
from resources.lib.services.Monitor import monitor

    def create_dispatcher(self, ping_timeout):
        timeout = ping_timeout or 1
        if self.sock.is_ssl():
            return SSLSCDispatcher(self, timeout)

        return SCDispatcher(self, timeout)


class WS(Monitor):
    def __init__(self):
        super().__init__()
        self.wsapp = None

    def run(self):
        thread.start_new_thread(self.run_ws, ())

    def run_ws(self):
        self.wsapp = SCWebSocketApp("wss://ws.stream-cinema.online",
                                    cookie='uid={}'.format(get_uuid()), on_message=self.on_message)
        try:
            self.wsapp.run_forever()
            self.reconnect()
        except:
            self.reconnect()

    def onNotification(self, sender, method, data):
        try:
            self.wsapp.send(method)
        except:
            pass

        logger.debug('[WS] monitor onNotification {} {} {}'.format(decode(sender), decode(method), decode(data)))