def connect(self):
        """Connect to the server and update connection to websocket"""
        email = __addon__.getSetting('email')
        secret = __addon__.getSetting('secret')
        if not email or not secret:
            logger.debug('Email and/or secret not defined, not connecting')
            return

        logger.debug('trying to connect')
        try:
            request = HTTPRequest(self.url,
                                  auth_username=email,
                                  auth_password=secret)

            self.websocket = yield websocket_connect(request)
        except Exception as ex:
            logger.debug('connection error: {}'.format(str(ex)))
            self.websocket = None
            notification(strings.FAILED_TO_CONNECT,
                         level='error',
                         tag='connection')
        else:
            logger.debug('Connected')
            self.connected = True
            notification(strings.CONNECTED, tag='connection')
            self.run()
    def run(self):
        """Main loop handling incomming messages"""
        while True:
            message_str = yield self.websocket.read_message()
            if message_str is None:
                logger.debug('Connection closed')
                self.websocket = None
                notification(strings.DISCONNECTED,
                             level='warn',
                             tag='connection')
                break

            try:
                message = json.loads(message_str)
                logger.debug(message)
                data = message['data']

                response_data = self.handler.handler(data)
            except Exception as ex:
                logger.error('Handler failed: {}'.format(str(ex)))
                response_data = {"status": "error", "error": "Unknown error"}

            self.websocket.write_message(
                json.dumps({
                    "correlationId": message['correlationId'],
                    "data": response_data
                }))
Example #3
0
    def find_and_play(self, video_filter):
        filtered_entities = self.find_entities(video_filter)

        entity = filtering.get_best_match(filtered_entities)
        logger.debug(u'Found Entity {}'.format(str(entity)))

        if not entity:
            notification(strings.NOTHING_FOUND)
            return False

        try:
            if 'movieid' in entity:
                self._set_last_playback_change_at()
                play_movie(entity)
            elif 'tvshowid' in entity:
                season, episode = _pick(video_filter, 'season', 'episode')
                self._set_last_playback_change_at()
                play_tvshow(entity, season, episode)
            else:
                logger.error(
                    'Invalid entity, not "movieid" nor "tvshowid" found')
                return False
        except Exception as error:
            logger.error(u'Failed to play entity: {}'.format(str(error)))

        return True
Example #4
0
    def find_and_display(self, video_filter):
        filtered_entities = self.find_entities(video_filter)

        if not filtered_entities:
            notification(strings.NOTHING_FOUND)
            return False

        best_matches = filtering.get_best_matches(filtered_entities, 10)

        display_entities = get_display_entities(best_matches)

        logger.debug('display_entities: {}'.format(str(display_entities)))

        kodi_rpc.execute_addon({"entities": display_entities})

        return True
Example #5
0
    def find_and_play(self, video_filter):
        filtered_entities = self.find_entities(video_filter)

        entity = filtering.get_best_match(filtered_entities)
        logger.debug('Found Entity {}'.format(str(entity)))

        if not entity:
            notification(strings.NOTHING_FOUND)
            return False

        if 'movieid' in entity:
            play_movie(entity)
        elif 'tvshowid' in entity:
            season, episode = _pick(video_filter, 'season', 'episode')
            play_tvshow(entity, season, episode)
        else:
            return False

        return True
Example #6
0
    def connect(self):
        """Connect to the server and update connection to websocket"""
        if self.websocket is not None or self.connecting:
            return

        email = __addon__.getSetting('email')
        secret = __addon__.getSetting('secret')
        if not email or not secret:
            logger.debug('Email and/or secret not defined, not connecting')
            return

        logger.debug('trying to connect')
        self.connecting = True
        try:
            request = HTTPRequest(self.url,
                                  headers=dict(addonversion=VERSION),
                                  auth_username=email,
                                  auth_password=secret)

            self.websocket = yield websocket_connect(request)
        except:
            logger.debug(u'connection error: {}'.format(
                traceback.format_exc()))
            self.websocket = None
            notification(strings.FAILED_TO_CONNECT,
                         level='error',
                         tag='connection')
        else:
            logger.debug('Connected')
            self.connected = True
            notification(strings.CONNECTED, tag='connection')
            self.ioloop.call_later(1, send_playback_status, self.kodi,
                                   self.get_async_tunnel())
            self.run()
        finally:
            self.connecting = False