Jump to content

User:GalliumBot/darn/darn.py

From Wikipedia, the free encyclopedia
"""
Copyright (c) 2022 theleekycauldron

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.
"""
import pywikibot as pwb
import re
version = " [[[User:GalliumBot#darn|d a r n]] v1.0.3]" 
site = pwb.Site('en', 'wikipedia')
time_page = pwb.Page(site,"User:GalliumBot/Time")
timestamps = time_page.get().splitlines()
class Nomination:
    def __init__(self,name):
        self.name = name #should be the nompage name
        self.log = [] # [(time, text, user, revid)]
        self.authors = [] #list of names, no "User:"
        
    def __repr__(self):
        return self.name + ": " + str(self.log)
        
    def loglink(self,ind):
        return self.log[ind][1]+" ['''[[Special:Diff/"+str(self.log[ind][3])+"|"+self.log[ind][0].replace("T"," at ").replace("Z","")+"]]''', by {{noping|"+self.log[ind][2]+"}}]\n"

def generate_pqs():
    res = [pwb.Page(site,"Template:Did you know")]
    for i in range(1,8):
        res.append(pwb.Page(site,"Template:Did you know/Preparation area "+str(i)))
        res.append(pwb.Page(site,"Template:Did you know/Queue/"+str(i)))
    return res

def main():
    nominations = {}
    pqs = generate_pqs() # preps and queues, as a list of Page objects
    for page in pqs: #Populating
        print(page)
        for revision in page.revisions(endtime=timestamps[0]):
            text = page.getOldVersion(revision.revid)
            try:
                hooktext = text[text.index("<!--Hooks-->"):text.index("<!--HooksEnd-->")].splitlines()[1:]
            except ValueError as e:
                continue
            for hook in hooktext:
                articles = [a[0].capitalize() + a[1:] for a in re.findall(r"'''\[\[([^\|\]]+)(?:\||\]\])",hook)]
                for article in articles:
                    subpages = re.findall(r"\* {{DYKmake\|"+article+r"\|.+\|subpage=([^}]+)}}",text)
                    if len(subpages) == 0:
                        continue
                    if subpages[0] not in nominations:
                        nominations[subpages[0]] = Nomination(subpages[0])
                    nominations[subpages[0]].log.append((str(revision.timestamp),hook,revision.user,revision.revid))
                    break
    
    latest_timestamp = str(timestamps[1])
    for nom in nominations:
        nominations[nom].log.sort(key=lambda x:x[0])
        if nominations[nom].log[-1][0] <= timestamps[1]:
            continue
        for i in range(len(nominations[nom].log)):
            if nominations[nom].log[i][0] > timestamps[1]:
                nominations[nom].log = nominations[nom].log[max(i-1,0):]
                break
        log = nominations[nom].log
        latest_timestamp = max(latest_timestamp,log[-1][0])
        if log[0][1] != log[-1][1]:
            output = "\n\n== Hook modifications ==\n"+nominations[nom].loglink(0)
            for i in range(1,len(log)):
                if log[i][1] != log[i-1][1]:
                    output += nominations[nom].loglink(i)
            nomtalk = pwb.Page(site,"Template talk:Did you know nominations/"+nom)
            nomtalk.text += output + "~~~~"
            nomtalk.save(summary="logging detected modifications"+version)
    time_page.text = timestamps[1]+"\n"+latest_timestamp
    time_page.save(summary="updating time"+version)
    
main()