import pywikibot
import re
from pywikibot import pagegenerators
site = pywikibot.Site('en', 'wikipedia')
checkpage = pywikibot.Page(site, u"User:PearBOT/Infobox request/config")
linkpage = pywikibot.Page(site, u"User:PearBOT/linkpage")
logpage =  pywikibot.Page(site, u"User:PearBOT/logpage")
allregex = "{{\s?([iI]nfobox [^|]*)"
generator=pagegenerators.GeneratorFactory()
notinfoboxregex = "\s*[mM]apframe"

def infoboxregex(template,project):
    if project[1].get("infoboxes") is not "" and "infoboxes" in project[1].keys():
        if re.search(re.sub("\&","|",project[1].get(re.escape("infoboxes"))),template[0]):
            return True
        else:
            return False
    else:
        if re.search(allregex,template[0]) and not re.search(allregex+notinfoboxregex,template[0]):
            return True
        else:
            return False

def removerequest(talkpage,project):
    talkpagetext=talkpage.text
    talkpagetemplates = pywikibot.textlib.extract_templates_and_params(talkpage.text,remove_disabled_parts="yes",strip = "yes")
    removeregex = "({{\s?"+re.sub("\&","|",re.escape(project[1].get("template")))+"[^{}]*)\s?\|\s?"+re.sub("\&","|",re.escape(project[1].get("parameter")))+"[^\|}]*"
    if not re.search(page.full_url(),logpage.text):
        for template in talkpagetemplates:
            if re.search(re.sub("\&","|",re.escape(project[1].get("template"))),template[0]):
                talkpagetext=re.sub(removeregex,re.search(removeregex,talkpagetext).group(1),talkpagetext)
                talkpage.text=talkpagetext
                logpage.text+="\n"+page.full_url()
                talkpage.save("Removed infobox request since the article has an infobox. ([[Wikipedia:Bots/Requests for approval/PearBOT 2|Task 2]])")

checkpagetemplates=pywikibot.textlib.extract_templates_and_params(checkpage.text,remove_disabled_parts="yes",strip = "yes")
for project in checkpagetemplates:
    print(checkpagetemplates)
    if project[0] == "User:PearBOT/Infobox request/config/template":
        print(project)
        for page in pywikibot.pagegenerators.SearchPageGenerator('Talk:incategory:"'+project[1].get("category")+'"'):
            article = page.toggleTalkPage()
            talkpage= page
            articletext=re.sub("==(a|[^a])*","",article.text)
            articletemplates = pywikibot.textlib.extract_templates_and_params(articletext,remove_disabled_parts="yes",strip = "yes")
            for template in articletemplates:
                if infoboxregex(template,project):
                    removerequest(talkpage,project)
('User:PearBOT/Infobox request/config/template', OrderedDict([('category', 'Journal articles needing infoboxes'), ('template', 'WikiProject Academic Journals'), ('infoboxes', 'Infobox journal'), ('parameter', 'needs-infobox')]))
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
Page [[Talk:Bridging Eastern and Western Psychiatry]] saved
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
({{\s?WikiProject\ Academic\ Journals[^{}]*)\s?\|\s?needs\-infobox[^\|}]*
('User:PearBOT/Infobox request/config/template', OrderedDict([('category', 'Radio articles needing infoboxes'), ('template', 'WikiProject Radio'), ('parameter', 'needs-infobox')]))
('User:PearBOT/Infobox request/config/template', OrderedDict([('category', 'Amateur radio articles needing infoboxes'), ('template', 'WikiProject Radio Stations'), ('infoboxes', 'Infobox radio station'), ('parameter', 'needs-infobox')]))
('User:PearBOT/Infobox request/config/template', OrderedDict([('category', 'Amateur radio articles needing infoboxes'), ('template', 'WikiProject Amateur radio'), ('parameter', 'needs-infobox')]))
('User:PearBOT/Infobox request/config/template', OrderedDict([('category', 'Company articles needing infoboxes'), ('template', 'WikiProject Companies'), ('parameter', 'needs-infobox')]))