.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
external_urls.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4import math
5
6from searx.data import EXTERNAL_URLS
7
8
9IMDB_PREFIX_TO_URL_ID = {
10 'tt': 'imdb_title',
11 'mn': 'imdb_name',
12 'ch': 'imdb_character',
13 'co': 'imdb_company',
14 'ev': 'imdb_event',
15}
16HTTP_WIKIMEDIA_IMAGE = 'http://commons.wikimedia.org/wiki/Special:FilePath/'
17
18
19def get_imdb_url_id(imdb_item_id):
20 id_prefix = imdb_item_id[:2]
21 return IMDB_PREFIX_TO_URL_ID.get(id_prefix)
22
23
25 if url.startswith(HTTP_WIKIMEDIA_IMAGE):
26 return url[len(HTTP_WIKIMEDIA_IMAGE) :]
27 if url.startswith('File:'):
28 return url[len('File:') :]
29 return url
30
31
32def get_external_url(url_id, item_id, alternative="default"):
33 """Return an external URL or None if url_id is not found.
34
35 url_id can take value from data/external_urls.json
36 The "imdb_id" value is automatically converted according to the item_id value.
37
38 If item_id is None, the raw URL with the $1 is returned.
39 """
40 if item_id is not None:
41 if url_id == 'imdb_id':
42 url_id = get_imdb_url_id(item_id)
43 elif url_id == 'wikimedia_image':
44 item_id = get_wikimedia_image_id(item_id)
45
46 url_description = EXTERNAL_URLS.get(url_id)
47 if url_description:
48 url_template = url_description["urls"].get(alternative)
49 if url_template is not None:
50 if item_id is not None:
51 return url_template.replace('$1', item_id)
52 return url_template
53 return None
54
55
56def get_earth_coordinates_url(latitude, longitude, osm_zoom, alternative='default'):
57 url = (
58 get_external_url('map', None, alternative)
59 .replace('${latitude}', str(latitude))
60 .replace('${longitude}', str(longitude))
61 .replace('${zoom}', str(osm_zoom))
62 )
63 return url
64
65
67 """Convert an area in km² into an OSM zoom. Less reliable if the shape is not round.
68
69 logarithm regression using these data:
70 * 9596961 -> 4 (China)
71 * 3287263 -> 5 (India)
72 * 643801 -> 6 (France)
73 * 6028 -> 9
74 * 1214 -> 10
75 * 891 -> 12
76 * 12 -> 13
77
78 In WolframAlpha:
79 >>> log fit {9596961,15},{3287263, 14},{643801,13},{6028,10},{1214,9},{891,7},{12,6}
80
81 with 15 = 19-4 (China); 14 = 19-5 (India) and so on
82
83 Args:
84 area (int,float,str): area in km²
85
86 Returns:
87 int: OSM zoom or 19 in area is not a number
88 """
89 try:
90 amount = float(area)
91 return max(0, min(19, round(19 - 0.688297 * math.log(226.878 * amount))))
92 except ValueError:
93 return 19
get_imdb_url_id(imdb_item_id)
get_external_url(url_id, item_id, alternative="default")
get_earth_coordinates_url(latitude, longitude, osm_zoom, alternative='default')