on master help sign in
Merge pull request #452 from pointhi/engine_fix

[enh] fix content fetching, parse published date from description for startpage and ixquick

commit3a2f29344a2846f530418632b88db4e345f32249 authorAdam Tauber <asciimoo@gmail.com> dateMon 26 Oct 2015, 09:29 (11 years ago) parenta9599774508c96
2 files changed+40−9
searx/engines/startpage.py+37−6 searx/tests/engines/test_startpage.py+3−3
searx/engines/startpage.py+37−6
@@ -12,6 +12,8 @@
1212
1313from lxml import html
1414from cgi import escape
15+from dateutil import parser
16+from datetime import datetime, timedelta
1517import re
1618from searx.engines.xpath import extract_text
1719
@@ -79,15 +81,44 @@ def response(resp):
7981
8082 title = escape(extract_text(link))
8183
82− if result.xpath('./p[@class="desc"]'):
83− content = escape(extract_text(result.xpath('./p[@class="desc"]')))
84+ if result.xpath('./p[@class="desc clk"]'):
85+ content = escape(extract_text(result.xpath('./p[@class="desc clk"]')))
8486 else:
8587 content = ''
8688
87− # append result
88− results.append({'url': url,
89− 'title': title,
90− 'content': content})
89+ published_date = None
90+
91+ # check if search result starts with something like: "2 Sep 2014 ... "
92+ if re.match("^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
93+ date_pos = content.find('...')+4
94+ date_string = content[0:date_pos-5]
95+ published_date = parser.parse(date_string, dayfirst=True)
96+
97+ # fix content string
98+ content = content[date_pos:]
99+
100+ # check if search result starts with something like: "5 days ago ... "
101+ elif re.match("^[0-9]+ days? ago \.\.\. ", content):
102+ date_pos = content.find('...')+4
103+ date_string = content[0:date_pos-5]
104+
105+ # calculate datetime
106+ published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
107+
108+ # fix content string
109+ content = content[date_pos:]
110+
111+ if published_date:
112+ # append result
113+ results.append({'url': url,
114+ 'title': title,
115+ 'content': content,
116+ 'publishedDate': published_date})
117+ else:
118+ # append result
119+ results.append({'url': url,
120+ 'title': title,
121+ 'content': content})
91122
92123 # return results
93124 return results
searx/tests/engines/test_startpage.py+3−3
@@ -42,7 +42,7 @@ class TestStartpageEngine(SearxTestCase):
4242 </a>
4343 <span id='title_stars_2' name='title_stars_2'> </span>
4444 </h3>
45− <p class='desc'>
45+ <p class='desc clk'>
4646 This should be the content.
4747 </p>
4848 <p>
@@ -78,7 +78,7 @@ class TestStartpageEngine(SearxTestCase):
7878 </a>
7979 <span id='title_stars_2' name='title_stars_2'> </span>
8080 </h3>
81− <p class='desc'>
81+ <p class='desc clk'>
8282 This should be the content.
8383 </p>
8484 <p>
@@ -101,7 +101,7 @@ class TestStartpageEngine(SearxTestCase):
101101 <h3>
102102 <span id='title_stars_2' name='title_stars_2'> </span>
103103 </h3>
104− <p class='desc'>
104+ <p class='desc clk'>
105105 This should be the content.
106106 </p>
107107 <p>