.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.discourse Namespace Reference

Functions

 request (query, params)
 
 response (resp)
 

Variables

dict about
 
str base_url = None
 
str search_endpoint = '/search.json'
 
str api_order = 'likes'
 
bool show_avatar = False
 
str api_key = ''
 
str api_username = ''
 
bool paging = True
 
bool time_range_support = True
 
dict AGO_TIMEDELTA
 

Detailed Description

.. sidebar:: info

  - `builtwith.com Discourse <https://trends.builtwith.com/websitelist/Discourse>`_

Discourse is an open source Internet forum system.  To search in a forum this
engine offers some additional settings:

- :py:obj:`base_url`
- :py:obj:`api_order`
- :py:obj:`search_endpoint`
- :py:obj:`show_avatar`
- :py:obj:`api_key`
- :py:obj:`api_username`

Example
=======

To search in your favorite Discourse forum, add a configuration like shown here
for the ``paddling.com`` forum:

.. code:: yaml

   - name: paddling
     engine: discourse
     shortcut: paddle
     base_url: 'https://forums.paddling.com/'
     api_order: views
     categories: ['social media', 'sports']
     show_avatar: true

If the forum is private, you need to add an API key and username for the search:

.. code:: yaml

   - name: paddling
     engine: discourse
     shortcut: paddle
     base_url: 'https://forums.paddling.com/'
     api_order: views
     categories: ['social media', 'sports']
     show_avatar: true
     api_key: '<KEY>'
     api_username: 'system'


Implementations
===============

Function Documentation

◆ request()

searx.engines.discourse.request ( query,
params )

Definition at line 101 of file discourse.py.

101def request(query, params):
102
103 if len(query) <= 2:
104 return None
105
106 q = [query, f'order:{api_order}']
107 time_range = params.get('time_range')
108 if time_range:
109 after_date = datetime.now() - AGO_TIMEDELTA[time_range]
110 q.append('after:' + after_date.strftime('%Y-%m-%d'))
111
112 args = {
113 'q': ' '.join(q),
114 'page': params['pageno'],
115 }
116
117 params['url'] = f'{base_url}{search_endpoint}?{urlencode(args)}'
118 params['headers'] = {
119 'Accept': 'application/json, text/javascript, */*; q=0.01',
120 'X-Requested-With': 'XMLHttpRequest',
121 }
122
123 if api_key != '':
124 params['headers']['Api-Key'] = api_key
125
126 if api_username != '':
127 params['headers']['Api-Username'] = api_username
128
129 return params
130
131

◆ response()

searx.engines.discourse.response ( resp)

Definition at line 132 of file discourse.py.

132def response(resp):
133
134 results = []
135 json_data = resp.json()
136
137 if ('topics' or 'posts') not in json_data.keys():
138 return []
139
140 topics = {}
141
142 for item in json_data['topics']:
143 topics[item['id']] = item
144
145 for post in json_data['posts']:
146 result = topics.get(post['topic_id'], {})
147
148 url = f"{base_url}/p/{post['id']}"
149 status = gettext("closed") if result.get('closed', '') else gettext("open")
150 comments = result.get('posts_count', 0)
151 publishedDate = parser.parse(result['created_at'])
152
153 metadata = []
154 metadata.append('@' + post.get('username', ''))
155
156 if int(comments) > 1:
157 metadata.append(f'{gettext("comments")}: {comments}')
158
159 if result.get('has_accepted_answer'):
160 metadata.append(gettext("answered"))
161 elif int(comments) > 1:
162 metadata.append(status)
163
164 result = {
165 'url': url,
166 'title': html.unescape(result['title']),
167 'content': html.unescape(post.get('blurb', '')),
168 'metadata': ' | '.join(metadata),
169 'publishedDate': publishedDate,
170 'upstream': {'topics': result},
171 }
172
173 avatar = post.get('avatar_template', '').replace('{size}', '96')
174 if show_avatar and avatar:
175 result['thumbnail'] = base_url + avatar
176
177 results.append(result)
178
179 results.append({'number_of_results': len(json_data['topics'])})
180
181 return results

Variable Documentation

◆ about

dict searx.engines.discourse.about
Initial value:
1= {
2 "website": "https://discourse.org/",
3 "wikidata_id": "Q15054354",
4 "official_api_documentation": "https://docs.discourse.org/",
5 "use_official_api": True,
6 "require_api_key": False,
7 "results": "JSON",
8}

Definition at line 60 of file discourse.py.

◆ AGO_TIMEDELTA

dict searx.engines.discourse.AGO_TIMEDELTA
Initial value:
1= {
2 'day': timedelta(days=1),
3 'week': timedelta(days=7),
4 'month': timedelta(days=31),
5 'year': timedelta(days=365),
6}

Definition at line 93 of file discourse.py.

◆ api_key

str searx.engines.discourse.api_key = ''

Definition at line 84 of file discourse.py.

◆ api_order

str searx.engines.discourse.api_order = 'likes'

Definition at line 78 of file discourse.py.

◆ api_username

str searx.engines.discourse.api_username = ''

Definition at line 87 of file discourse.py.

◆ base_url

str searx.engines.discourse.base_url = None

Definition at line 69 of file discourse.py.

◆ paging

bool searx.engines.discourse.paging = True

Definition at line 90 of file discourse.py.

◆ search_endpoint

str searx.engines.discourse.search_endpoint = '/search.json'

Definition at line 72 of file discourse.py.

◆ show_avatar

bool searx.engines.discourse.show_avatar = False

Definition at line 81 of file discourse.py.

◆ time_range_support

bool searx.engines.discourse.time_range_support = True

Definition at line 91 of file discourse.py.