deviantart.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(r'\/200H\/')
  36. # parse results
  37. for result in dom.xpath('.//span[@class="thumb wide"]'):
  38. link = result.xpath('.//a[@class="torpedo-thumb-link"]')[0]
  39. url = link.attrib.get('href')
  40. title = extract_text(result.xpath('.//span[@class="title"]'))
  41. thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
  42. img_src = regex.sub('/', thumbnail_src)
  43. # http to https, remove domain sharding
  44. thumbnail_src = re.sub(r"https?://(th|fc)\d+.", "https://th01.", thumbnail_src)
  45. thumbnail_src = re.sub(r"http://", "https://", thumbnail_src)
  46. url = re.sub(r"http://(.*)\.deviantart\.com/", "https://\\1.deviantart.com/", url)
  47. # append result
  48. results.append({'url': url,
  49. 'title': title,
  50. 'img_src': img_src,
  51. 'thumbnail_src': thumbnail_src,
  52. 'template': 'images.html'})
  53. # return results
  54. return results