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")
page = pywikibot.Page(site, u"User:PearBOT/test")
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)
    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()):
            print("freq")
            if(((float(curtime)-oldtime)*1.05)>(float(frequency)*3600)):
                return True
            else:
                return False
        else:
            print("no freq")
            if((curtime-oldtime)*1.05>0*3600):
                return True
            else:
                return False
    else:
        return True
    return False
updated="\n<small>Updated: %d" % now.hour+":%d, " % now.minute+"%d" %now.day +" "+ months() + " %d" % now.year + "</small>"
print("test")
for page in pywikibot.pagegenerators.SearchPageGenerator('hastemplate:"User:PearBOT/Template cache"'):
    print(page.full_url())
    print("test")
    text=page.text
    templates=pywikibot.textlib.extract_templates_and_params(text,remove_disabled_parts="yes",strip = "yes")
    for template in templates:
        print("test")
        if template[0] == "User:PearBOT/Template cache":
            print("test")
            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)
                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")
test
https://en.wikipedia.org/wiki/User%3APearBOT%2Ftest
test
test
test
freq
test
https://en.wikipedia.org/wiki/Portal%3AAsia%2FNews
test
test
test
freq
test