2021-01-13 11:31:25 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-12-18 17:59:47 +01:00
|
|
|
# lint: pylint
|
|
|
|
"""Reddit
|
2016-03-25 14:30:32 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
from datetime import datetime
|
2020-08-06 17:42:46 +02:00
|
|
|
from urllib.parse import urlencode, urljoin, urlparse
|
2016-03-25 14:30:32 +01:00
|
|
|
|
2021-01-13 11:31:25 +01:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://www.reddit.com/',
|
|
|
|
"wikidata_id": 'Q1136',
|
|
|
|
"official_api_documentation": 'https://www.reddit.com/dev/api',
|
|
|
|
"use_official_api": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
|
|
|
|
2016-03-25 14:30:32 +01:00
|
|
|
# engine dependent config
|
2021-11-01 20:37:17 +01:00
|
|
|
categories = ['social media']
|
2016-03-25 14:30:32 +01:00
|
|
|
page_size = 25
|
|
|
|
|
|
|
|
# search-url
|
2016-04-16 12:22:31 +02:00
|
|
|
base_url = 'https://www.reddit.com/'
|
|
|
|
search_url = base_url + 'search.json?{query}'
|
2016-03-25 14:30:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def request(query, params):
|
2021-12-18 17:59:47 +01:00
|
|
|
|
2016-11-30 18:43:03 +01:00
|
|
|
query = urlencode({'q': query, 'limit': page_size})
|
2016-03-25 14:30:32 +01:00
|
|
|
params['url'] = search_url.format(query=query)
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
2021-12-18 17:59:47 +01:00
|
|
|
|
2016-03-25 14:30:32 +01:00
|
|
|
img_results = []
|
|
|
|
text_results = []
|
|
|
|
|
|
|
|
search_results = json.loads(resp.text)
|
|
|
|
|
|
|
|
# return empty array if there are no results
|
|
|
|
if 'data' not in search_results:
|
|
|
|
return []
|
|
|
|
|
|
|
|
posts = search_results.get('data', {}).get('children', [])
|
|
|
|
|
|
|
|
# process results
|
|
|
|
for post in posts:
|
|
|
|
data = post['data']
|
|
|
|
|
|
|
|
# extract post information
|
|
|
|
params = {
|
2016-04-16 12:22:31 +02:00
|
|
|
'url': urljoin(base_url, data['permalink']),
|
2016-03-25 14:30:32 +01:00
|
|
|
'title': data['title']
|
|
|
|
}
|
|
|
|
|
|
|
|
# if thumbnail field contains a valid URL, we need to change template
|
|
|
|
thumbnail = data['thumbnail']
|
|
|
|
url_info = urlparse(thumbnail)
|
|
|
|
# netloc & path
|
|
|
|
if url_info[1] != '' and url_info[2] != '':
|
2016-04-16 12:22:31 +02:00
|
|
|
params['img_src'] = data['url']
|
2016-03-25 14:30:32 +01:00
|
|
|
params['thumbnail_src'] = thumbnail
|
|
|
|
params['template'] = 'images.html'
|
|
|
|
img_results.append(params)
|
|
|
|
else:
|
|
|
|
created = datetime.fromtimestamp(data['created_utc'])
|
2016-12-09 11:44:24 +01:00
|
|
|
content = data['selftext']
|
2016-03-27 00:09:04 +01:00
|
|
|
if len(content) > 500:
|
|
|
|
content = content[:500] + '...'
|
|
|
|
params['content'] = content
|
2016-03-25 14:30:32 +01:00
|
|
|
params['publishedDate'] = created
|
|
|
|
text_results.append(params)
|
|
|
|
|
|
|
|
# show images first and text results second
|
|
|
|
return img_results + text_results
|