#!/usr/bin/env python
# -*- coding: utf-8 -*-
-# http://pywikipediabot.sourceforge.net/
-# svn co http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia pywikipedia
-
-
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
-# - we need to de-interwikify any interwiki links given to Wiki:
+# TODO/bugs:
+
+# - the bot should loginto the system. the simplemediawiki library
+# should help us with that
+# - we need to de-interwikify any interwiki links given to Wiki
+# - the interwiki stuff isn't working for a few reason. i think we should turn it off
+# we should only add interwiki links to the api
+# we should only do it when we create an account on the wiki for the bot
# - normalize messages for non-unix linebreaks
+# - get_revid_for_date is not being called at the moment a date w/o a
+# a revision is not showing up
class Wiki:
def __init__(self, url):
'titles': page,
'intoken' : 'edit'})
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):
'redirects': 'true'})
pages = wc['query']['pages']
page = pages[pages.keys()[0]]
- return page['title']
+
+ return page['title'].replace(" ", "_")
def get_talk_page(self, page):
normalized = self.normalize_title(page)
'redirects': 'true',
'rvprop' : 'content'})
pages = wc['query']['pages']
- # something here
+ # something here:LP
page = pages[pages.keys()[0]]
return page['revisions'][0]['*']
def append_to_talk_page(self, page, text):
talk_page = self.get_talk_page(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',
+ 'bot' : 'true',
'token' : edit_token,
- 'not_minor': 'true',
+ 'notminor': 'true',
'section' : 'new',
- 'summary': 'Suggestion from an offline user',
+ 'summary': 'Suggestion from an offline user (via [[User:BackWashBot|BackWashBot]])',
'text': text })
- #import pprint
- #print 'edit result:' + pprint.pformat(wc)
-
def make_link(wiki, page, revision):
return '%sindex.php?title=%s&oldid=%s' % (wiki, page.replace(' ', '_'), revision)
assembly = [('date', ' on %s,'),
('username', ' [[User:%s]]', ' an anonymous user'),
('e-mail', ' (e-mail: %s)'),
- (' made a comment',),
+ (' made the following comment',),
('page-revision',
- lambda x: ' on [' + make_link(self.headers['wiki'], self.headers['page'], self.headers['page-revision']) + 'this revision]'),
+ lambda x: ' on [' + make_link(self.headers['wiki'], self.headers['page'], self.headers['page-revision']) + ' this revision]'),
('user-agent', ' (using %s)')
]
elif len(stage) == 3:
comment += stage[2]
- return ': ' + comment.strip().capitalize() + ":\n\n" + self.body
+ comment = comment.strip()
+ return ":''" + comment[0].capitalize() + comment[1:] + \
+ " using an offline wikireader:''\n\n" + self.body.strip() + \
+ "\n\n—via [[User:BackWashBot|BackWashBot]] ~~~~~"
@classmethod
def from_string(cls, s):
body = s[body_index+2:]
return cls(headers, body)
-
-
-
def parse_edits(lines):
edits = []
acc = ""
if '\n\n' in acc and line.startswith('Page:'):
we = WikiEdit.from_string(acc)
edits.append(we)
- print we, we.headers, we.body
+ #print we, we.headers, we.body
acc = ""
acc += line
if acc != '':
we = WikiEdit.from_string(acc)
edits.append(we)
- print (we, we.headers, we.body)
+ #print (we, we.headers, we.body)
return edits
edits = parse_edits(sys.stdin)
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']
-
- # print revid
- # # rvdir=newer&rvprop=timestamp|ids
-
- # 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")