vimeo.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Vimeo (Videos)
  2. #
  3. # @website https://vimeo.com/
  4. # @provide-api yes (http://developer.vimeo.com/api),
  5. # they have a maximum count of queries/hour
  6. #
  7. # @using-api no (TODO, rewrite to api)
  8. # @results HTML (using search portal)
  9. # @stable no (HTML can change)
  10. # @parse url, title, publishedDate, thumbnail, embedded
  11. #
  12. # @todo rewrite to api
  13. # @todo set content-parameter with correct data
  14. from urllib import urlencode
  15. from lxml import html
  16. from HTMLParser import HTMLParser
  17. from searx.engines.xpath import extract_text
  18. from dateutil import parser
  19. # engine dependent config
  20. categories = ['videos']
  21. paging = True
  22. # search-url
  23. base_url = 'https://vimeo.com'
  24. search_url = base_url + '/search/page:{pageno}?{query}'
  25. # specific xpath variables
  26. results_xpath = '//div[contains(@class,"results_grid")]/ul/li'
  27. url_xpath = './/a/@href'
  28. title_xpath = './/span[@class="title"]'
  29. thumbnail_xpath = './/img[@class="js-clip_thumbnail_image"]/@src'
  30. publishedDate_xpath = './/time/attribute::datetime'
  31. embedded_url = '<iframe data-src="//player.vimeo.com/video{videoid}" ' +\
  32. 'width="540" height="304" frameborder="0" ' +\
  33. 'webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
  34. # do search-request
  35. def request(query, params):
  36. params['url'] = search_url.format(pageno=params['pageno'],
  37. query=urlencode({'q': query}))
  38. return params
  39. # get response from search-request
  40. def response(resp):
  41. results = []
  42. dom = html.fromstring(resp.text)
  43. p = HTMLParser()
  44. # parse results
  45. for result in dom.xpath(results_xpath):
  46. videoid = result.xpath(url_xpath)[0]
  47. url = base_url + videoid
  48. title = p.unescape(extract_text(result.xpath(title_xpath)))
  49. thumbnail = extract_text(result.xpath(thumbnail_xpath)[0])
  50. publishedDate = parser.parse(extract_text(result.xpath(publishedDate_xpath)[0]))
  51. embedded = embedded_url.format(videoid=videoid)
  52. # append result
  53. results.append({'url': url,
  54. 'title': title,
  55. 'content': '',
  56. 'template': 'videos.html',
  57. 'publishedDate': publishedDate,
  58. 'embedded': embedded,
  59. 'thumbnail': thumbnail})
  60. # return results
  61. return results