vimeo.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 HTMLParser import HTMLParser
  16. from lxml import html
  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. url_xpath = './a/@href'
  27. content_xpath = './a/img/@src'
  28. title_xpath = './a/div[@class="data"]/p[@class="title"]/text()'
  29. results_xpath = '//div[@id="browse_content"]/ol/li'
  30. publishedDate_xpath = './/p[@class="meta"]//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. # TODO required?
  39. params['cookies']['__utma'] =\
  40. '00000000.000#0000000.0000000000.0000000000.0000000000.0'
  41. return params
  42. # get response from search-request
  43. def response(resp):
  44. results = []
  45. dom = html.fromstring(resp.text)
  46. p = HTMLParser()
  47. # parse results
  48. for result in dom.xpath(results_xpath):
  49. videoid = result.xpath(url_xpath)[0]
  50. url = base_url + videoid
  51. title = p.unescape(extract_text(result.xpath(title_xpath)))
  52. thumbnail = extract_text(result.xpath(content_xpath)[0])
  53. publishedDate = parser.parse(extract_text(
  54. result.xpath(publishedDate_xpath)[0]))
  55. embedded = embedded_url.format(videoid=videoid)
  56. # append result
  57. results.append({'url': url,
  58. 'title': title,
  59. 'content': '',
  60. 'template': 'videos.html',
  61. 'publishedDate': publishedDate,
  62. 'embedded': embedded,
  63. 'thumbnail': thumbnail})
  64. # return results
  65. return results