summaryrefslogtreecommitdiff
path: root/searx/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'searx/utils.py')
-rw-r--r--searx/utils.py32
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).