示例#1
0
def _save_audio(response, data: StepData, config: dict):
    post_generate = config.get("post_generate", {})
    extension = data.format(post_generate["file_extension"]) if post_generate.get("file_extension",
                                                                                  None) is not None else None
    # if multiple requests were used, get only the request with the audio file
    if config["generate"]["type"].startswith("request_multiple"):
        audio_idx = data.format(config["generate"]["audio_idx"])
        response = response[audio_idx]

    content_type = response["headers"]["content-type"]
    audio = response["content"]

    # if content type is JSON, try to decode JSON string with base64
    if content_type.startswith("application/json"):
        # get audio string
        audio = data_get_pattern(data.format(post_generate["audio_key"]), audio)
        # decode Audio Key with base64
        audio = base64.b64decode(audio)
    elif extension is None:
        # check if content type is an audio type
        if not content_type.startswith("audio"):
            raise InvalidContentTypeError(None, content_type, "'audio/*'")

        # get file extention from mime type:
        extension = mimetypes.guess_all_extensions(content_type)[0].replace(".", "")

    audio_path = resources.new_temp_resource_path(data.data["_pipe_id"], extension)

    with open(audio_path, "wb") as fp:
        fp.write(audio)

    return audio_path
示例#2
0
def select(values: dict, data: StepData):
    """Entfernt alle Keys, die nicht in `"relevant_keys"` stehen aus dem Dictionary.

    :param values: Werte aus der JSON-Datei
    :param data: Daten aus der API
    """
    root = values.get("_loop_states", {}).get("_loop", None)

    if root is None:
        # If root is data root
        old_root = dict(data.data)
        data.clear_data()
        root = data.data
    else:
        old_root = dict(root)
        root.clear()

    for key in values["relevant_keys"]:
        try:
            data_insert_pattern(key, root, data_get_pattern(key, old_root))
        except:
            if values.get("ignore_errors", False) is False:
                raise
    def get_data(self, key, values: dict = None, return_on_type=None):
        """
        Gibt die Daten zurück, die hinter `key_string` stehen.

        :param key: Pfad zu den Daten in self.data.
            Besteht aus den keys zu den Daten, getrennt mit | (Pipe) Symbolen.
        :param values: Werte aus der JSON-Datei.
        :param return_on_type: Ist key eine Instanz von einem der übergebenen Typen
            oder Klassen, wird der Wert einfach zurückgegeben.
        :return: Daten hinter `key_string`.
        :raises: StepKeyError
        """
        if return_on_type is not None:
            if isinstance(key, return_on_type):
                return key

        if values is None:
            values = {}

        data = {**self.__data, **values.get("_loop_states", {})}
        key = self.__formatter.format(key, data)

        return data_get_pattern(key, data)