File size: 2,348 Bytes
57d7008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Start local SearXNG, test each engine alone, print OK/FAIL per engine.
Usage: python tools\\engine_check.py engine1 engine2 ...   (default: a broad list)
"""
import json, os, subprocess, sys, time, urllib.parse, urllib.request
from concurrent.futures import ThreadPoolExecutor

HERE = os.path.dirname(os.path.abspath(__file__))
SX = os.path.join(HERE, "searxng")
SRC = os.path.join(SX, "src")
BASE = "http://127.0.0.1:8888"
DEFAULT = ("google bing duckduckgo brave startpage mojeek marginalia wikipedia wikidata github "
           "stackoverflow hackernews reddit qwant yahoo yandex arxiv crossref "
           "wikibooks wikisource wikiquote wiktionary searchcode_code huggingface "
           "'internet archive scholar' 'semantic scholar' openlibrary currency pypi npm "
           "'docker hub' 'mdn' 'bing news' 'google news' 'ecosia' 'presearch' 'yep'").replace("'", "")


def one(name):
    url = BASE + "/search?" + urllib.parse.urlencode(
        {"q": "python", "format": "json", "engines": name})
    try:
        with urllib.request.urlopen(url, timeout=40) as r:
            d = json.loads(r.read())
        n = len(d.get("results", []))
        un = d.get("unresponsive_engines")
        return name, n, un
    except Exception as e:
        return name, -1, str(e)[:80]


def main():
    names = sys.argv[1:] or [x for x in DEFAULT.split() if x]
    env = dict(os.environ, SEARXNG_SETTINGS_PATH=os.path.join(SX, "settings.yml"),
               PYTHONPATH=SRC + os.pathsep + os.path.join(SX, "wincompat"),
               PYTHONUTF8="1", SEARXNG_PORT="8888", SEARXNG_BIND_ADDRESS="127.0.0.1")
    log = open(os.path.join(HERE, "engine_check.log"), "w")
    p = subprocess.Popen([os.path.join(SX, "venv", "Scripts", "python.exe"), "-m", "searx.webapp"],
                         cwd=SRC, env=env, stdout=log, stderr=subprocess.STDOUT)
    try:
        for _ in range(90):
            try:
                urllib.request.urlopen(BASE + "/healthz", timeout=3)
                break
            except Exception:
                time.sleep(1)
        with ThreadPoolExecutor(6) as ex:
            for name, n, un in ex.map(one, names):
                print(("OK  " if n > 0 else "FAIL"), name, n, un or "")
    finally:
        p.terminate()
        try:
            p.wait(10)
        except Exception:
            p.kill()


main()