vimeo.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  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. # do search-request
  32. def request(query, params):
  33. params['url'] = search_url.format(pageno=params['pageno'],
  34. query=urlencode({'q': query}))
  35. # TODO required?
  36. params['cookies']['__utma'] =\
  37. '00000000.000#0000000.0000000000.0000000000.0000000000.0'
  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. url = base_url + result.xpath(url_xpath)[0]
  47. title = p.unescape(extract_text(result.xpath(title_xpath)))
  48. thumbnail = extract_text(result.xpath(content_xpath)[0])
  49. publishedDate = parser.parse(extract_text(
  50. result.xpath(publishedDate_xpath)[0]))
  51. # append result
  52. results.append({'url': url,
  53. 'title': title,
  54. 'content': '',
  55. 'template': 'videos.html',
  56. 'publishedDate': publishedDate,
  57. 'thumbnail': thumbnail})
  58. # return results
  59. return results