deviantart.py 1.6 KB

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