utils.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import re
  5. import json
  6. from imp import load_source
  7. from numbers import Number
  8. from os.path import splitext, join
  9. from io import open
  10. from random import choice
  11. from html.parser import HTMLParser
  12. from lxml.etree import XPath
  13. from babel.core import get_global
  14. from searx import settings
  15. from searx.version import VERSION_STRING
  16. from searx.languages import language_codes
  17. from searx import logger
  18. logger = logger.getChild('utils')
  19. blocked_tags = ('script',
  20. 'style')
  21. ecma_unescape4_re = re.compile(r'%u([0-9a-fA-F]{4})', re.UNICODE)
  22. ecma_unescape2_re = re.compile(r'%([0-9a-fA-F]{2})', re.UNICODE)
  23. useragents = json.loads(open(os.path.dirname(os.path.realpath(__file__))
  24. + "/data/useragents.json", 'r', encoding='utf-8').read())
  25. xpath_cache = dict()
  26. lang_to_lc_cache = dict()
  27. def searx_useragent():
  28. return 'searx/{searx_version} {suffix}'.format(
  29. searx_version=VERSION_STRING,
  30. suffix=settings['outgoing'].get('useragent_suffix', ''))
  31. def gen_useragent(os=None):
  32. return str(useragents['ua'].format(os=os or choice(useragents['os']), version=choice(useragents['versions'])))
  33. class HTMLTextExtractorException(Exception):
  34. pass
  35. class HTMLTextExtractor(HTMLParser):
  36. def __init__(self):
  37. HTMLParser.__init__(self)
  38. self.result = []
  39. self.tags = []
  40. def handle_starttag(self, tag, attrs):
  41. self.tags.append(tag)
  42. def handle_endtag(self, tag):
  43. if not self.tags:
  44. return
  45. if tag != self.tags[-1]:
  46. raise HTMLTextExtractorException()
  47. self.tags.pop()
  48. def is_valid_tag(self):
  49. return not self.tags or self.tags[-1] not in blocked_tags
  50. def handle_data(self, d):
  51. if not self.is_valid_tag():
  52. return
  53. self.result.append(d)
  54. def handle_charref(self, number):
  55. if not self.is_valid_tag():
  56. return
  57. if number[0] in ('x', 'X'):
  58. codepoint = int(number[1:], 16)
  59. else:
  60. codepoint = int(number)
  61. self.result.append(chr(codepoint))
  62. def handle_entityref(self, name):
  63. if not self.is_valid_tag():
  64. return
  65. # codepoint = htmlentitydefs.name2codepoint[name]
  66. # self.result.append(chr(codepoint))
  67. self.result.append(name)
  68. def get_text(self):
  69. return ''.join(self.result).strip()
  70. def html_to_text(html):
  71. html = html.replace('\n', ' ')
  72. html = ' '.join(html.split())
  73. s = HTMLTextExtractor()
  74. try:
  75. s.feed(html)
  76. except HTMLTextExtractorException:
  77. logger.debug("HTMLTextExtractor: invalid HTML\n%s", html)
  78. return s.get_text()
  79. def dict_subset(d, properties):
  80. result = {}
  81. for k in properties:
  82. if k in d:
  83. result[k] = d[k]
  84. return result
  85. # get element in list or default value
  86. def list_get(a_list, index, default=None):
  87. if len(a_list) > index:
  88. return a_list[index]
  89. else:
  90. return default
  91. def get_torrent_size(filesize, filesize_multiplier):
  92. try:
  93. filesize = float(filesize)
  94. if filesize_multiplier == 'TB':
  95. filesize = int(filesize * 1024 * 1024 * 1024 * 1024)
  96. elif filesize_multiplier == 'GB':
  97. filesize = int(filesize * 1024 * 1024 * 1024)
  98. elif filesize_multiplier == 'MB':
  99. filesize = int(filesize * 1024 * 1024)
  100. elif filesize_multiplier == 'KB':
  101. filesize = int(filesize * 1024)
  102. elif filesize_multiplier == 'TiB':
  103. filesize = int(filesize * 1000 * 1000 * 1000 * 1000)
  104. elif filesize_multiplier == 'GiB':
  105. filesize = int(filesize * 1000 * 1000 * 1000)
  106. elif filesize_multiplier == 'MiB':
  107. filesize = int(filesize * 1000 * 1000)
  108. elif filesize_multiplier == 'KiB':
  109. filesize = int(filesize * 1000)
  110. except:
  111. filesize = None
  112. return filesize
  113. def convert_str_to_int(number_str):
  114. if number_str.isdigit():
  115. return int(number_str)
  116. else:
  117. return 0
  118. # convert a variable to integer or return 0 if it's not a number
  119. def int_or_zero(num):
  120. if isinstance(num, list):
  121. if len(num) < 1:
  122. return 0
  123. num = num[0]
  124. return convert_str_to_int(num)
  125. def is_valid_lang(lang):
  126. if isinstance(lang, bytes):
  127. lang = lang.decode()
  128. is_abbr = (len(lang) == 2)
  129. lang = lang.lower()
  130. if is_abbr:
  131. for l in language_codes:
  132. if l[0][:2] == lang:
  133. return (True, l[0][:2], l[3].lower())
  134. return False
  135. else:
  136. for l in language_codes:
  137. if l[1].lower() == lang or l[3].lower() == lang:
  138. return (True, l[0][:2], l[3].lower())
  139. return False
  140. def _get_lang_to_lc_dict(lang_list):
  141. key = str(lang_list)
  142. value = lang_to_lc_cache.get(key, None)
  143. if value is None:
  144. value = dict()
  145. for lc in lang_list:
  146. value.setdefault(lc.split('-')[0], lc)
  147. lang_to_lc_cache[key] = value
  148. return value
  149. # auxiliary function to match lang_code in lang_list
  150. def _match_language(lang_code, lang_list=[], custom_aliases={}):
  151. # replace language code with a custom alias if necessary
  152. if lang_code in custom_aliases:
  153. lang_code = custom_aliases[lang_code]
  154. if lang_code in lang_list:
  155. return lang_code
  156. # try to get the most likely country for this language
  157. subtags = get_global('likely_subtags').get(lang_code)
  158. if subtags:
  159. subtag_parts = subtags.split('_')
  160. new_code = subtag_parts[0] + '-' + subtag_parts[-1]
  161. if new_code in custom_aliases:
  162. new_code = custom_aliases[new_code]
  163. if new_code in lang_list:
  164. return new_code
  165. # try to get the any supported country for this language
  166. return _get_lang_to_lc_dict(lang_list).get(lang_code, None)
  167. # get the language code from lang_list that best matches locale_code
  168. def match_language(locale_code, lang_list=[], custom_aliases={}, fallback='en-US'):
  169. # try to get language from given locale_code
  170. language = _match_language(locale_code, lang_list, custom_aliases)
  171. if language:
  172. return language
  173. locale_parts = locale_code.split('-')
  174. lang_code = locale_parts[0]
  175. # try to get language using an equivalent country code
  176. if len(locale_parts) > 1:
  177. country_alias = get_global('territory_aliases').get(locale_parts[-1])
  178. if country_alias:
  179. language = _match_language(lang_code + '-' + country_alias[0], lang_list, custom_aliases)
  180. if language:
  181. return language
  182. # try to get language using an equivalent language code
  183. alias = get_global('language_aliases').get(lang_code)
  184. if alias:
  185. language = _match_language(alias, lang_list, custom_aliases)
  186. if language:
  187. return language
  188. if lang_code != locale_code:
  189. # try to get language from given language without giving the country
  190. language = _match_language(lang_code, lang_list, custom_aliases)
  191. return language or fallback
  192. def load_module(filename, module_dir):
  193. modname = splitext(filename)[0]
  194. if modname in sys.modules:
  195. del sys.modules[modname]
  196. filepath = join(module_dir, filename)
  197. module = load_source(modname, filepath)
  198. module.name = modname
  199. return module
  200. def to_string(obj):
  201. if isinstance(obj, str):
  202. return obj
  203. if isinstance(obj, Number):
  204. return str(obj)
  205. if hasattr(obj, '__str__'):
  206. return obj.__str__()
  207. if hasattr(obj, '__repr__'):
  208. return obj.__repr__()
  209. def ecma_unescape(s):
  210. """
  211. python implementation of the unescape javascript function
  212. https://www.ecma-international.org/ecma-262/6.0/#sec-unescape-string
  213. https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/unescape
  214. """
  215. # s = unicode(s)
  216. # "%u5409" becomes "吉"
  217. s = ecma_unescape4_re.sub(lambda e: chr(int(e.group(1), 16)), s)
  218. # "%20" becomes " ", "%F3" becomes "ó"
  219. s = ecma_unescape2_re.sub(lambda e: chr(int(e.group(1), 16)), s)
  220. return s
  221. def get_engine_from_settings(name):
  222. """Return engine configuration from settings.yml of a given engine name"""
  223. if 'engines' not in settings:
  224. return {}
  225. for engine in settings['engines']:
  226. if 'name' not in engine:
  227. continue
  228. if name == engine['name']:
  229. return engine
  230. return {}
  231. def get_xpath(xpath_str):
  232. result = xpath_cache.get(xpath_str, None)
  233. if result is None:
  234. result = XPath(xpath_str)
  235. xpath_cache[xpath_str] = result
  236. return result
  237. def eval_xpath(element, xpath_str):
  238. xpath = get_xpath(xpath_str)
  239. return xpath(element)