[enh] Improved vimeo search engines, and add the configuration in the sample

This commit is contained in:
pw3t 2014-01-06 22:15:46 +01:00
parent 68364df342
commit 1950e697d6
2 changed files with 26 additions and 29 deletions

View File

@ -86,3 +86,10 @@ engine = dailymotion
locale = en_US locale = en_US
categories = videos categories = videos
[vimeo]
engine = vimeo
categories = videos
results_xpath = //div[@id="browse_content"]/ol/li
url_xpath=./a/@href
title_xpath=./a/div[@class="data"]/p[@class="title"]/text()
content_xpath=./a/img/@src

View File

@ -1,48 +1,38 @@
from urllib import urlencode from urllib import urlencode
from HTMLParser import HTMLParser from HTMLParser import HTMLParser
from xpath import * from xpath import extract_text
from lxml import html
categories = ['dev'] base_url = 'http://vimeo.com'
search_url = base_url + '/search?{query}'
search_url = 'http://vimeo.com/search?{query}'
# the cookie set by vime contains all the following values, but only __utma seems to be requiered
Cookie = { Cookie = {
'vuid':'918282893.1027205400' #'vuid':'918282893.1027205400'
, 'ab_bs':'%7B%223%22%3A279%7D' # 'ab_bs':'%7B%223%22%3A279%7D'
, '__utma':'18302654.101#6140782.1388942090.1388942090.1388942090.1' '__utma':'00000000.000#0000000.0000000000.0000000000.0000000000.0'
, '__utmb':'18302654.1.10.1388942090' # '__utmb':'18302654.1.10.1388942090'
, '__utmc':'18302654' #, '__utmc':'18302654'
, '__utmz':'18#302654.1388942090.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)' #, '__utmz':'18#302654.1388942090.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
, '__utml':'search' #, '__utml':'search'
} }
#'vuid=918282893.1027205400& ab_bs=%7B%223%22%3A279%7D& player="scaling=1&volume=1"& __utma=18302654.101#6140782.1388942090.1388942090.1388942090.1& __utmb=18302654.1.10.1388942090& __utmc=18302654& __utmz=18#302654.1388942090.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)& __utmli=search'
def request(query, params): def request(query, params):
params['url'] = search_url.format(query=urlencode({'q' :query})) params['url'] = search_url.format(query=urlencode({'q' :query}))
print params['url'] print params['url']
params['cookies'] = Cookie params['cookies'] = Cookie
return params return params
def response(resp): def response(resp):
results = [] results = []
dom = html.fromstring(resp.text) dom = html.fromstring(resp.text)
if results_xpath: p = HTMLParser()
for result in dom.xpath(results_xpath):
url = extract_url(result.xpath(url_xpath))
title = extract_text(result.xpath(title_xpath)[0 ]) for result in dom.xpath(results_xpath):
content = extract_text(result.xpath(content_xpath)[0]) url = base_url + result.xpath(url_xpath)[0]
results.append({'url': url, 'title': title, 'content': content}) title = p.unescape(extract_text(result.xpath(title_xpath)))
else: content = '<a href="{0}"> <img src="{2}"/> </a>'.format(url, title, extract_text(result.xpath(content_xpath)[0]))
for url, title, content in zip( results.append({'url': url, 'title': title, 'content': content})
map(extract_url, dom.xpath(url_xpath)), \
map(extract_text, dom.xpath(title_xpath)), \
map(extract_text, dom.xpath(content_xpath)), \
):
results.append({'url': url, 'title': title, 'content': content})
return results return results