From: Dafydd Harries Date: Fri, 8 Apr 2011 03:42:19 +0000 (-0400) Subject: snapshot from Gobby X-Git-Url: https://projects.mako.cc/source/backwash/commitdiff_plain/909b58c1f5a3d79d49c57cff224757511e2fc4fd?hp=dfc0b520a1cbd590a3bf2941c66e3b7556a6cb2d snapshot from Gobby --- diff --git a/backwash.py b/backwash.py index c712028..31cc469 100644 --- a/backwash.py +++ b/backwash.py @@ -9,6 +9,7 @@ import sys import re import iso8601 from simplemediawiki import MediaWiki +import interwiki # TODO: # - any space names in page names need to be turned into '_'s @@ -36,21 +37,33 @@ class Wiki: def get_edit_token(self, page): wc = self.wiki.call({ 'action' : 'query', - 'prop': 'info', - 'page': page, + 'prop': 'info|revisions', + 'titles': page, 'intoken' : 'edit'}) - import pprint - pprint.pprint(wc) pages = wc['query']['pages'] + import pprint + print 'edit token result:' + pprint.pformat(wc) page = pages[pages.keys()[0]] return page['edittoken'] + def normalize_title(self, page): + wc = self.wiki.call({ + 'action': 'query', + 'prop': 'info', + 'titles': page, + 'redirects': 'true'}) + pages = wc['query']['pages'] + page = pages[pages.keys()[0]] + return page['title'] + def get_talk_page(self, page): - if ':' in page: - (namespace, rest) = page.split(':', 1) - return '%s_talk:%s' % (namespace, rest) - else: - return 'Talk:%s' % page + normalized = self.normalize_title(page) + + if ':' in normalized: + (namespace, rest) = normalized.split(':', 1) + return '%s_talk:%s' % (namespace, rest) + else: + return 'Talk:%s' % page def get_page_text(self, page): wc = self.wiki.call({ @@ -60,58 +73,71 @@ class Wiki: 'redirects': 'true', 'rvprop' : 'content'}) pages = wc['query']['pages'] - return pages[pages.keys()[0]]['revisions'][0]['revid'] - - #query&prop=revisions&rvprop=content&format=xml&titles=Main%20Page - pass + # something here + page = pages[pages.keys()[0]] + return page['revisions'][0]['*'] - def append_to_talk_page(self, page): + def append_to_talk_page(self, page, text): talk_page = self.get_talk_page(page) - edit_token = self.get_edit_token(page) + print 'talk page: %r' % talk_page + edit_token = self.get_edit_token(talk_page) + print edit_token, "\n" wc = self.wiki.call({ 'action' : 'edit', 'title': talk_page, - 'bot' : 'true', - 'token' : self.get_edit_token(page)}) - import pprint - pprint.pprint(wc) - pages = wc['query']['pages'] - page = pages[pages.keys()[0]] - return page['edittoken'] +# 'bot' : 'true', + 'token' : edit_token, + 'not_minor': 'true', + 'section' : 'new', + 'summary': 'Suggestion from an offline user', + 'text': text }) - + #import pprint + #print 'edit result:' + pprint.pformat(wc) def make_link(wiki, page, revision): - return '%s/index.php?title=%s&oldid=%s' % (wiki, page, revision) + return '%sindex.php?title=%s&oldid=%s' % (wiki, page.replace(' ', '_'), revision) class WikiEdit: fields = ['page', 'date', 'e-mail', 'export-date', 'redirect', 'page-revision', 'user-agent', 'username', 'wiki'] datefields = ['date', 'export-date'] - def __init__(self, headers={}, body=''): + def __init__(self, headers={}, body='', normalize=True): self.headers = headers self.body = body + if normalize: + self.normalize_headers() + + def normalize_headers(self): + self.headers.setdefault('wiki', 'http://en.wikipedia.org/wiki/') + if self.headers['wiki'] in interwiki.mapping.keys(): + self.headers['wiki'] = interwiki.mapping[self.headers['wiki']] + def to_comment(self): - assembly = [('date', 'On %s,'), - ('username', ' [[User:%s]]'), - (' an anonymous user'), - ('e-mail', ' (e-mail)'), + assembly = [('date', ' on %s,'), + ('username', ' [[User:%s]]', ' an anonymous user'), + ('e-mail', ' (e-mail: %s)'), (' made a comment',), + ('page-revision', + lambda x: ' on [' + make_link(self.headers['wiki'], self.headers['page'], self.headers['page-revision']) + 'this revision]'), ('user-agent', ' (using %s)') ] - comment = ": " + comment = "" for stage in assembly: if len(stage) == 1: comment += stage[0] elif self.headers.has_key(stage[0]): - comment += stage[1] % (self.headers[stage[0]]) + if isinstance(stage[1], str): + comment += stage[1] % (self.headers[stage[0]]) + else: + comment += stage[1](stage[0]) elif len(stage) == 3: comment += stage[2] - return comment + ":\n\n" + self.body + return ': ' + comment.strip().capitalize() + ":\n\n" + self.body @classmethod def from_string(cls, s): @@ -124,46 +150,18 @@ class WikiEdit: - # @classmethod - # defomrom_string(cls, s): - # body_index = s.index('\n\n') - # ders = dict([(X[:X.index(':')], X[X.index(':')+1:].strip()) for X in s[:body_index].split('\n')]) - # kvs = [re.split(': *', key_value, 1) for key_value in s[:body_index].splitlines()] - # headers = dict([(key.lower(), value) for (key, value) in kvs]) - # print headers - # body = s[body_index+2:] - # return cls(headers, body) - def parse_edits(lines): edits = [] - #cur = WikiEdit() - #message_body = False acc = "" for line in lines: - if line.startswith('Page:'): - if '\n\n' in acc: - we = WikiEdit.from_string(acc) - edits.append(we) - print we, we.headers, we.body - #sys.exit(0) - acc = "" + if '\n\n' in acc and line.startswith('Page:'): + we = WikiEdit.from_string(acc) + edits.append(we) + print we, we.headers, we.body + acc = "" acc += line - # cur = WikiEdit() - # edits.append(cur) - # message_body = False - - # if line == '\n': - # message_body = True - - # elif message_body: - # cur.body += line - - # else: - # key_end = line.index(':') - # cur.headers[line[:key_end]] = line[key_end+1:].strip() - if acc != '': we = WikiEdit.from_string(acc) edits.append(we) @@ -178,17 +176,22 @@ if __name__ =='__main__': for edit in edits: # if there is an export date but no revision, lets go to wiki an # find out what the revision id is - if edit.headers.has_key("export-date") and \ - not edit.headers.has_key("page-revision"): - wiki = Wiki(edit.headers['wiki']) - revid = wiki.get_revid_for_date(edit.headers["page"], edit.headers["export-date"]) - else: - revid = edit.headers['page-revision'] + # if edit.headers.has_key("export-date") and \ + # not edit.headers.has_key("page-revision"): + # wiki = Wiki(edit.headers['wiki']) + # revid = wiki.get_revid_for_date(edit.headers["page"], edit.headers["export-date"]) + # else: + # revid = edit.headers['page-revision'] + + # print revid + # # rvdir=newer&rvprop=timestamp|ids - print revid - # rvdir=newer&rvprop=timestamp|ids + # edit_msg = edit.to_comment() + # print edit_msg - edit_msg = edit.to_comment() - print edit_msg + wiki = Wiki(edit.headers['wiki']) + #print wiki.get_page_text(edit.headers['page']) + wiki.append_to_talk_page(edit.headers['page'], edit.to_comment()) + # iso8601.parse_date("2007-06-20T12:34:40+03:00")