Jump to content

User:PearBOT/Template cache/source

From Wikipedia, the free encyclopedia

This is the source code for PearBOT task 3 implementing template caches.

import pywikibot
import re
import datetime
import time
from pywikibot import pagegenerators
now = datetime.datetime.now()
site = pywikibot.Site('en', 'wikipedia')
errorpage = pywikibot.Page(site, u"User:PearBOT/Infobox request problems")
checkpage = pywikibot.Page(site, u"User:PearBOT/Infobox request config")
def minute():
    n = now.minute
    if n < 10:
        result = '%02d' % n
    else:
        result = str(n)
    return result
def months():
    if now.month == 1:
        return "January"
    elif now.month == 2:
        return "February"
    elif now.month == 3:
        return "March"
    elif now.month == 4:
        return "April"
    elif now.month == 5:
        return "May"
    elif now.month == 6:
        return "June"
    elif now.month == 7:
        return "July"
    elif now.month == 8:
        return "August"
    elif now.month == 9:
        return "September"
    elif now.month == 10:
        return "October"
    elif now.month == 11:
        return "November"
    elif now.month == 12:
        return "December"
def frequency(template):
    lastupdate=re.search("(?<=<!--Update timestamp:)\d*",page.text)
    if lastupdate:
        oldtime=float(lastupdate.group(0))
    curtime=time.time()
    if lastupdate:
        frequency=str(template[1].get("frequency"))
        if(template[1].get("frequency") is not "" and "frequency" in template[1].keys()):
            if(((float(curtime)-oldtime)*1.05)>(float(frequency)*3600)):
                return True
            else:
                return False
        else:
            if((curtime-oldtime)*1.05>24*3600):
                return True
            else:
                return False
    else:
        return True
    return False
updated="\n<small>Updated: %d" % now.hour+":%s, " % minute()   +"%d" %now.day +" "+ months() + " %d" % now.year + "</small>"
for page in pywikibot.pagegenerators.SearchPageGenerator('hastemplate:"Template cache"',namespaces=[4,100]):
    text=page.text
    templates=pywikibot.textlib.extract_templates_and_params(text,remove_disabled_parts="yes",strip = "yes")
    for template in templates:
        if template[0] == "Template cache" or template[0] == "template cache":
            if frequency(template):
                templatecall="{{subst:"
                templatecall+=template[1].get("1")
                loop=True
                parameter=2
                while loop:
                    if str(parameter) in template[1].keys():
                        templatecall += "|"
                        templatecall += template[1].get(str(parameter))
                        parameter+=1
                    else:
                        loop=False
                templatecall+="}}"
                templatecall=re.sub("{{=}}","=",templatecall)
                text=re.sub("(?<=<!--Template cache top-->)(a|[^a])*(?=<!--Template cache bottom-->)",templatecall,text)
                if template[1].get("noupdated") or template[1].get("noupdated") == "yes"or template[1].get("noupdated") == "Yes":
                    text=re.sub("<!--Template cache bottom-->","<noinclude>"+updated+"</noinclude><!--Update timestamp:"+str(int(time.time()))+"-->"+"<!--Template cache bottom-->",text)
                else:
                    text=re.sub("<!--Template cache bottom-->",updated+"<!--Update timestamp:"+str(int(time.time()))+"-->"+"<!--Template cache bottom-->",text)
                page.text=text
                page.save("Updated template cache.")