deviantart.py 2.1 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, thumbnail_src, img_src
  9. @todo rewrite to api
  10. """
  11. from urllib import urlencode
  12. from urlparse import urljoin
  13. from lxml import html
  14. import re
  15. from searx.engines.xpath import extract_text
  16. # engine dependent config
  17. categories = ['images']
  18. paging = True
  19. # search-url
  20. base_url = 'https://www.deviantart.com/'
  21. search_url = base_url + 'browse/all/?offset={offset}&{query}'
  22. # do search-request
  23. def request(query, params):
  24. offset = (params['pageno'] - 1) * 24
  25. params['url'] = search_url.format(offset=offset,
  26. query=urlencode({'q': query}))
  27. return params
  28. # get response from search-request
  29. def response(resp):
  30. results = []
  31. # return empty array if a redirection code is returned
  32. if resp.status_code == 302:
  33. return []
  34. dom = html.fromstring(resp.text)
  35. regex = re.compile('\/200H\/')
  36. # parse results
  37. for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
  38. link = result.xpath('.//a[contains(@class, "thumb")]')[0]
  39. url = urljoin(base_url, link.attrib.get('href'))
  40. title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]')
  41. title = extract_text(title_links[0])
  42. thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
  43. img_src = regex.sub('/', thumbnail_src)
  44. # http to https, remove domain sharding
  45. thumbnail_src = re.sub(r"https?://(th|fc)\d+.", "https://th01.", thumbnail_src)
  46. thumbnail_src = re.sub(r"http://", "https://", thumbnail_src)
  47. url = re.sub(r"http://(.*)\.deviantart\.com/", "https://\\1.deviantart.com/", url)
  48. # append result
  49. results.append({'url': url,
  50. 'title': title,
  51. 'img_src': img_src,
  52. 'thumbnail_src': thumbnail_src,
  53. 'template': 'images.html'})
  54. # return results
  55. return results