1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Clément Schreiner
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from urlparse import urlsplit, urljoin
import urllib
import datetime
import re
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from weboob.tools.json import json as simplejson
from weboob.capabilities.content import Revision
__all__ = ['MediawikiBrowser']
class APIError(Exception):
pass
# Browser
class MediawikiBrowser(BaseBrowser):
ENCODING = 'utf-8'
def __init__(self, url, apiurl, *args, **kwargs):
url_parsed = urlsplit(url)
self.PROTOCOL = url_parsed.scheme
self.DOMAIN = url_parsed.netloc
self.BASEPATH = url_parsed.path
if self.BASEPATH.endswith('/'):
self.BASEPATH = self.BASEPATH[:-1]
self.apiurl = apiurl
BaseBrowser.__init__(self, *args, **kwargs)
def url2page(self, page):
baseurl = self.PROTOCOL + '://' + self.DOMAIN + self.BASEPATH
m = re.match('^' + urljoin(baseurl, 'wiki/(.+)$'), page)
if m:
return m.group(1)
else:
return page
def get_wiki_source(self, page):
assert isinstance(self.apiurl, basestring)
page = self.url2page(page)
data = {'action': 'query',
'prop': 'revisions|info',
'titles': page,
'rvprop': 'content|timestamp',
'rvlimit': '1',
'intoken': 'edit',
}
result = self.API_get(data)
pageid = result['query']['pages'].keys()[0]
if pageid == "-1": # Page does not exist
return ""
return result['query']['pages'][str(pageid)]['revisions'][0]['*']
def get_token(self, page, _type):
''' _type can be edit, delete, protect, move, block, unblock, email or import'''
if len(self.username) > 0 and not self.is_logged():
self.login()
data = {'action': 'query',
'prop': 'info',
'titles': page,
'intoken': _type,
}
result = self.API_get(data)
pageid = result['query']['pages'].keys()[0]
return result['query']['pages'][str(pageid)][_type + 'token']
def set_wiki_source(self, content, message=None, minor=False):
if len(self.username) > 0 and not self.is_logged():
self.login()
page = content.id
token = self.get_token(page, 'edit')
data = {'action': 'edit',
'title': page,
'token': token,
'text': content.content.encode('utf-8'),
'summary': message,
}
if minor:
data['minor'] = 'true'
self.API_post(data)
def get_wiki_preview(self, content, message=None):
data = {'action': 'parse',
'title': content.id,
'text': content.content.encode('utf-8'),
'summary': message,
}
result = self.API_post(data)
return result['parse']['text']['*']
def is_logged(self):
data = {'action': 'query',
'meta': 'userinfo',
}
result = self.API_get(data)
return result['query']['userinfo']['id'] != 0
def login(self):
assert isinstance(self.username, basestring)
assert isinstance(self.password, basestring)
assert isinstance(self.apiurl, basestring)
data = {'action': 'login',
'lgname': self.username,
'lgpassword': self.password,
}
result = self.API_post(data)
if result['login']['result'] == 'WrongPass':
raise BrowserIncorrectPassword()
if result['login']['result'] == 'NeedToken':
data['lgtoken'] = result['login']['token']
self.API_post(data)
def iter_wiki_revisions(self, page, nb_entries):
"""
Yield 'Revision' objects for the last <nb_entries> revisions of the specified page.
"""
if len(self.username) > 0 and not self.is_logged():
self.login()
data = {'action': 'query',
'titles': page,
'prop': 'revisions',
'rvprop': 'ids|timestamp|comment|user|flags',
'rvlimit': str(nb_entries),
}
result = self.API_get(data)
pageid = str(result['query']['pages'].keys()[0])
if pageid != "-1":
for rev in result['query']['pages'][pageid]['revisions']:
rev_content = Revision(str(rev['revid']))
rev_content.comment = rev['comment']
rev_content.author = rev['user']
rev_content.timestamp = datetime.datetime.strptime(rev['timestamp'], '%Y-%m-%dT%H:%M:%SZ')
rev_content.minor = 'minor' in rev
yield rev_content
def home(self):
# We don't need to change location, we're using the JSON API here.
pass
def check_result(self, result):
if 'error' in result:
raise APIError('%s' % result['error']['info'])
def API_get(self, data):
"""
Submit a GET request to the website
The JSON data is parsed and returned as a dictionary
"""
data['format'] = 'json'
result = simplejson.loads(self.readurl(self.buildurl(self.apiurl, **data)), 'utf-8')
self.check_result(result)
return result
def API_post(self, data):
"""
Submit a POST request to the website
The JSON data is parsed and returned as a dictionary
"""
data['format'] = 'json'
result = simplejson.loads(self.readurl(self.apiurl, urllib.urlencode(data)), 'utf-8')
self.check_result(result)
return result
|