1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-14 08:40:11 +01:00
searxng/searx/engines/deviantart.py

78 lines
1.8 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
"""
Deviantart (Images)
"""
2014-09-02 16:48:18 +02:00
from urllib.parse import urlencode
from lxml import html
2013-10-20 11:12:10 +02:00
# about
about = {
"website": 'https://www.deviantart.com/',
"wikidata_id": 'Q46523',
"official_api_documentation": 'https://www.deviantart.com/developers/',
"use_official_api": False,
"require_api_key": False,
"results": 'HTML',
}
2014-09-02 16:48:18 +02:00
# engine dependent config
2013-10-20 11:12:10 +02:00
categories = ['images']
2014-09-02 16:48:18 +02:00
paging = True
2016-07-19 10:06:47 +02:00
time_range_support = True
2013-10-20 11:12:10 +02:00
time_range_dict = {
'day': 'popular-24-hours',
'week': 'popular-1-week',
'month': 'popular-1-month',
'year': 'most-recent',
}
2014-01-30 00:09:47 +01:00
# search-url
base_url = 'https://www.deviantart.com'
2014-01-20 02:31:20 +01:00
2013-10-20 11:12:10 +02:00
def request(query, params):
# https://www.deviantart.com/search/deviations?page=5&q=foo
query = {
'page' : params['pageno'],
'q' : query,
}
2016-07-26 00:22:05 +02:00
if params['time_range'] in time_range_dict:
query['order'] = time_range_dict[params['time_range']]
2014-09-02 16:48:18 +02:00
params['url'] = base_url + '/search/deviations?' + urlencode(query)
2013-10-20 11:12:10 +02:00
return params
2013-10-20 11:12:10 +02:00
def response(resp):
2014-09-02 16:48:18 +02:00
results = []
2014-09-02 16:48:18 +02:00
2013-10-20 11:12:10 +02:00
dom = html.fromstring(resp.text)
2015-01-17 19:24:35 +01:00
2019-10-16 14:42:31 +02:00
for row in dom.xpath('//div[contains(@data-hook, "content_row")]'):
for result in row.xpath('./div'):
a_tag = result.xpath('.//a[@data-hook="deviation_link"]')[0]
noscript_tag = a_tag.xpath('.//noscript')
if noscript_tag:
img_tag = noscript_tag[0].xpath('.//img')
else:
img_tag = a_tag.xpath('.//img')
if not img_tag:
continue
img_tag = img_tag[0]
results.append({
'template': 'images.html',
'url': a_tag.attrib.get('href'),
'img_src': img_tag.attrib.get('src'),
'title': img_tag.attrib.get('alt'),
})
2013-10-20 11:12:10 +02:00
return results