Jump to content

User:DreamRimmer/PERM.py

From Wikipedia, the free encyclopedia
"""
Copyright (c) 2024 DreamRimmer

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, re
from datetime import datetime, timedelta

def get_page(site, title):
    req = site.simple_request(action="query", prop="info", titles=title, inprop="url")
    data = req.submit()
    pages = data.get("query", {}).get("pages", {})
    for page_id, page_data in pages.items():
        if page_id == "-1":
            print(f"'{title}' does not exist.")
            return None, None
        content = pywikibot.Page(site, title).text
        return content, page_data.get("lastrevid")

def split_sections(content):
    sections = re.split(r"(?m)^={4}(.*?)={4}$", content)
    results = []
    for i in range(1, len(sections), 2):
        header = sections[i].strip()
        section_body = sections[i + 1]
        header_text = re.sub(r"\[\[|\]\]", "", header).strip()
        anchor = header_text.replace(" ", "_").replace("#", "%23")
        done = any(template in section_body for template in ["{{Already done}}", "{{Did}}", "{{Done-t}}", "{{DONE}}", "{{AlreadyDone}}", "{{Alreadydone}}", "{{Already Done}}", "{{done}}", "{{DONE}}"])
        not_done = any(template in section_body for template in ["{{nd}}", "{{Nd}}", "{{Notdone}}", "{{NotDone}}", "{{Request withdrawn}}", "{{Not done for now}}", "{{Ndfn}}", "{{not done}}", "{{withdrawn}}", "{{Not done}}", "{{Withdrawn}}"])
        user_match = re.search(r"\*{{rfplinks\|1=([^}]+)}}", section_body)
        user = user_match.group(1) if user_match else None
        timestamps = [datetime.strptime(m.group(1), "%H:%M, %d %B %Y") for m in re.finditer(r"(\d{1,2}:\d{2}, \d{1,2} \w+ \d{4})", section_body)]
        nearest_timestamp = min(timestamps, key=lambda t: abs(datetime.utcnow() - t)) if timestamps else None
        results.append({
            "header": header_text,
            "clean_header": anchor,
            "user": user,
            "done": done,
            "not_done": not_done,
            "nearest_timestamp": nearest_timestamp,
        })
    return len(sections) // 2, results

def create_lists(results, permalink, title):
    approved_list = {}
    denied_list = {}
    cutoff_date = datetime.utcnow() - timedelta(days=3)
    for result in results:
        if not result["user"] or not result["nearest_timestamp"]:
            continue
        if result["nearest_timestamp"] > cutoff_date:
            continue
        date_str = result["nearest_timestamp"].strftime("%-d %B")
        entry = f"*{{{{Usercheck-short|{result['user']}}}}} [[{title}]] <sup>[https://wiki.riteme.site/wiki/Special:Permalink/{permalink}#{result['clean_header']} link]</sup>"
        if result["done"]:
            approved_list.setdefault(date_str, []).append(entry)
        elif result["not_done"]:
            denied_list.setdefault(date_str, []).append(entry)
    return approved_list, denied_list

def sort_by_date(date_dict):
    return sorted(date_dict.keys(), key=lambda x: datetime.strptime(x, "%d %B"))

def post(site, page_title, approved_list, denied_list):
    output = "== Approved ==\n"
    for date in sort_by_date(approved_list):
        formatted_date = datetime.strptime(date, "%d %B").strftime("%B %-d")
        output += f"=== {formatted_date} ===\n"
        output += "\n".join(approved_list[date]) + "\n\n"

    output += "== Denied ==\n"
    for date in sort_by_date(denied_list):
        formatted_date = datetime.strptime(date, "%d %B").strftime("%B %-d")
        output += f"=== {formatted_date} ===\n"
        output += "\n".join(denied_list[date]) + "\n\n"

    p_page = pywikibot.Page(site, page_title)
    p_page.text = output
    p_page.save("Processed [[Wikipedia:Requests for permissions|PERM]] requests")

if __name__ == "__main__":
    titles = [
        "Wikipedia:Requests for permissions/Account creator",
        "Wikipedia:Requests for permissions/Autopatrolled",
        "Wikipedia:Requests for permissions/AutoWikiBrowser",
        "Wikipedia:Requests for permissions/Confirmed",
        "Wikipedia:Requests for permissions/Event coordinator",
        "Wikipedia:Requests for permissions/Extended confirmed",
        "Wikipedia:Requests for permissions/File mover",
        "Wikipedia:Requests for permissions/Mass message sender",
        "Wikipedia:Requests for permissions/New page reviewer",
        "Wikipedia:Requests for permissions/Page mover",
        "Wikipedia:Requests for permissions/Pending changes reviewer",
        "Wikipedia:Requests for permissions/Rollback",
        "Wikipedia:Requests for permissions/Template editor",
    ]

    site = pywikibot.Site("en", "wikipedia")
    overall_approved_list = {}
    overall_denied_list = {}

    for title in titles:
        print(f"Processing {title}")
        content, permalink = get_page(site, title)
        if not content or not permalink:
            continue
        count, parsed = split_sections(content)
        print(f"Found {count // 2} requests on {title}.")
        approved_list, denied_list = create_lists(parsed, permalink, title)

        for date, entries in approved_list.items():
            overall_approved_list.setdefault(date, []).extend(entries)
        for date, entries in denied_list.items():
            overall_denied_list.setdefault(date, []).extend(entries)

    print("\nSaving...")
    post(site, "User:DreamRimmer/sandbox", overall_approved_list, overall_denied_list)