-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsearx_instances.py
72 lines (61 loc) · 2.47 KB
/
searx_instances.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from searxstats.config import get_git_repository_path, SEARXINSTANCES_GIT_REPOSITORY
from searxstats.model import SearxStatisticsResult
from searxstats.common.git_tool import get_repository
from searxstats.common.utils import import_module
def load_searx_instances() -> dict:
repo_directory = get_git_repository_path(SEARXINSTANCES_GIT_REPOSITORY)
get_repository(repo_directory, SEARXINSTANCES_GIT_REPOSITORY)
model_module = import_module('searxinstances.model', repo_directory)
return model_module.load()
def add_slash(url: str) -> str:
if not url.endswith('/'):
return url + '/'
else:
return url
def copy_dict_slash(dictionnary: dict) -> dict:
result = dict()
for ourl, ocomment in dictionnary.items():
result[add_slash(ourl)] = ocomment
return result
async def get_searx_stats_result_from_repository() -> SearxStatisticsResult:
"""
Fetch SearXNG instances from https://github.com/searxng/searx-instances/
"""
searx_stats_result = SearxStatisticsResult(private=False)
searx_instances = load_searx_instances()
for url, instance in searx_instances.items():
url = add_slash(url)
searx_stats_result.update_instance(url, {
'git_url': instance.git_url,
'analytics': instance.analytics,
'comments': instance.comments,
'alternativeUrls': copy_dict_slash(instance.additional_urls),
'main': True,
})
for aurl, comment in instance.additional_urls.items():
aurl = add_slash(aurl)
a_aurls = copy_dict_slash(instance.additional_urls)
a_aurls[url] = ''
if aurl in a_aurls:
del a_aurls[aurl]
searx_stats_result.update_instance(aurl, {
'git_url': instance.git_url,
'analytics': instance.analytics,
'comments': [comment],
'alternativeUrls': a_aurls
})
return searx_stats_result
async def get_searx_stats_result_from_list(instance_urls: list, private: bool) -> SearxStatisticsResult:
"""
Fetch searx instances from instance_urls given parameter.
"""
searx_stats_result = SearxStatisticsResult(private=private)
for url in instance_urls:
url = add_slash(url)
searx_stats_result.update_instance(url, {
'git_url': None,
'analytics': False,
'comments': [],
'alternativeUrls': dict()
})
return searx_stats_result