Riley_Huntley

Untitled

Apr 29th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.69 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8  -*-
  3. import sys
  4. import pagegenerators
  5. import codecs
  6. import os
  7. import re
  8. import time
  9. import wikipedia
  10. # array of strings to search for and exclude those from re-tagging
  11. regex_skip    =  [
  12.                   u'prod',
  13.                   u'proposal to delete',
  14.                   u'proposed deletion',
  15.                  ]
  16. regex_skip    = ur'\{(Template:)?('+u'|'.join(regex_skip)+u')'
  17. REGEX         = re.compile(regex_skip, flags=re.IGNORECASE)
  18. site          = wikipedia.getSite()
  19. # page where run logs are uploaded to
  20. log_page      = wikipedia.Page(site,u'User:RileyBot/Logs/10')
  21. # links from this page are what get tagged
  22. source_page   = wikipedia.Page(site,u'User:RileyBot/Trial/10')
  23. # automated shutdown feature
  24. stop_page     = wikipedia.Page(site,u'User:RileyBot/Stop/10')
  25. # PROD reason
  26. tag_reason    = u'Non-notable diplomat stub article. For the relevant notability policy, please see [[Wikipedia:Notability_(people)#Diplomats|Wikipedia:Notability (people)#Diplomats]].'
  27. # user talk page notice
  28. warn_template = u'{{subst:Proposed deletion notify|%s|concern='+tag_reason+'}} ~~~~'
  29.  
  30. def main():
  31.   log('Run started')
  32.   # get a list of pages to tag
  33.   pages_to_work_on = [x for x in pagegenerators.NamespaceFilterPageGenerator(source_page.linkedPages(),[0])]
  34.   for page in pages_to_work_on:
  35.     # is the bot allowed to do this task?
  36.     check_page()
  37.     # if page has already been deleted, log and skip
  38.     if page.exists():
  39.       # check if it is a redirect, if it is log and skip
  40.       if not page.isRedirectPage():
  41.         # get page contents
  42.         text = page.get()
  43.         # check to see if there is a template on the black list, if it is log and skip
  44.         if not REGEX.search(text):
  45.           # add PROD notice
  46.           newtext = '{{subst:Proposed deletion|%s}}\n%s' % (tag_reason,text)
  47.           try:
  48.             # try and save the page and log it
  49.             page.put(newtext, comment=u'[[User:RileyBot|Bot]] trial: Nominating [[%s]] for [[WP:proposed deletion|proposed deletion]] by request of [[User:Kleinzach|Kleinzach]].) ([[User:RileyBot/10|Task 10]]' % page.title(), watchArticle = False, minorEdit = True)
  50.             log(u'Tagging [[%s]]' % page.title())
  51.           # page is protected
  52.           except wikipedia.LockedPage:
  53.             log(u"Page %s is locked; skipping." % page.title())
  54.           # Edit conflicted with another user
  55.           except wikipedia.EditConflict:
  56.             log(u'Skipping %s because of edit conflict' % (page.title()))
  57.           # some unknown error has occurred, log and review at a latter date
  58.           except:
  59.             log(u'Skipping %s because of unknown error' % (page.title()))
  60.           # page has been tagged for PROD now need to notify the creator
  61.           warn_user(page)
  62.         else:
  63.           log('[[%s]] ignored due to regular expression' % page.title())
  64.       else:
  65.         log('Page %s is a redirect; skipping.' % page.title())
  66.     else:
  67.       log('Page %s does not exist; skipping.' % page.title())
  68.   shut_down()
  69.  
  70. def check_page():
  71.   # get check page and if contents are not 'enabled' have the bot shutdown as someone has disabled the bot
  72.   text = stop_page.get(force=True)
  73.   if text.lower() != u'enable':
  74.     log('Check page disabled')
  75.     shut_down()
  76. # notfy a user about a page that has been tagged for deletion, dont check the stop page as we want to avoid not notifying the creator of the talk page
  77. def warn_user(page):
  78.   # get the first person to edit the page not 100% reliable but is the best for identifying who created a page
  79.   creator,ts = page.getCreator()
  80.   # create a page object for their talk page
  81.   talk_page = wikipedia.Page(site,u'User talk:%s' % creator)
  82.   # due to renames, switching accounts and what not sometimes the talk page has been redirected to a different username if so find out and use that as the correct page
  83.   if talk_page.isRedirectPage():
  84.     talk_page = talk_page.getRedirectTarget()
  85.     if not talk_page.isTalkPage():
  86.       # for some reason the new talk page isnt a talk page (banned/blocked user, whos talk page redirects to their user page ect)
  87.       log(u'Unable to locate talk page for [[User:%s]]' % creator)
  88.       return
  89.   # format talk page notice
  90.   warn_text = warn_template % (page.title())
  91.   # get user talk page contents
  92.   text = talk_page.get()
  93.   # combine the two
  94.   text+=u"\n%s" % warn_text
  95.   try:
  96.     # save the talk page and log the details
  97.     talk_page.put(text, u"[[User:RileyBot|Bot]] notification: Proposed deletion of [[%s]].) ([[User:RileyBot/10|Task 10]]" % page.title())
  98.     log(u'Notifying [[User:%s]] about [[%s]]' % (creator,page.title()))
  99.   except wikipedia.LockedPage:
  100.     log(u"Page %s is locked; skipping." % (talk_page.title()))
  101.   except wikipedia.EditConflict:
  102.     log(u'Skipping %s because of edit conflict' % (talk_page.title()))
  103.   except:
  104.     log(u'Skipping %s because of unknown error' % (talk_page.title()))
  105.   return
  106. # used to wrap up and cleanly exit the program, can be called from mutiple locations within the program
  107. def shut_down():
  108.   # log shutdown
  109.   log(u'Run Ended')
  110.   # get the contents of the log file
  111.   f = codecs.open('RileyBot10.txt','r', 'utf-8')
  112.   log_text = f.read()
  113.   f.close()
  114.   text = log_page.get()
  115.   # post the log file to the wiki log page
  116.   log_page.put(log_text,'Uploading logs for [[User:RileyBot/10|Task 10]]')
  117.   sys.exit(0)
  118. # general logging function
  119. def log(text):
  120.   # timestamp every item to make tracking easier, also use UTC time to avoid local issues
  121.   tm = time.strftime(u'%Y-%m-%d %H:%M:%S',time.gmtime())
  122.   f3 = codecs.open('RileyBot10.txt', 'a', 'utf-8')
  123.   # post every item to its own line
  124.   f3.write('\n* %s\t%s' % (tm,text))
  125.   f3.close()
  126.  
  127. if __name__ == "__main__":
  128.   main()
Advertisement
Add Comment
Please, Sign In to add comment