vimeo.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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