|
@@ -10,8 +10,6 @@
|
|
import re
|
|
import re
|
|
import json
|
|
import json
|
|
from urllib import urlencode
|
|
from urllib import urlencode
|
|
-from lxml import html
|
|
|
|
-from searx.engines.xpath import extract_text
|
|
|
|
|
|
|
|
# search-url
|
|
# search-url
|
|
url = 'http://www.wolframalpha.com/'
|
|
url = 'http://www.wolframalpha.com/'
|
|
@@ -25,42 +23,31 @@ def request(query, params):
|
|
return params
|
|
return params
|
|
|
|
|
|
|
|
|
|
-# tries to find answer under the pattern given
|
|
|
|
-def extract_answer(script_list, pattern):
|
|
|
|
- answer = None
|
|
|
|
|
|
+# get response from search-request
|
|
|
|
+def response(resp):
|
|
|
|
+ results = []
|
|
|
|
+
|
|
|
|
+ # the answer is inside a js function
|
|
|
|
+ # answer can be located in different 'pods', although by default it should be in pod_0200
|
|
|
|
+ possible_locations = ['pod_0200\.push(.*)\n',
|
|
|
|
+ 'pod_0100\.push(.*)\n']
|
|
|
|
|
|
# get line that matches the pattern
|
|
# get line that matches the pattern
|
|
- for script in script_list:
|
|
|
|
|
|
+ for pattern in possible_locations:
|
|
try:
|
|
try:
|
|
- line = re.search(pattern, script.text_content()).group(1)
|
|
|
|
|
|
+ line = re.search(pattern, resp.text).group(1)
|
|
|
|
+ break
|
|
except AttributeError:
|
|
except AttributeError:
|
|
continue
|
|
continue
|
|
|
|
|
|
- # extract answer from json
|
|
|
|
- answer = line[line.find('{') : line.rfind('}')+1]
|
|
|
|
- answer = json.loads(answer.encode('unicode-escape'))
|
|
|
|
- answer = answer['stringified'].decode('unicode-escape')
|
|
|
|
-
|
|
|
|
- return answer
|
|
|
|
-
|
|
|
|
|
|
+ if not line:
|
|
|
|
+ return results
|
|
|
|
|
|
-# get response from search-request
|
|
|
|
-def response(resp):
|
|
|
|
-
|
|
|
|
- dom = html.fromstring(resp.text)
|
|
|
|
-
|
|
|
|
- # the answer is inside a js script
|
|
|
|
- scripts = dom.xpath('//script')
|
|
|
|
|
|
+ # extract answer from json
|
|
|
|
+ answer = line[line.find('{') : line.rfind('}')+1]
|
|
|
|
+ answer = json.loads(answer.encode('unicode-escape'))
|
|
|
|
+ answer = answer['stringified'].decode('unicode-escape')
|
|
|
|
|
|
- results = []
|
|
|
|
-
|
|
|
|
- # answer can be located in different 'pods', although by default it should be in pod_0200
|
|
|
|
- answer = extract_answer(scripts, 'pod_0200\.push(.*)\n')
|
|
|
|
- if not answer:
|
|
|
|
- answer = extract_answer(scripts, 'pod_0100\.push(.*)\n')
|
|
|
|
- if answer:
|
|
|
|
- results.append({'answer': answer})
|
|
|
|
- else:
|
|
|
|
- results.append({'answer': answer})
|
|
|
|
|
|
+ results.append({'answer': answer})
|
|
|
|
|
|
return results
|
|
return results
|