2 # -*- coding: utf-8 -*-
4 # http://pywikipediabot.sourceforge.net/
5 # svn co http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia pywikipedia
11 from simplemediawiki import MediaWiki
14 # - any space names in page names need to be turned into '_'s
15 # - we need to de-interwikify any interwiki links given to Wiki:
16 # - normalize messages for non-unix linebreaks
19 def __init__(self, url):
21 self.wiki = MediaWiki(self.url + 'api.php')
23 def get_revid_for_date(self, page, date):
32 'rvprop' : 'timestamp|ids'})
33 pages = wc['query']['pages']
34 return pages[pages.keys()[0]]['revisions'][0]['revid']
36 def get_edit_token(self, page):
44 pages = wc['query']['pages']
45 page = pages[pages.keys()[0]]
46 return page['edittoken']
48 def get_talk_page(self, page):
50 (namespace, rest) = page.split(':', 1)
51 return '%s_talk:%s' % (namespace, rest)
53 return 'Talk:%s' % page
55 def get_page_text(self, page):
61 'rvprop' : 'content'})
62 pages = wc['query']['pages']
63 return pages[pages.keys()[0]]['revisions'][0]['revid']
65 #query&prop=revisions&rvprop=content&format=xml&titles=Main%20Page
68 def append_to_talk_page(self, page):
69 talk_page = self.get_talk_page(page)
70 edit_token = self.get_edit_token(page)
76 'token' : self.get_edit_token(page)})
79 pages = wc['query']['pages']
80 page = pages[pages.keys()[0]]
81 return page['edittoken']
85 def make_link(wiki, page, revision):
86 return '%s/index.php?title=%s&oldid=%s' % (wiki, page, revision)
89 fields = ['page', 'date', 'e-mail', 'export-date', 'redirect', 'page-revision', 'user-agent', 'username', 'wiki']
90 datefields = ['date', 'export-date']
92 def __init__(self, headers={}, body=''):
93 self.headers = headers
97 assembly = [('date', 'On %s,'),
98 ('username', ' [[User:%s]]'),
99 (' an anonymous user'),
100 ('e-mail', ' (<a href="mailto:%s">e-mail</a>)'),
101 (' made a comment',),
102 ('user-agent', ' (using %s)')
106 for stage in assembly:
109 elif self.headers.has_key(stage[0]):
110 comment += stage[1] % (self.headers[stage[0]])
111 elif len(stage) == 3:
114 return comment + ":\n\n" + self.body
117 def from_string(cls, s):
118 body_index = s.index('\n\n')
119 # headers = dict([(X[:X.index(':')], X[X.index(':')+1:].strip()) for X in s[:body_index].split('\n')])
120 kvs = [re.split(': *', key_value, 1) for key_value in s[:body_index].splitlines()]
121 headers = dict([(key.lower(), value) for (key, value) in kvs])
122 body = s[body_index+2:]
123 return cls(headers, body)
128 # defomrom_string(cls, s):
129 # body_index = s.index('\n\n')
130 # ders = dict([(X[:X.index(':')], X[X.index(':')+1:].strip()) for X in s[:body_index].split('\n')])
131 # kvs = [re.split(': *', key_value, 1) for key_value in s[:body_index].splitlines()]
132 # headers = dict([(key.lower(), value) for (key, value) in kvs])
134 # body = s[body_index+2:]
135 # return cls(headers, body)
138 def parse_edits(lines):
141 #message_body = False
144 if line.startswith('Page:'):
146 we = WikiEdit.from_string(acc)
148 print we, we.headers, we.body
155 # message_body = False
158 # message_body = True
164 # key_end = line.index(':')
165 # cur.headers[line[:key_end]] = line[key_end+1:].strip()
168 we = WikiEdit.from_string(acc)
170 print (we, we.headers, we.body)
174 if __name__ =='__main__':
175 # parse the .wpe file on standard
176 edits = parse_edits(sys.stdin)
179 # if there is an export date but no revision, lets go to wiki an
180 # find out what the revision id is
181 if edit.headers.has_key("export-date") and \
182 not edit.headers.has_key("page-revision"):
183 wiki = Wiki(edit.headers['wiki'])
184 revid = wiki.get_revid_for_date(edit.headers["page"], edit.headers["export-date"])
186 revid = edit.headers['page-revision']
189 # rvdir=newer&rvprop=timestamp|ids
191 edit_msg = edit.to_comment()
194 # iso8601.parse_date("2007-06-20T12:34:40+03:00")