跳转到内容

User:Sz-iwbot/Discussionbot.py

维基百科,自由的百科全书

修改自Discussionbot.py v2.7

indiscussionbot.py v1.0

--百無一用是書生 () 2008年3月5日 (三) 17:04 (UTC)

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, codecs
import wikipedia, pagegenerators, catlib, config
import time, os
import re, gc
 
site = wikipedia.getSite()
def run():
  n=0
  pages=[]
  dic={}
  template = 'indiscussion'
  regex ='%s' % template
  
  s=wikipedia.Page(site, 'Template:%s' % template)
  
  pages = [page for page in s.getReferences(onlyTemplateInclusion=True)]
  for page in pages:
    pageparse(page,regex)
    n=n+1

  t=wikipedia.Page(site, 'Template:CurrentDiscussion')
  c= u"<font color=red>%s</font>项<noinclude>\n----\n参见[[:Category:進行中的討論|進行中的討論]]\n[[category:維基站務模板|C]]\n</noinclude>" % n
  comment= u"更新: 当前有 %s 项专题讨论" % n
  wikipedia.output('update: Current %s Discussion' % n)
  t.put(c, comment, minorEdit=False)
  gc.collect() 
def pageparse(page,regex):
  wikipedia.output('[[%s]]' % page.title())
  try:
    talktext    = page.get()
    g3 = re.search('\{\{indiscussion(|)(.*?)\}\}',talktext,re.I)
    gi=g3.group(0)
    if page.isTalkPage():
    
        
        Time=0
        g3 = gi.split('|')[1]
        g3 = g3.split('}')[0]
        Time = int(g3)
        Time= time.gmtime()[1] - Time
        if Time < 0:
          Time= Time +12
        if Time >2:
          talktext = re.sub('\{\{'+regex+'(|)(.*?)\}\}','',talktext)
          comment= 'remove old indiscussion template'
          page.put(talktext, comment)
          wikipedia.output(comment)
          gc.collect() 
          return '',''
    else:
        text = re.sub(gi,'',text)
        comment= 'remove indiscussion template, Plese use in Talk Page.'
        page.put(text, comment)
        wikipedia.output(comment)
        gc.collect() 
        return '',''
        
    gc.collect() 
    return Time
  except:
    text = page.get()
    text = re.sub('\{\{'+regex+'(.*?)\}\}','{{indiscussion|'+str(time.gmtime()[1])+'}}',text)
    comment= 'fix {{indiscussion}}'
    page.put(text, comment)
    wikipedia.output(comment)
    gc.collect() 
    return '',''
 
try:
    run()

finally:
    wikipedia.stopme()