Revision: 7085
Author: alexsh
Date: 2009-07-24 16:26:48 +0000 (Fri, 24 Jul 2009)
Log Message:
-----------
Add API method for site().randompage() and site().randomredirectpage.
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2009-07-23 16:51:23 UTC (rev 7084)
+++ trunk/pywikipedia/wikipedia.py 2009-07-24 16:26:48 UTC (rev 7085)
@@ -5584,18 +5584,40 @@
break
def randompage(self):
- """Yield random page via Special:Random"""
- html = self.getUrl(self.random_address())
- m = re.search('var wgPageName = "(?P<title>.+?)";', html)
- if m is not None:
- return Page(self, m.group('title'))
+ if config.use_api:
+ params = {
+ 'action': 'query',
+ 'list': 'random',
+ #'rnnamespace': '0',
+ 'rnlimit': '1',
+ #'': '',
+ }
+ data = query.GetData(params, useAPI = True)
+ return Page(self, data['query']['random'][0]['title'])
+ else:
+ """Yield random page via Special:Random"""
+ html = self.getUrl(self.random_address())
+ m = re.search('var wgPageName = "(?P<title>.+?)";', html)
+ if m is not None:
+ return Page(self, m.group('title'))
def randomredirectpage(self):
- """Yield random redirect page via Special:RandomRedirect."""
- html = self.getUrl(self.randomredirect_address())
- m = re.search('var wgPageName = "(?P<title>.+?)";', html)
- if m is not None:
- return Page(self, m.group('title'))
+ if config.use_api:
+ params = {
+ 'action': 'query',
+ 'list': 'random',
+ #'rnnamespace': '0',
+ 'rnlimit': '1',
+ 'rnredirect': '1',
+ }
+ data = query.GetData(params, useAPI = True)
+ return Page(self, data['query']['random'][0]['title'])
+ else:
+ """Yield random redirect page via Special:RandomRedirect."""
+ html = self.getUrl(self.randomredirect_address())
+ m = re.search('var wgPageName = "(?P<title>.+?)";', html)
+ if m is not None:
+ return Page(self, m.group('title'))
def allpages(self, start='!', namespace=None, includeredirects=True,
throttle=True):
@@ -5837,8 +5859,6 @@
data = query.GetData(params, useAPI = True)
if data['query']['exturlusage'] == []:
break
-
-
for pages in data['query']['exturlusage']:
if not siteurl in pages['title']:
# the links themselves have similar form