vimeo.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 lxml import html
  16. from dateutil import parser
  17. from cgi import escape
  18. # engine dependent config
  19. categories = ['videos']
  20. paging = True
  21. # search-url
  22. base_url = 'http://vimeo.com'
  23. search_url = base_url + '/search/page:{pageno}?{query}'
  24. # specific xpath variables
  25. results_xpath = '//div[@id="browse_content"]/ol/li'
  26. url_xpath = './a/@href'
  27. title_xpath = './a/div[@class="data"]/p[@class="title"]'
  28. content_xpath = './a/img/@src'
  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. return params
  35. # get response from search-request
  36. def response(resp):
  37. results = []
  38. dom = html.fromstring(resp.text)
  39. # parse results
  40. for result in dom.xpath(results_xpath):
  41. url = base_url + result.xpath(url_xpath)[0]
  42. title = escape(html.tostring(result.xpath(title_xpath)[0], method='text', encoding='UTF-8').decode("utf-8"))
  43. thumbnail = result.xpath(content_xpath)[0]
  44. publishedDate = parser.parse(result.xpath(publishedDate_xpath)[0])
  45. # append result
  46. results.append({'url': url,
  47. 'title': title,
  48. 'content': '',
  49. 'template': 'videos.html',
  50. 'publishedDate': publishedDate,
  51. 'thumbnail': thumbnail})
  52. # return results
  53. return results