vimeo.py 2.1 KB

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