User:PearBOT/Template cache/source

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

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.")

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.