deviantart.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. ## Deviantart (Images)
  2. #
  3. # @website https://www.deviantart.com/
  4. # @provide-api yes (https://www.deviantart.com/developers/) (RSS)
  5. #
  6. # @using-api no (TODO, rewrite to api)
  7. # @results HTML
  8. # @stable no (HTML can change)
  9. # @parse url, title, thumbnail_src, img_src
  10. #
  11. # @todo rewrite to api
  12. from urllib import urlencode
  13. from urlparse import urljoin
  14. from lxml import html
  15. import re
  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+'search?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")]') # noqa
  41. title = ''.join(title_links[0].xpath('.//text()'))
  42. thumbnail_src = link.xpath('.//img')[0].attrib['src']
  43. img_src = regex.sub('/', thumbnail_src)
  44. # append result
  45. results.append({'url': url,
  46. 'title': title,
  47. 'img_src': img_src,
  48. 'thumbnail_src': thumbnail_src,
  49. 'template': 'images.html'})
  50. # return results
  51. return results