2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009 Ian Weller <ian@ianweller.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program. If not, see <http://www.gnu.org/licenses/>.
22 from StringIO import StringIO
29 def __init__(self, api_url):
30 self.api_url = api_url
31 self.cookiejar = cookielib.CookieJar()
32 self.opener = urllib2.build_opener(
33 urllib2.HTTPCookieProcessor(self.cookiejar))
34 self._high_limits = None
37 data['format'] = 'json'
38 request = urllib2.Request(self.api_url, urllib.urlencode(data))
39 request.add_header('Accept-encoding', 'gzip')
40 response = self.opener.open(request)
41 if response.headers.get('Content-Encoding') == 'gzip':
42 compressed = StringIO(response.read())
43 gzipper = gzip.GzipFile(fileobj=compressed)
46 data = response.read()
47 return json.loads(data)
49 def limits(self, low, high):
50 if self._high_limits == None:
51 result = self.call({'action': 'query',
54 self._high_limits = 'apihighlimits' in \
55 result['query']['userinfo']['rights']
62 def pagename_to_filename(name):
63 name = name.replace(' ', '_')
64 name = name.replace('/', '!')
68 def filename_to_pagename(name):
69 name = name.replace('!', '/')
70 name = name.replace('_', ' ')