Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Python:Discussionbot.py

From Botwiki
Jump to: navigation, search

Discussionbot.py v3.7

License:

Copyright (c) 2007 Betacommand, Messedrocker, Greg Maxwell

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Source

#!/usr/bin/python
# -*-  coding: utf-8  -*-
import sys, wikipedia
import wikipedia, pagegenerators, catlib
import editarticle, time, os
import re, codecs, gc, threading
 
site = wikipedia.getSite()
def main():
  while 1<2:
   update(['RFCbio','rfcbio'],'Wikipedia:Requests for comment/Biographies',"{{RFC list footer|RFCbio|hide_instructions={{{hide_instructions}}} }}").start()
   update(['RFCecon','rfcecon'],'Wikipedia:Requests for comment/Economy, trade, and companies',"{{RFC list footer|RFCecon|hide_instructions={{{hide_instructions}}} }}").start()
   update(['RFChist','rfchist'],'Wikipedia:Requests for comment/History and geography',"{{RFC list footer|RFChist|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFClang','rfclang'],'Wikipedia:Requests for comment/Language and linguistics',"{{RFC list footer|RFClang|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCsci', 'rfcsci'],'Wikipedia:Requests for comment/Maths, science, and technology',"{{RFC list footer|RFCsci|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCmedia', 'rfcmedia', 'RFCart', 'rfcart'],'Wikipedia:Requests for comment/Art, architecture, literature and media',"{{RFC list footer|RFCmedia|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCpol', 'rfcpol'],'Wikipedia:Requests for comment/Politics',"{{RFC list footer|RFCpol|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCreli', 'rfcreli'],'Wikipedia:Requests for comment/Religion and philosophy',"{{RFC list footer|RFCreli|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCsoc', 'rfcsoc'],'Wikipedia:Requests for comment/Society, sports, law, and sex',"{{RFC list footer|RFCsoc|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCstyle', 'rfcstyle'],'Wikipedia:Requests for comment/Style issues',"{{RFC list footer|RFCstyle|hide_instructions={{{hide_instructions}}} }} ").start()
   update(['RFCpolicy', 'rfcpolicy'],'Wikipedia:Requests for comment/Policies',"{{RFC list footer|RFCpolicy|hide_instructions={{{hide_instructions}}} }} ").start()
   gc.collect() 
   time.sleep(1800)
 
class update(threading.Thread):
  def __init__ ( self,templates,post_template,bottomtxt):
        self.templates = templates
        self.post_template = post_template
        self.bottomtxt = bottomtxt
        threading.Thread.__init__ ( self )
 
  def run(self):
    pages=[]
    message=''
    dic={}
    templates = self.templates
    bottomtxt ='%s' % self.bottomtxt
    post_template = self.post_template
    if len(templates)==1:
      regex ='%s' % templates[0]
    else:
      regex='('
      part=''
      for template in templates:
        regex+= part + template
        part='|'
      regex+=')'
    for template in templates:
      pagegen = pagegenerators.ReferringPageGenerator(wikipedia.Page(site, 'Template:%s' % template), onlyTemplateInclusion = True)
      for page in pagegen:
        pages.append(page)
    pages = sorted(set(pages))
    message =u''
    if pages:
      for page in pages:
        Time,line = pageparse(page,regex)
        dic[Time]=line
      keys= dic.keys()
      keys.sort(reverse=True)
      for key in keys:
        message +=u'%s' % dic[key]
    else:
      message = u'* No discussions listed'
    pagetext= u"'''The following discussions are requested to have community-wide attention:'''\n%s\n\n%s" % (message,bottomtxt)
    templatepage = wikipedia.Page(site,'%s' % post_template )
    templatepage.put(pagetext, comment='Update, %s current discussions' % len(pages))
    wikipedia.output('Update, %s current discussions\n Sleeping for 30 minutes' % len(pages))
    gc.collect() 
def pageparse(page,regex):
  wikipedia.output(page.title())
  try:
    message =''
    text    = page.get()
    Time    = time.time()
    reason  = ''
    section = ''
    dic     = {}
    g = re.search('section(|\s)=.*?\!\!',text,re.I)
    g = g.group(0).split('=')[1]
    g = g.split('!')[0]
    dic['section'] = g.strip()
    g2 = re.search('reason(|\s)=.*?\!\!',text,re.I)
    g2 = g2.group(0).split('=')[1]
    g2 = g2.split('!')[0]
    dic['reason'] = g2.strip()
    g3 = re.search('time(|\s)=.*?\}\}',text,re.I)
    g3 = g3.group(0).split('=')[1]
    g3 = g3.split('}')[0]
    dic['time'] = g3.strip()
    if 'reason' in dic:
      reason = dic['reason']
    if 'time' in dic:
      Time = dic['time']
      edittime = time.strptime(Time, '%H:%S, %d %B %Y (UTC)')
      Time = time.mktime(edittime)
    if 'section' in dic:
      section = dic['section']
      section = re.sub(' ','_',section)
      section = re.sub('\[','',section)
      section = re.sub('\]','',section)
      link = u'\n* <strong>[[%s#%s|%s]]</strong> ' % (page.title(),section,page.title())
    else:
      link = u'\n* [[%s]] ' % page.title()
    if Time < (time.time()- 2592000):
      text = re.sub('\{\{'+regex+'(.*?)\}\}','',text)
      page.put(text,comment= 'removing old RFC' )
      gc.collect() 
      return '',''
    message =u'%s%s' % (link,reason)
    message = re.sub('&#124;','|',message)
    gc.collect() 
    return Time, message
  except: 
    text = page.get()
    text = re.sub('\{\{'+regex+'(.*?)\}\}','{{RFC error}}',text)
    page.put(text,'RFC error')
    gc.collect() 
    return '',''
 
if __name__ == '__main__':
  try:
    main()
  finally:
    wikipedia.stopme()
Personal tools
Share