| 
				
			 | 
			
			
				@@ -0,0 +1,70 @@ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				1
			 | 
			
			
				+# Wolfram Alpha (Maths) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				2
			 | 
			
			
				+# 
			 | 
		
	
		
			
			| 
				
			 | 
			
				3
			 | 
			
			
				+# @website     http://www.wolframalpha.com 
			 | 
		
	
		
			
			| 
				
			 | 
			
				4
			 | 
			
			
				+# @provide-api yes (http://api.wolframalpha.com/v2/) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				5
			 | 
			
			
				+# 
			 | 
		
	
		
			
			| 
				
			 | 
			
				6
			 | 
			
			
				+# @using-api   yes 
			 | 
		
	
		
			
			| 
				
			 | 
			
				7
			 | 
			
			
				+# @results     XML 
			 | 
		
	
		
			
			| 
				
			 | 
			
				8
			 | 
			
			
				+# @stable      yes 
			 | 
		
	
		
			
			| 
				
			 | 
			
				9
			 | 
			
			
				+# @parse       result 
			 | 
		
	
		
			
			| 
				
			 | 
			
				10
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				11
			 | 
			
			
				+from urllib import urlencode 
			 | 
		
	
		
			
			| 
				
			 | 
			
				12
			 | 
			
			
				+from lxml import etree 
			 | 
		
	
		
			
			| 
				
			 | 
			
				13
			 | 
			
			
				+from searx.engines.xpath import extract_text 
			 | 
		
	
		
			
			| 
				
			 | 
			
				14
			 | 
			
			
				+from searx.utils import html_to_text 
			 | 
		
	
		
			
			| 
				
			 | 
			
				15
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				16
			 | 
			
			
				+# search-url 
			 | 
		
	
		
			
			| 
				
			 | 
			
				17
			 | 
			
			
				+base_url = 'http://api.wolframalpha.com/v2/query' 
			 | 
		
	
		
			
			| 
				
			 | 
			
				18
			 | 
			
			
				+search_url = base_url + '?appid={api_key}&{query}&format=plaintext' 
			 | 
		
	
		
			
			| 
				
			 | 
			
				19
			 | 
			
			
				+site_url = 'http://wolframalpha.com/input/?{query}' 
			 | 
		
	
		
			
			| 
				
			 | 
			
				20
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				21
			 | 
			
			
				+#embedded_url = '<iframe width="540" height="304" ' +\ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				22
			 | 
			
			
				+#    'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				23
			 | 
			
			
				+#    'frameborder="0" allowfullscreen></iframe>' 
			 | 
		
	
		
			
			| 
				
			 | 
			
				24
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				25
			 | 
			
			
				+# do search-request 
			 | 
		
	
		
			
			| 
				
			 | 
			
				26
			 | 
			
			
				+def request(query, params): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				27
			 | 
			
			
				+    params['url'] = search_url.format(query=urlencode({'input': query}), 
			 | 
		
	
		
			
			| 
				
			 | 
			
				28
			 | 
			
			
				+                                      api_key=api_key) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				29
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				30
			 | 
			
			
				+    # need this for url in response 
			 | 
		
	
		
			
			| 
				
			 | 
			
				31
			 | 
			
			
				+    global my_query 
			 | 
		
	
		
			
			| 
				
			 | 
			
				32
			 | 
			
			
				+    my_query = query 
			 | 
		
	
		
			
			| 
				
			 | 
			
				33
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				34
			 | 
			
			
				+    return params 
			 | 
		
	
		
			
			| 
				
			 | 
			
				35
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				36
			 | 
			
			
				+# replace private user area characters to make text legible 
			 | 
		
	
		
			
			| 
				
			 | 
			
				37
			 | 
			
			
				+def replace_pua_chars(text): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				38
			 | 
			
			
				+    pua_chars = { u'\uf74c': 'd', 
			 | 
		
	
		
			
			| 
				
			 | 
			
				39
			 | 
			
			
				+                  u'\uf74d': u'\u212f', 
			 | 
		
	
		
			
			| 
				
			 | 
			
				40
			 | 
			
			
				+                  u'\uf74e': 'i', 
			 | 
		
	
		
			
			| 
				
			 | 
			
				41
			 | 
			
			
				+                  u'\uf7d9': '=' } 
			 | 
		
	
		
			
			| 
				
			 | 
			
				42
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				43
			 | 
			
			
				+    for k, v in pua_chars.iteritems(): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				44
			 | 
			
			
				+        text = text.replace(k, v) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				45
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				46
			 | 
			
			
				+    return text 
			 | 
		
	
		
			
			| 
				
			 | 
			
				47
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				48
			 | 
			
			
				+# get response from search-request 
			 | 
		
	
		
			
			| 
				
			 | 
			
				49
			 | 
			
			
				+def response(resp): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				50
			 | 
			
			
				+    results = [] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				51
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				52
			 | 
			
			
				+    search_results = etree.XML(resp.content) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				53
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				54
			 | 
			
			
				+    # return empty array if there are no results 
			 | 
		
	
		
			
			| 
				
			 | 
			
				55
			 | 
			
			
				+    if search_results.xpath('/queryresult[attribute::success="false"]'): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				56
			 | 
			
			
				+        return [] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				57
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				58
			 | 
			
			
				+    # parse result 
			 | 
		
	
		
			
			| 
				
			 | 
			
				59
			 | 
			
			
				+    result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text 
			 | 
		
	
		
			
			| 
				
			 | 
			
				60
			 | 
			
			
				+    result = replace_pua_chars(result) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				61
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				62
			 | 
			
			
				+    # bind url from site 
			 | 
		
	
		
			
			| 
				
			 | 
			
				63
			 | 
			
			
				+    result_url = site_url.format(query=urlencode({'i': my_query})) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				64
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				65
			 | 
			
			
				+    # append result 
			 | 
		
	
		
			
			| 
				
			 | 
			
				66
			 | 
			
			
				+    results.append({'url': result_url, 
			 | 
		
	
		
			
			| 
				
			 | 
			
				67
			 | 
			
			
				+                    'title': result}) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				68
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				69
			 | 
			
			
				+    # return results 
			 | 
		
	
		
			
			| 
				
			 | 
			
				70
			 | 
			
			
				+    return results 
			 |