deviantart.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, thumbnail_src, img_src
  9. @todo rewrite to api
  10. """
  11. from lxml import html
  12. import re
  13. from searx.engines.xpath import extract_text
  14. from searx.url_utils import urlencode
  15. # engine dependent config
  16. categories = ['images']
  17. paging = True
  18. time_range_support = True
  19. # search-url
  20. base_url = 'https://www.deviantart.com/'
  21. search_url = base_url + 'search?page={page}&{query}'
  22. time_range_url = '&order={range}'
  23. time_range_dict = {'day': 11,
  24. 'week': 14,
  25. 'month': 15}
  26. # do search-request
  27. def request(query, params):
  28. if params['time_range'] and params['time_range'] not in time_range_dict:
  29. return params
  30. params['url'] = search_url.format(page=params['pageno'],
  31. query=urlencode({'q': query}))
  32. if params['time_range'] in time_range_dict:
  33. params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
  34. return params
  35. # get response from search-request
  36. def response(resp):
  37. results = []
  38. # return empty array if a redirection code is returned
  39. if resp.status_code == 302:
  40. return []
  41. dom = html.fromstring(resp.text)
  42. # parse results
  43. for row in dom.xpath('//div[contains(@data-hook, "content_row")]'):
  44. for result in row.xpath('./div'):
  45. link = result.xpath('.//a[@data-hook="deviation_link"]')[0]
  46. url = link.attrib.get('href')
  47. title = link.attrib.get('title')
  48. thumbnail_src = result.xpath('.//img')[0].attrib.get('src')
  49. img_src = thumbnail_src
  50. # http to https, remove domain sharding
  51. thumbnail_src = re.sub(r"https?://(th|fc)\d+.", "https://th01.", thumbnail_src)
  52. thumbnail_src = re.sub(r"http://", "https://", thumbnail_src)
  53. url = re.sub(r"http://(.*)\.deviantart\.com/", "https://\\1.deviantart.com/", url)
  54. # append result
  55. results.append({'url': url,
  56. 'title': title,
  57. 'img_src': img_src,
  58. 'thumbnail_src': thumbnail_src,
  59. 'template': 'images.html'})
  60. # return results
  61. return results