summaryrefslogtreecommitdiff
path: root/searx/engines/wolframalpha_noapi.py
blob: a5fbdeae75fd7051fc549809df178a026493c2c0 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Wolfram|Alpha (Science)
#
# @website     https://www.wolframalpha.com/
# @provide-api yes (https://api.wolframalpha.com/v2/)
#
# @using-api   no
# @results     JSON
# @stable      no
# @parse       url, infobox

from cgi import escape
from json import loads
from time import time
from urllib import urlencode
from lxml.etree import XML

from searx.poolrequests import get as http_get

# search-url
url = 'https://www.wolframalpha.com/'

search_url = url + 'input/json.jsp'\
    '?async=true'\
    '&banners=raw'\
    '&debuggingdata=false'\
    '&format=image,plaintext,imagemap,minput,moutput'\
    '&formattimeout=2'\
    '&{query}'\
    '&output=JSON'\
    '&parsetimeout=2'\
    '&proxycode={token}'\
    '&scantimeout=0.5'\
    '&sponsorcategories=true'\
    '&statemethod=deploybutton'

referer_url = url + 'input/?{query}'

token = {'value': '',
         'last_updated': None}

# xpath variables
success_xpath = '/pod[attribute::error="false"]'
plaintext_xpath = './plaintext'
title_xpath = './@title'
image_xpath = './img'
img_src_xpath = './img/@src'
img_alt_xpath = './img/@alt'

# pods to display as image in infobox
# this pods do return a plaintext, but they look better and are more useful as images
image_pods = {'Visual representation',
              'Manipulatives illustration',
              'Symbol'}


# seems, wolframalpha resets its token in every hour
def obtain_token():
    update_time = time() - (time() % 3600)
    try:
        token_response = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
        token['value'] = loads(token_response.text)['code']
        token['last_updated'] = update_time
    except:
        pass
    return token


obtain_token()


# do search-request
def request(query, params):
    # obtain token if last update was more than an hour
    if time() - token['last_updated'] > 3600:
        obtain_token()
    params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
    params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))

    return params


# get additional pod
# NOTE: this makes an additional requests to server, so the response will take longer and might reach timeout
def get_async_pod(url):
    pod = {'subpods': []}

    try:
        resp = http_get(url, timeout=2.0)

        resp_pod = XML(resp.content)
        if resp_pod.xpath(success_xpath):

            for subpod in resp_pod:
                plaintext = subpod.xpath(plaintext_xpath)[0].text
                if plaintext:
                    pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
                                           'plaintext': plaintext})
                elif subpod.xpath(image_xpath):
                    pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
                                           'plaintext': '',
                                           'img': {'src': subpod.xpath(img_src_xpath)[0],
                                                   'alt': subpod.xpath(img_alt_xpath)[0]}})
    except:
        pass

    return pod


# get response from search-request
def response(resp):
    results = []

    resp_json = loads(resp.text)

    if not resp_json['queryresult']['success']:
        return []

    # TODO handle resp_json['queryresult']['assumptions']
    result_chunks = []
    infobox_title = None
    for pod in resp_json['queryresult']['pods']:
        pod_title = pod.get('title', '')

        if 'subpods' not in pod:
            # comment this section if your requests always reach timeout
            if pod['async']:
                result = get_async_pod(pod['async'])
                if result:
                    pod = result
            else:
                continue

        # infobox title is input or text content on first pod
        if pod_title.startswith('Input') or not infobox_title:
            try:
                infobox_title = pod['subpods'][0]['plaintext']
            except:
                infobox_title = ''
                pass

        for subpod in pod['subpods']:
            if subpod['plaintext'] != '' and pod_title not in image_pods:
                # append unless it's not an actual answer
                if subpod['plaintext'] != '(requires interactivity)':
                    result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})

            elif 'img' in subpod:
                result_chunks.append({'label': pod_title, 'image': subpod['img']})

    if not result_chunks:
        return []

    results.append({'infobox': infobox_title,
                    'attributes': result_chunks,
                    'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})

    results.append({'url': resp.request.headers['Referer'],
                    'title': 'Wolfram|Alpha',
                    'content': infobox_title})

    return results