Revision: 7856
Author: philip
Date: 2010-01-05 16:44:49 +0000 (Tue, 05 Jan 2010)
Log Message:
-----------
Apply on api-search when enabled use_api and version >= 1.11.
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2010-01-04 14:48:21 UTC (rev 7855)
+++ trunk/pywikipedia/wikipedia.py 2010-01-05 16:44:49 UTC (rev 7856)
@@ -6515,9 +6515,45 @@
text = self.getUrl(url, sysop = sysop)
self._getUserDataOld(text, sysop = sysop, force = force)
+
+
+ def search(self, query, number = 10, namespaces = None):
+ """
+ Yield search results for query.
+ Use API when enabled use_api and version >= 1.11,
+ or use Special:Search.
+ """
+ if config.use_api and self.versionnumber() >= 11:
+ _search = self._search_with_api
+ else:
+ _search = self._search_without_api
+ return _search(query, number, namespaces)
+
+ def _search_with_api(self, q, number, namespaces):
+ """Yield search results (using api) for query."""
+ params = {
+ 'action': 'query',
+ 'list': 'search',
+ 'srsearch': q,
+ 'srlimit': number
+ }
+ if namespaces:
+ params['srnamespace'] = namespaces
+ offset = 0
+ while True:
+ params['sroffset'] = offset
+ data = query.GetData(params, self)['query']
+ if 'error' in data:
+ raise RuntimeError('%s' % data['error'])
+ if not data['search']:
+ break
+ for s in data['search']:
+ offset += 1
+ page = Page(self, s['title'])
+ yield page, s['snippet'], '', s['size'], s['wordcount'],
s['timestamp']
- def search(self, query, number = 10, namespaces = None):
+ def _search_without_api(self, query, number, namespaces):
"""Yield search results (using Special:Search page) for query."""
throttle = True
path = self.search_address(urllib.quote_plus(query.encode('utf-8')),
_______________________________________________
Pywikipedia-svn mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn