projects
/
mw
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Nix warning on edit collision since we do that now
[mw]
/
src
/
mw
/
api.py
diff --git
a/src/mw/api.py
b/src/mw/api.py
index cd913658696bb0bbeb26d5e7a2f82c2755653513..6aec91c7b36a72353b328c2872e9da0b4882865b 100644
(file)
--- a/
src/mw/api.py
+++ b/
src/mw/api.py
@@
-1,6
+1,6
@@
###
# mw - VCS-like nonsense for MediaWiki websites
###
# mw - VCS-like nonsense for MediaWiki websites
-# Copyright (C) 20
09
Ian Weller <ian@ianweller.org>
+# Copyright (C) 20
10
Ian Weller <ian@ianweller.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@
-19,6
+19,9
@@
import cookielib
import gzip
import json
import cookielib
import gzip
import json
+import mw
+import mw.metadir
+import os
from StringIO import StringIO
import urllib
import urllib2
from StringIO import StringIO
import urllib
import urllib2
@@
-26,25
+29,38
@@
import urllib2
class API(object):
class API(object):
- def __init__(self, api_url):
+ def __init__(self, api_url
, metadir
):
self.api_url = api_url
self.api_url = api_url
- self.cookiejar = cookielib.CookieJar()
+ self.metadir = metadir
+ self.cookiejar = cookielib.MozillaCookieJar(os.path.join(
+ self.metadir.location, 'cookies'))
+ try:
+ self.cookiejar.load()
+ except IOError:
+ self.cookiejar.save()
+ self.cookiejar.load()
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cookiejar))
self._high_limits = None
def call(self, data):
data['format'] = 'json'
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cookiejar))
self._high_limits = None
def call(self, data):
data['format'] = 'json'
- request = urllib2.Request(self.api_url, urllib.urlencode(data))
+ user_agent = 'mw/%s +http://github.com/ianweller/mw' % mw.version
+ request = urllib2.Request(self.api_url, urllib.urlencode(data),
+ {'User-Agent': user_agent})
request.add_header('Accept-encoding', 'gzip')
response = self.opener.open(request)
request.add_header('Accept-encoding', 'gzip')
response = self.opener.open(request)
+ self.cookiejar.save()
if response.headers.get('Content-Encoding') == 'gzip':
compressed = StringIO(response.read())
gzipper = gzip.GzipFile(fileobj=compressed)
data = gzipper.read()
else:
data = response.read()
if response.headers.get('Content-Encoding') == 'gzip':
compressed = StringIO(response.read())
gzipper = gzip.GzipFile(fileobj=compressed)
data = gzipper.read()
else:
data = response.read()
- return json.loads(data)
+ the_data = json.loads(data)
+ if 'error' in the_data.keys():
+ raise APIError(the_data['error']['info'])
+ return the_data
def limits(self, low, high):
if self._high_limits == None:
def limits(self, low, high):
if self._high_limits == None:
@@
-59,6
+75,15
@@
class API(object):
return low
return low
+class APIError(Exception):
+
+ def __init__(self, info):
+ self.info = info
+
+ def __str__(self):
+ return self.info
+
+
def pagename_to_filename(name):
name = name.replace(' ', '_')
name = name.replace('/', '!')
def pagename_to_filename(name):
name = name.replace(' ', '_')
name = name.replace('/', '!')
Benjamin Mako Hill
||
Want to submit a patch?