Riley_Huntley

Untitled

Apr 29th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8  -*-
  3. import sys
  4. import catlib
  5. import pagegenerators
  6. import wikipedia
  7. import codecs
  8. import os
  9. import re
  10. import time
  11.  
  12. template_skip_list = [
  13.                         u'extant organization',
  14.                         u'COI editnotice',
  15.                         u'Disambiguation',
  16.                         u'Disambiguation page',
  17.                         u'Surname',
  18.                         u'Sportindex',
  19.                         u'Given name',
  20.                         u'DAB',
  21.                         u'Dab',
  22.                         u'Sia',
  23.                         u'Set index',
  24.                         u'SIA',
  25.                         u'Set index article',
  26.                         u'Dbig',
  27.                         u'Disam',
  28.                         u'Disamb',
  29.                         u'Disambig',
  30.                         u'Hndis',
  31.                         u'Hospital disambiguation',
  32.                         u'Mathematical disambiguation',
  33.                         u'Mountain index',
  34.                         u'Roadindex',
  35.                         u'School disambiguation',
  36.                         u'Shipindex',
  37.                         u'Mountain index',
  38.                         u'Disambiguation cleanup',
  39.                     ]
  40. template_skip_regex = re.compile(ur'\{(Template:)?('+u'|'.join(template_skip_list)+u')',re.I)
  41. skip_these = [u'Organization',]
  42. title_blacklist = [
  43.                     u'list',
  44.                     u'disambiguation',
  45.                   ]
  46. title_blacklist_regex = re.compile(ur'(%s)' % u'|'.join(title_blacklist),re.I)
  47. site = wikipedia.getSite()
  48.  
  49. log_page = wikipedia.Page(site,u'User:RileyBot/Logs/11')
  50. stop_page = wikipedia.Page(site,u'User:RileyBot/Stop/11')
  51.  
  52. def main():
  53.     check_page()
  54.     log('Run started')
  55.     cat = catlib.Category(site,'Category:Companies based in California')
  56.     gen = pagegenerators.CategorizedPageGenerator(cat)
  57.     for page in gen:
  58.         if page.namespace() == 0:
  59.             if page.title() not in skip_these and not title_blacklist_regex.search(page.title()):
  60.                 if page.exists():
  61.                     if not page.isRedirectPage():
  62.                         talk_page = page.toggleTalkPage()
  63.                         try:
  64.                             text = talk_page.get()
  65.                         except wikipedia.NoPage:
  66.                             text = u''
  67.                         if not template_skip_regex.search(text):
  68.                             newtext = '{{COI editnotice}}\n%s' % (text)
  69.                             try:
  70.                                 check_page()
  71.                                 talk_page.put(newtext, comment=u'[[User:RileyBot|Bot]] trial: Added [[Template:COI editnotice]] to [[%s]]) ([[User:RileyBot/11|Task 11]] - [[User:RileyBot/Stop/11|disable]]' % page.title(), watchArticle = False, minorEdit = True)
  72.                                 log(u'Saved edit on [[%s]]' % talk_page.title())
  73.                             except wikipedia.LockedPage:
  74.                                 log(u"Page %s is locked; skipping." % talk_page.title())
  75.                             except wikipedia.EditConflict:
  76.                                 log(u'Skipping %s because of edit conflict' % (talk_page.title()))
  77.                             except:
  78.                                 log(u'Skipping %s because of unknown error' % (talk_page.title()))
  79.                         else:
  80.                             log('[[%s]] ignored due to regular expression' % talk_page.title())
  81.                     else:
  82.                         log('Page %s is a redirect; skipping.' % page.title())
  83.                 else:
  84.                     log('Page %s does not exist; skipping.' % page.title())
  85.             else:
  86.                 log('Page %s was in skip list.' % page.title())
  87.         else:
  88.             log('Page %s is not in the article namespace.' % page.title())
  89.     shut_down()
  90. def check_page():
  91.     text = stop_page.get(force=True)
  92.     if text.lower() != u'enable':
  93.         log('Check page disabled')
  94.         shut_down()
  95.        
  96. def shut_down():
  97.     log(u'Run Ended')
  98.     f = codecs.open('RileyBot11.txt','r', 'utf-8')
  99.     log_text = f.read()
  100.     f.close()
  101.     text = log_page.get()
  102.     log_page.put(log_text,'Uploading logs for [[User:RileyBot/11|Task 11]]')
  103.     sys.exit(0)
  104.  
  105. def log(text):
  106.     tm = time.strftime(u'%Y-%m-%d %H:%M:%S',time.gmtime())
  107.     print tm + ": " + text
  108.     f3 = codecs.open('RileyBot11.txt', 'a', 'utf-8')
  109.     f3.write('\n* %s\t%s' % (tm,text))
  110.     f3.close()
  111.  
  112. if __name__ == "__main__":
  113.     main()
Advertisement
Add Comment
Please, Sign In to add comment