コード例 #1
0
ファイル: app.py プロジェクト: karl34/script.tubecast
    def handle_cmd(self, cmd):
        if get_setting_as_bool('debug-cmd'):
            logger.debug("CMD: {}".format(cmd))

        if case("c", cmd):
            logger.debug("C cmd received")
            self.sid = re.findall('"c","(.+?)"', cmd)[0]
            self.bind_vals["SID"] = self.sid

        elif case("S", cmd):
            logger.debug("Session established received")
            self.session_id = re.findall('"S","(.+?)"', cmd)[0]
            self.bind_vals["gsessionid"] = self.session_id

        elif case("remoteConnected", cmd):
            # Parse data
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.info("Remote connected: {}".format(data))
                self.has_client = True
                if not self.player:
                    # Start "player" thread
                    threading.Thread(target=self.__player_thread).start()
                # Start a new volume_monitor if not yet available
                if not self.volume_monitor:
                    threading.Thread(target=self.__monitor_volume).start()
                # Disable automatic playback from youtube (this is kodi not youtube :))
                self._set_disabled()
                # Check if it is a new association
                if not self.connected_client or self.connected_client != data:
                    self.connected_client = data
                    kodibrigde.remote_connected(data["name"])
            else:
                logger.debug("Command ignored, already executed before")

        elif case("remoteDisconnected", cmd):
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.info("Remote disconnected: {}".format(data))
                self._initial_app_state()
                kodibrigde.remote_disconnected(data["name"])
                # Kill player if exists
                if self.player and self.player.isPlaying:
                    self._ready()
            else:
                logger.debug("Command ignored, already executed before")

        elif case("getNowPlaying", cmd):
            logger.debug("getNowPlaying received")
            self._ready()

        elif case("setPlaylist", cmd):
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.debug("setPlaylist: {}".format(data))
                cur_video_id = data["videoId"]
                video_ids = data["videoIds"]
                if 'ctt' in data:
                    self.ctt = data["ctt"]
                self.cur_list_id = data["listId"]
                self.current_index = int(data["currentIndex"])
                self.cur_list = video_ids.split(",")
                # Set info on our custom player instance and request playback
                self.player.setInfo(cur_video_id, self.ctt, self.cur_list_id, self.current_index)
                self.player.play_from_youtube(kodibrigde.get_youtube_plugin_path(cur_video_id))
            else:
                logger.debug("Command ignored, already executed before")

        elif case("updatePlaylist", cmd):
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.debug("updatePlaylist: {}".format(data))
                if "videoIds" in list(data.keys()):
                    self.cur_list = data["videoIds"].split(",")
                    if self.current_index and self.current_index >= len(self.cur_list):
                        self.current_index -= 1
                else:
                    self.cur_list = []
                    self.current_index = 0
                    # Check if kodi is playing and if so request stop
                    if self.player.playing:
                        self.player.stop()
            else:
                logger.debug("Command ignored, already executed before")

        elif case("next", cmd):
            logger.debug("Next received")
            if self.current_index + 1 < len(self.cur_list):
                self._next()

        elif case("previous", cmd):
            logger.debug("Previous received")
            if self.current_index > 0:
                self._previous()

        elif case("pause", cmd):
            logger.debug("Pause received")
            self._pause()

        elif case("stopVideo", cmd):
            logger.debug("stopVideo received")
            if self.player and self.player.playing:
                self._ready()

        elif case("seekTo", cmd):
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.debug("seekTo: {}".format(data))
                time_seek = data["newTime"]
                self._seek(time_seek)
            else:
                logger.debug("Command ignored, already executed before")

        elif case("getVolume", cmd):
            logger.debug("getVolume received")
            self._get_volume()

        elif case("setVolume", cmd):
            code, data = parse_cmd(cmd)
            if code > self.code:
                self.code = code
                logger.debug("setVolume: {}".format(data))
                new_volume = data["volume"]
                # Set volume only if it differs from current volume
                if new_volume != kodibrigde.get_kodi_volume():
                    self._set_volume(new_volume)
            else:
                logger.debug("Command ignored, already executed before")

        elif case("play", cmd):
            logger.debug("play received")
            self.play_state = 1
            self._resume()
コード例 #2
0
ファイル: app.py プロジェクト: Lunatixz/repo-scripts
    def handle_cmd(self, cmd):  # type: (Command) -> None
        debug_cmds = get_setting_as_bool('debug-cmd')

        if debug_cmds:
            logger.debug("CMD: %s", cmd)

        code, name, data = cmd

        if code <= self.code:
            if debug_cmds:
                logger.debug("Command ignored, already executed before")
            return

        self.code = code

        if name == "c":
            logger.debug("C cmd received")
            self.bind_vals["SID"] = data[0]

        elif name == "S":
            logger.debug("Session established received")
            self.bind_vals["gsessionid"] = data

        elif name == "remoteConnected":
            logger.info("Remote connected: {}".format(data))
            if not self.player:
                # Start "player" thread
                threading.Thread(name="Player",
                                 target=self.__player_thread).start()
            # Start a new volume_monitor if not yet available
            if not self.volume_monitor:
                self.volume_monitor = VolumeMonitor(self)
                self.volume_monitor.start()

            # Disable automatic playback from youtube (this is kodi not youtube :))
            # TODO: see issue #15
            self._disable_autoplay()
            # Check if it is a new association
            if self.connected_client != data:
                self.connected_client = data
                kodibrigde.remote_connected(data["name"])

        elif name == "remoteDisconnected":
            logger.info("Remote disconnected: {}".format(data))
            self._initial_app_state()
            kodibrigde.remote_disconnected(data["name"])

        elif name == "getNowPlaying":
            logger.debug("getNowPlaying received")
            self.report_now_playing()

        elif name == "setPlaylist":
            logger.debug("setPlaylist: {}".format(data))
            self.state.handle_set_playlist(data)
            play_url = kodibrigde.get_youtube_plugin_path(self.state.video_id, seek=data.get("currentTime", 0))
            self.player.play_from_youtube(play_url)

        elif name == "updatePlaylist":
            logger.debug("updatePlaylist: {}".format(data))
            self.state.handle_update_playlist(data)
            if not self.state.has_playlist and self.player.isPlaying():
                self.player.stop()

        elif name == "next":
            logger.debug("Next received")
            self._next()

        elif name == "previous":
            logger.debug("Previous received")
            self._previous()

        elif name == "pause":
            logger.debug("Pause received")
            self._pause()

        elif name == "stopVideo":
            logger.debug("stopVideo received")
            if self.player.isPlaying():
                self.player.stop()

        elif name == "seekTo":
            logger.debug("seekTo: {}".format(data))
            self._seek(int(data["newTime"]))

        elif name == "getVolume":
            logger.debug("getVolume received")
            volume = kodibrigde.get_kodi_volume()
            self.report_volume(volume)

        elif name == "setVolume":
            logger.debug("setVolume: {}".format(data))
            new_volume = data["volume"]
            # Set volume only if it differs from current volume
            if new_volume != kodibrigde.get_kodi_volume():
                self._set_volume(new_volume)

        elif name == "play":
            logger.debug("play received")
            self._resume()

        elif debug_cmds:
            logger.debug("unhandled command: %r", name)