http://www.mediawiki.org/wiki/Special:Code/pywikipedia/9528
Revision: 9528 Author: valhallasw Date: 2011-09-18 15:19:02 +0000 (Sun, 18 Sep 2011) Log Message: ----------- Added support for MW >= 1.17.0 wiki's
Fixes https://sourceforge.net/tracker/?func=detail&atid=603138&aid=3400045...
Modified Paths: -------------- trunk/pywikipedia/generate_family_file.py
Modified: trunk/pywikipedia/generate_family_file.py =================================================================== --- trunk/pywikipedia/generate_family_file.py 2011-09-18 12:45:29 UTC (rev 9527) +++ trunk/pywikipedia/generate_family_file.py 2011-09-18 15:19:02 UTC (rev 9528) @@ -16,6 +16,9 @@ from urllib2 import HTTPError import urllib2
+from BeautifulSoup import BeautifulSoup +from distutils.version import StrictVersion as V + def urlopen(url): req = urllib2.Request(url, headers = {'User-agent': 'Pywikipedia family generator 0.1 - pywikipediabot.sf.net'}) return urllib2.urlopen(req) @@ -225,7 +228,19 @@ raise data = e.read() pass + + bs = BeautifulSoup(data) + try: + self.version = bs.find("meta", attrs={'name': "generator"})['content'].replace("MediaWiki ", "") + except Exception: + self.version = "0.0"
+ if V(self.version) < V("1.17.0"): + self._parse_pre_117(data) + else: + self._parse_post_117(bs) + + def _parse_pre_117(self, data): if not self.REwgEnableApi.search(data): print "*** WARNING: Api does not seem to be enabled on %s" % fromurl try: @@ -237,6 +252,13 @@ self.articlepath = self.REwgArticlePath.search(data).groups()[0] self.lang = self.REwgContentLanguage.search(data).groups()[0]
+ def _parse_post_117(self, bs): + apipath = bs.find("link", rel='EditURI')['href'].split("?")[0] + info = json.load(urlopen(apipath + "?action=query&meta=siteinfo&format=json"))['query']['general'] + + for item in ['server', 'scriptpath', 'articlepath', 'lang']: + setattr(self, item, info[item]) + def __cmp__(self, other): return (self.server + self.scriptpath == other.server + other.scriptpath)
pywikipedia-svn@lists.wikimedia.org