2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-04-26 18:18:20 +00:00
|
|
|
# lint: pylint
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
Deviantart (Images)
|
|
|
|
"""
|
2014-09-02 14:48:18 +00:00
|
|
|
|
2020-08-06 15:42:46 +00:00
|
|
|
from urllib.parse import urlencode
|
2020-11-03 07:44:41 +00:00
|
|
|
from lxml import html
|
2013-10-20 09:12:10 +00:00
|
|
|
|
2021-01-13 10:31:25 +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
|
|
|
|
2020-11-03 07:44:41 +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
|
|
|
|
2020-11-03 07:44:41 +00:00
|
|
|
# search-url
|
|
|
|
base_url = 'https://www.deviantart.com'
|
2014-01-20 01:31:20 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2013-10-20 09:12:10 +00:00
|
|
|
def request(query, params):
|
2016-12-11 15:41:14 +00:00
|
|
|
|
2020-11-03 07:44:41 +00:00
|
|
|
# https://www.deviantart.com/search/deviations?page=5&q=foo
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
query = {
|
|
|
|
'page': params['pageno'],
|
|
|
|
'q': query,
|
2020-11-03 07:44:41 +00:00
|
|
|
}
|
2016-07-25 22:22:05 +00:00
|
|
|
if params['time_range'] in time_range_dict:
|
2020-11-03 07:44:41 +00:00
|
|
|
query['order'] = time_range_dict[params['time_range']]
|
2014-09-02 14:48:18 +00:00
|
|
|
|
2020-11-03 07:44:41 +00:00
|
|
|
params['url'] = base_url + '/search/deviations?' + urlencode(query)
|
2013-10-20 09:12:10 +00:00
|
|
|
|
2020-11-03 07:44:41 +00:00
|
|
|
return params
|
2013-10-20 09:12:10 +00:00
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
|
2013-10-20 09:12:10 +00:00
|
|
|
def response(resp):
|
2014-09-02 14:48:18 +00:00
|
|
|
|
2020-11-03 07:44:41 +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'):
|
2020-11-03 07:44:41 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
results.append(
|
|
|
|
{
|
|
|
|
'template': 'images.html',
|
|
|
|
'url': a_tag.attrib.get('href'),
|
|
|
|
'img_src': img_tag.attrib.get('src'),
|
|
|
|
'title': img_tag.attrib.get('alt'),
|
|
|
|
}
|
|
|
|
)
|
2020-11-03 07:44:41 +00:00
|
|
|
|
2013-10-20 09:12:10 +00:00
|
|
|
return results
|