diff options
author | Allen <64094914+allendema@users.noreply.github.com> | 2024-05-23 23:21:58 +0000 |
---|---|---|
committer | Markus Heiser <markus.heiser@darmarIT.de> | 2024-05-29 17:56:17 +0200 |
commit | 0fa81fc782feb104bbd6616d87a6f441aad7d5bd (patch) | |
tree | 4fd2f697240307d616f8b49614787852e5923fc4 /searx/utils.py | |
parent | 0fb3f0e4aeecf62612cb6568910cf0f97c98cab9 (diff) | |
download | searxng-0fa81fc782feb104bbd6616d87a6f441aad7d5bd.tar.gz searxng-0fa81fc782feb104bbd6616d87a6f441aad7d5bd.zip |
[enh] add re-usable func to filter text
Diffstat (limited to 'searx/utils.py')
-rw-r--r-- | searx/utils.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/searx/utils.py b/searx/utils.py index f50618ea2..58ff72bb9 100644 --- a/searx/utils.py +++ b/searx/utils.py @@ -2,6 +2,9 @@ """Utility functions for the engines """ + +from __future__ import annotations + import re import importlib import importlib.util @@ -371,6 +374,35 @@ def convert_str_to_int(number_str: str) -> int: return 0 +def extr(txt: str, begin: str, end: str, default: str = ""): + """Extract the string between ``begin`` and ``end`` from ``txt`` + + :param txt: String to search in + :param begin: First string to be searched for + :param end: Second string to be searched for after ``begin`` + :param default: Default value if one of ``begin`` or ``end`` is not + found. Defaults to an empty string. + :return: The string between the two search-strings ``begin`` and ``end``. + If at least one of ``begin`` or ``end`` is not found, the value of + ``default`` is returned. + + Examples: + >>> extr("abcde", "a", "e") + "bcd" + >>> extr("abcde", "a", "z", deafult="nothing") + "nothing" + + """ + + # From https://github.com/mikf/gallery-dl/blob/master/gallery_dl/text.py#L129 + + try: + first = txt.index(begin) + len(begin) + return txt[first : txt.index(end, first)] + except ValueError: + return default + + def int_or_zero(num: Union[List[str], str]) -> int: """Convert num to int or 0. num can be either a str or a list. If num is a list, the first element is converted to int (or return 0 if the list is empty). |