cleaned up code and removed debugging statements master
authorBenjamin Mako Hill <mako@atdot.cc>
Fri, 8 Apr 2011 18:01:57 +0000 (14:01 -0400)
committerBenjamin Mako Hill <mako@atdot.cc>
Fri, 8 Apr 2011 18:01:57 +0000 (14:01 -0400)
backwash.py

index 31cc4699fb93ca77a453fc56c0b5076568a61b06..7373e22173a9712eac04ab6063e0bdbecfb05653 100644 (file)
@@ -1,20 +1,22 @@
 #!/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):
@@ -41,9 +43,8 @@ class Wiki:
             '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):
@@ -54,7 +55,8 @@ class Wiki:
             '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)
@@ -73,29 +75,24 @@ class Wiki:
             '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)
 
@@ -119,9 +116,9 @@ class WikiEdit:
         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)')
                     ]
 
@@ -137,7 +134,10 @@ class WikiEdit:
             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):
@@ -148,9 +148,6 @@ class WikiEdit:
         body = s[body_index+2:]
         return cls(headers, body)
 
-
-    
-
 def parse_edits(lines):
     edits = []
     acc = ""
@@ -158,14 +155,14 @@ def parse_edits(lines):
         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
 
@@ -174,24 +171,7 @@ if __name__ =='__main__':
     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")

Benjamin Mako Hill || Want to submit a patch?