searxng/searx/engines/deviantart.py

78 lines
1.9 KiB
Python
Raw Normal View History

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Deviantart (Images)
"""
# pylint: disable=missing-function-docstring
2014-09-02 14:48:18 +00:00
from urllib.parse import urlencode
from lxml import html
2013-10-20 09:12:10 +00: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 14:48:18 +00:00
# engine dependent config
2013-10-20 09:12:10 +00:00
categories = ['images']
2014-09-02 14:48:18 +00:00
paging = True
2016-07-19 08:06:47 +00:00
time_range_support = True
2013-10-20 09:12:10 +00:00
time_range_dict = {
'day': 'popular-24-hours',
'week': 'popular-1-week',
'month': 'popular-1-month',
'year': 'most-recent',
}
2014-01-29 23:09:47 +00:00
# search-url
base_url = 'https://www.deviantart.com'
2014-01-20 01:31:20 +00:00
2013-10-20 09:12:10 +00:00
def request(query, params):
# https://www.deviantart.com/search/deviations?page=5&q=foo
query = {
'page' : params['pageno'],
'q' : query,
}
2016-07-25 22:22:05 +00:00
if params['time_range'] in time_range_dict:
query['order'] = time_range_dict[params['time_range']]
2014-09-02 14:48:18 +00:00
params['url'] = base_url + '/search/deviations?' + urlencode(query)
2013-10-20 09:12:10 +00:00
return params
2013-10-20 09:12:10 +00:00
def response(resp):
2014-09-02 14:48:18 +00:00
results = []
2014-09-02 14:48:18 +00:00
2013-10-20 09:12:10 +00:00
dom = html.fromstring(resp.text)
2015-01-17 18:24:35 +00:00
2019-10-16 12:42:31 +00: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 09:12:10 +00:00
return results