2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2010 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 the_data = json.loads(data)
48 if 'error' in the_data.keys():
49 raise APIError(the_data['error']['info'])
52 def limits(self, low, high):
53 if self._high_limits == None:
54 result = self.call({'action': 'query',
57 self._high_limits = 'apihighlimits' in \
58 result['query']['userinfo']['rights']
65 class APIError(Exception):
67 def __init__(self, info):
74 def pagename_to_filename(name):
75 name = name.replace(' ', '_')
76 name = name.replace('/', '!')
80 def filename_to_pagename(name):
81 name = name.replace('!', '/')
82 name = name.replace('_', ' ')