2014-01-05 22:10:46 +01:00
|
|
|
from urllib import urlencode
|
|
|
|
from HTMLParser import HTMLParser
|
2014-01-06 22:15:46 +01:00
|
|
|
from lxml import html
|
2014-02-05 20:24:31 +01:00
|
|
|
from xpath import extract_text
|
2014-03-18 15:56:22 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil import parser
|
2014-01-05 22:10:46 +01:00
|
|
|
|
2014-01-06 22:15:46 +01:00
|
|
|
base_url = 'http://vimeo.com'
|
|
|
|
search_url = base_url + '/search?{query}'
|
2014-01-20 02:31:20 +01:00
|
|
|
url_xpath = None
|
2014-01-11 11:14:46 +01:00
|
|
|
content_xpath = None
|
2014-01-20 02:31:20 +01:00
|
|
|
title_xpath = None
|
2014-01-11 11:14:46 +01:00
|
|
|
results_xpath = ''
|
2014-01-20 02:31:20 +01:00
|
|
|
content_tpl = '<a href="{0}"> <img src="{2}"/> </a>'
|
2014-03-18 15:56:22 +01:00
|
|
|
publishedDate_xpath = './/p[@class="meta"]//attribute::datetime'
|
2014-01-05 22:10:46 +01:00
|
|
|
|
2014-01-20 02:31:20 +01:00
|
|
|
# the cookie set by vimeo contains all the following values,
|
|
|
|
# but only __utma seems to be requiered
|
2014-01-11 11:14:46 +01:00
|
|
|
cookie = {
|
2014-01-06 22:15:46 +01:00
|
|
|
#'vuid':'918282893.1027205400'
|
|
|
|
# 'ab_bs':'%7B%223%22%3A279%7D'
|
2014-01-20 02:31:20 +01:00
|
|
|
'__utma': '00000000.000#0000000.0000000000.0000000000.0000000000.0'
|
2014-01-06 22:15:46 +01:00
|
|
|
# '__utmb':'18302654.1.10.1388942090'
|
|
|
|
#, '__utmc':'18302654'
|
2014-01-20 02:31:20 +01:00
|
|
|
#, '__utmz':'18#302654.1388942090.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)' # noqa
|
2014-01-06 22:15:46 +01:00
|
|
|
#, '__utml':'search'
|
2014-01-05 22:10:46 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 02:31:20 +01:00
|
|
|
|
2014-01-05 22:10:46 +01:00
|
|
|
def request(query, params):
|
2014-01-20 02:31:20 +01:00
|
|
|
params['url'] = search_url.format(query=urlencode({'q': query}))
|
2014-01-11 11:14:46 +01:00
|
|
|
params['cookies'] = cookie
|
2014-01-05 22:10:46 +01:00
|
|
|
return params
|
|
|
|
|
2014-01-20 02:31:20 +01:00
|
|
|
|
2014-01-05 22:10:46 +01:00
|
|
|
def response(resp):
|
|
|
|
results = []
|
|
|
|
dom = html.fromstring(resp.text)
|
2014-01-11 11:14:46 +01:00
|
|
|
|
2014-01-06 22:15:46 +01:00
|
|
|
p = HTMLParser()
|
2014-01-05 22:10:46 +01:00
|
|
|
|
2014-01-06 22:15:46 +01:00
|
|
|
for result in dom.xpath(results_xpath):
|
|
|
|
url = base_url + result.xpath(url_xpath)[0]
|
|
|
|
title = p.unescape(extract_text(result.xpath(title_xpath)))
|
2014-01-12 18:31:57 +01:00
|
|
|
thumbnail = extract_text(result.xpath(content_xpath)[0])
|
2014-03-18 15:56:22 +01:00
|
|
|
publishedDate = parser.parse(extract_text(result.xpath(publishedDate_xpath)[0]))
|
|
|
|
|
2014-01-20 02:31:20 +01:00
|
|
|
results.append({'url': url,
|
|
|
|
'title': title,
|
|
|
|
'content': content_tpl.format(url, title, thumbnail),
|
|
|
|
'template': 'videos.html',
|
2014-03-18 15:56:22 +01:00
|
|
|
'publishedDate': publishedDate,
|
2014-01-20 02:31:20 +01:00
|
|
|
'thumbnail': thumbnail})
|
2014-01-05 22:10:46 +01:00
|
|
|
return results
|