2 # -*- coding: utf-8 -*-
6 from simplemediawiki import MediaWiki
11 # - the bot should loginto the system. the simplemediawiki library
12 # should help us with that
13 # - we need to de-interwikify any interwiki links given to Wiki
14 # - the interwiki stuff isn't working for a few reason. i think we should turn it off
15 # we should only add interwiki links to the api
16 # we should only do it when we create an account on the wiki for the bot
17 # - normalize messages for non-unix linebreaks
18 # - get_revid_for_date is not being called at the moment a date w/o a
19 # a revision is not showing up
22 def __init__(self, url):
24 self.wiki = MediaWiki(self.url + 'api.php')
26 def get_revid_for_date(self, page, date):
35 'rvprop' : 'timestamp|ids'})
36 pages = wc['query']['pages']
37 return pages[pages.keys()[0]]['revisions'][0]['revid']
39 def get_edit_token(self, page):
42 'prop': 'info|revisions',
45 pages = wc['query']['pages']
46 page = pages[pages.keys()[0]]
48 return page['edittoken']
50 def normalize_title(self, page):
56 pages = wc['query']['pages']
57 page = pages[pages.keys()[0]]
59 return page['title'].replace(" ", "_")
61 def get_talk_page(self, page):
62 normalized = self.normalize_title(page)
65 (namespace, rest) = normalized.split(':', 1)
66 return '%s_talk:%s' % (namespace, rest)
68 return 'Talk:%s' % page
70 def get_page_text(self, page):
76 'rvprop' : 'content'})
77 pages = wc['query']['pages']
79 page = pages[pages.keys()[0]]
80 return page['revisions'][0]['*']
82 def append_to_talk_page(self, page, text):
83 talk_page = self.get_talk_page(page)
84 edit_token = self.get_edit_token(talk_page)
93 'summary': 'Suggestion from an offline user (via [[User:BackWashBot|BackWashBot]])',
96 def make_link(wiki, page, revision):
97 return '%sindex.php?title=%s&oldid=%s' % (wiki, page.replace(' ', '_'), revision)
100 fields = ['page', 'date', 'e-mail', 'export-date', 'redirect', 'page-revision', 'user-agent', 'username', 'wiki']
101 datefields = ['date', 'export-date']
103 def __init__(self, headers={}, body='', normalize=True):
104 self.headers = headers
108 self.normalize_headers()
110 def normalize_headers(self):
111 self.headers.setdefault('wiki', 'http://en.wikipedia.org/wiki/')
112 if self.headers['wiki'] in interwiki.mapping.keys():
113 self.headers['wiki'] = interwiki.mapping[self.headers['wiki']]
115 def to_comment(self):
116 assembly = [('date', ' on %s,'),
117 ('username', ' [[User:%s]]', ' an anonymous user'),
118 ('e-mail', ' (e-mail: %s)'),
119 (' made the following comment',),
121 lambda x: ' on [' + make_link(self.headers['wiki'], self.headers['page'], self.headers['page-revision']) + ' this revision]'),
122 ('user-agent', ' (using %s)')
126 for stage in assembly:
129 elif self.headers.has_key(stage[0]):
130 if isinstance(stage[1], str):
131 comment += stage[1] % (self.headers[stage[0]])
133 comment += stage[1](stage[0])
134 elif len(stage) == 3:
137 comment = comment.strip()
138 return ":''" + comment[0].capitalize() + comment[1:] + \
139 " using an offline wikireader:''\n\n" + self.body.strip() + \
140 "\n\n—via [[User:BackWashBot|BackWashBot]] ~~~~~"
143 def from_string(cls, s):
144 body_index = s.index('\n\n')
145 # headers = dict([(X[:X.index(':')], X[X.index(':')+1:].strip()) for X in s[:body_index].split('\n')])
146 kvs = [re.split(': *', key_value, 1) for key_value in s[:body_index].splitlines()]
147 headers = dict([(key.lower(), value) for (key, value) in kvs])
148 body = s[body_index+2:]
149 return cls(headers, body)
151 def parse_edits(lines):
155 if '\n\n' in acc and line.startswith('Page:'):
156 we = WikiEdit.from_string(acc)
158 #print we, we.headers, we.body
163 we = WikiEdit.from_string(acc)
165 #print (we, we.headers, we.body)
169 if __name__ =='__main__':
170 # parse the .wpe file on standard
171 edits = parse_edits(sys.stdin)
174 wiki = Wiki(edit.headers['wiki'])
175 wiki.append_to_talk_page(edit.headers['page'], edit.to_comment())