deviantart.py 1.9 KB

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