deviantart.py 1.8 KB

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