New login command (that saves your login, too)
[mw] / src / mw / api.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2010  Ian Weller <ian@ianweller.org>
4 #
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.
9 #
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.
14 #
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/>.
17 ###
18
19 import cookielib
20 import gzip
21 import json
22 import mw.metadir
23 import os
24 from StringIO import StringIO
25 import urllib
26 import urllib2
27
28
29 class API(object):
30
31     def __init__(self, api_url, metadir):
32         self.api_url = api_url
33         self.metadir = metadir
34         self.cookiejar = cookielib.MozillaCookieJar(os.path.join(
35                 self.metadir.location, 'cookies'
36         ))
37         try:
38             self.cookiejar.load()
39         except IOError:
40             self.cookiejar.save()
41             self.cookiejar.load()
42         self.opener = urllib2.build_opener(
43                 urllib2.HTTPCookieProcessor(self.cookiejar))
44         self._high_limits = None
45
46     def call(self, data):
47         data['format'] = 'json'
48         request = urllib2.Request(self.api_url, urllib.urlencode(data))
49         request.add_header('Accept-encoding', 'gzip')
50         response = self.opener.open(request)
51         self.cookiejar.save()
52         if response.headers.get('Content-Encoding') == 'gzip':
53             compressed = StringIO(response.read())
54             gzipper = gzip.GzipFile(fileobj=compressed)
55             data = gzipper.read()
56         else:
57             data = response.read()
58         the_data = json.loads(data)
59         if 'error' in the_data.keys():
60             raise APIError(the_data['error']['info'])
61         return the_data
62
63     def limits(self, low, high):
64         if self._high_limits == None:
65             result = self.call({'action': 'query',
66                                 'meta': 'userinfo',
67                                 'uiprop': 'rights'})
68             self._high_limits = 'apihighlimits' in \
69                     result['query']['userinfo']['rights']
70         if self._high_limits:
71             return high
72         else:
73             return low
74
75
76 class APIError(Exception):
77
78     def __init__(self, info):
79         self.info = info
80
81     def __str__(self):
82         return self.info
83
84
85 def pagename_to_filename(name):
86     name = name.replace(' ', '_')
87     name = name.replace('/', '!')
88     return name
89
90
91 def filename_to_pagename(name):
92     name = name.replace('!', '/')
93     name = name.replace('_', ' ')
94     return name

Benjamin Mako Hill || Want to submit a patch?