This e-mail was sent by Maarten to the pywikipedia-svn mailing list.
Revision: 8136
Author: shizhao
Date: 2010-04-28 20:29:31 +0000 (Wed, 28 Apr 2010)
Log Message:
-----------
categories() new add API query.
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2010-04-28 14:33:06 UTC (rev 8135)
+++ trunk/pywikipedia/wikipedia.py 2010-04-28 20:29:31 UTC (rev 8136)
@@ -2173,12 +2173,50 @@
category links in the page text.
"""
- try:
- category_links_to_return = getCategoryLinks(self.get(get_redirect=get_redirect), self.site())
- except NoPage:
- category_links_to_return = []
- return category_links_to_return
+# New add API query.
+# api.php?action=query&prop=categories&titles=Albert%20Einstein
+
+#
+ if not self.site().has_api():
+ try:
+ category_links_to_return = getCategoryLinks(self.get(get_redirect=get_redirect), self.site())
+ except NoPage:
+ category_links_to_return = []
+ return category_links_to_return
+
+ params = {
+ 'action': 'query',
+ 'prop' : 'categories',
+ 'titles' : self.title(),
+ }
+ if not self.site().isAllowed('apihighlimits') and config.special_page_limit> 500:
+ params['cllimit'] = 500
+
+
+ allDone = False
+ cats=[]
+ while not allDone:
+ output(u'Getting categories in %s via API...' % self.aslink())
+
+ datas = query.GetData(params, self.site())
+ data=datas['query']['pages'].values()[0]
+ if "categories" in data:
+ for c in data['categories']:
+ cats.append(c['title'])
+# if len(data) == 2:
+# data = data[0] + data[1]
+# else:
+# data = data[0]
+
+ if 'query-continue' in datas:
+ if 'categories' in datas['query-continue']:
+ params['clcontinue'] = datas['query-continue']['categories']['clcontinue']
+
+ else:
+ allDone = True
+ return cats
+
def __cmp__(self, other):
"""Test for equality and inequality of Page objects"""
if not isinstance(other, Page):
_______________________________________________
Pywikipedia-svn mailing list
Pywikipedia-svn@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikipedia-svn