deviantart.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """
  4. Deviantart (Images)
  5. """
  6. # pylint: disable=missing-function-docstring
  7. from urllib.parse import urlencode
  8. from lxml import html
  9. # about
  10. about = {
  11. "website": 'https://www.deviantart.com/',
  12. "wikidata_id": 'Q46523',
  13. "official_api_documentation": 'https://www.deviantart.com/developers/',
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'HTML',
  17. }
  18. # engine dependent config
  19. categories = ['images']
  20. paging = True
  21. time_range_support = True
  22. time_range_dict = {
  23. 'day': 'popular-24-hours',
  24. 'week': 'popular-1-week',
  25. 'month': 'popular-1-month',
  26. 'year': 'most-recent',
  27. }
  28. # search-url
  29. base_url = 'https://www.deviantart.com'
  30. def request(query, params):
  31. # https://www.deviantart.com/search/deviations?page=5&q=foo
  32. query = {
  33. 'page' : params['pageno'],
  34. 'q' : query,
  35. }
  36. if params['time_range'] in time_range_dict:
  37. query['order'] = time_range_dict[params['time_range']]
  38. params['url'] = base_url + '/search/deviations?' + urlencode(query)
  39. return params
  40. def response(resp):
  41. results = []
  42. dom = html.fromstring(resp.text)
  43. for row in dom.xpath('//div[contains(@data-hook, "content_row")]'):
  44. for result in row.xpath('./div'):
  45. a_tag = result.xpath('.//a[@data-hook="deviation_link"]')[0]
  46. noscript_tag = a_tag.xpath('.//noscript')
  47. if noscript_tag:
  48. img_tag = noscript_tag[0].xpath('.//img')
  49. else:
  50. img_tag = a_tag.xpath('.//img')
  51. if not img_tag:
  52. continue
  53. img_tag = img_tag[0]
  54. results.append({
  55. 'template': 'images.html',
  56. 'url': a_tag.attrib.get('href'),
  57. 'img_src': img_tag.attrib.get('src'),
  58. 'title': img_tag.attrib.get('alt'),
  59. })
  60. return results