logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

deviantart.py (2460B)


  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 + 'browse/all/?offset={offset}&{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. offset = (params['pageno'] - 1) * 24
  31. params['url'] = search_url.format(offset=offset,
  32. query=urlencode({'q': query}))
  33. if params['time_range'] in time_range_dict:
  34. params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
  35. return params
  36. # get response from search-request
  37. def response(resp):
  38. results = []
  39. # return empty array if a redirection code is returned
  40. if resp.status_code == 302:
  41. return []
  42. dom = html.fromstring(resp.text)
  43. regex = re.compile(r'\/200H\/')
  44. # parse results
  45. for result in dom.xpath('.//span[@class="thumb wide"]'):
  46. link = result.xpath('.//a[@class="torpedo-thumb-link"]')[0]
  47. url = link.attrib.get('href')
  48. title = extract_text(result.xpath('.//span[@class="title"]'))
  49. thumbnail_src = link.xpath('.//img')[0].attrib.get('src')
  50. img_src = regex.sub('/', thumbnail_src)
  51. # http to https, remove domain sharding
  52. thumbnail_src = re.sub(r"https?://(th|fc)\d+.", "https://th01.", thumbnail_src)
  53. thumbnail_src = re.sub(r"http://", "https://", thumbnail_src)
  54. url = re.sub(r"http://(.*)\.deviantart\.com/", "https://\\1.deviantart.com/", url)
  55. # append result
  56. results.append({'url': url,
  57. 'title': title,
  58. 'img_src': img_src,
  59. 'thumbnail_src': thumbnail_src,
  60. 'template': 'images.html'})
  61. # return results
  62. return results