User:GalliumBot/transfem/transfem.py

'''

Copyright (c) 2025 theleekycauldron,

except with respect to code that has been copied from the source code of WugBot,

which is the copyright of Wugapodes (2017)

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.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

THE SOFTWARE.

'''

import pywikibot as pwb

import datetime

import re

site = pwb.Site('en','wikipedia')

now = datetime.datetime.now(datetime.timezone.utc)

current_year = now.year

tag = " transfem v0.1.0]"

class Nomination:

def __init__(self,page,date,source):

self.date = date

self.page = pwb.Page(site,page) if isinstance(page,str) else page

self.is_open = self.open()

self.source = source

if self.is_open:

self.is_approved = self.approved()

print(self.page.title(),["is not approved","is approved"][self.is_approved])

def open(self):

return "{{DYKsubpage" in self.page.text

def computeDYKChecklistStatus(self,template):

if type(template) is list:

template='\n'.join(template)

elif type(template) is not str:

raise ValueError('template must be either list or string')

dykcr = re.compile(r'status\s*=\s*(?:y|Y)')

match = re.search(dykcr,template)

if match != None:

return(1)

else:

return(0)

def approved(self,status=0):

global nclosed

global aclosed

global pageCnt

page = self.page

dykchecklist = []

dykc = 0

good_ticks = ["File:Symbol confirmed.svg"]

bad_ticks = ["File:Symbol voting keep.svg",

"File:Symbol question.svg",

"File:Symbol possible vote.svg",

"File:Symbol delete vote.svg",

"File:Symbol redirect vote 4.svg"]

for line in page.text.split('\n'):

if dykc == 1:

dykchecklist.append(line)

if '}}' in line and '{{tq|' not in line:

dykc=0

status = self.computeDYKChecklistStatus('\n'.join(dykchecklist))

elif '{{DYK checklist' in line:

if '}}' in line:

status = self.computeDYKChecklistStatus(line)

else:

dykchecklist.append(line)

dykc = 1

line = line.replace('_',' ')

if any(f"16px" in line for tick in good_ticks):

status = 1

elif any(f"16px" in line for tick in bad_ticks):

status = 0

return status

def __repr__(self):

return f"Nomination({self.page},{self.date},{self.source})"

new = [0,0]

retranscluded = [0,0]

closed = [0,0]

DYKN = pwb.Page(site,"User:GalliumBot/transfem/DYKN")

DYKNA = pwb.Page(site,"User:GalliumBot/transfem/DYKNA")

DYKN_text = DYKN.text.split("\n")

DYKNA_text = DYKNA.text.split("\n")

DYKN_header = DYKN_text[:DYKN_text.index("==Older nominations==")+1]

DYKN_footer = [""]+DYKN_text[DYKN_text.index("==Special occasion holding area=="):]

DYKNA_header = DYKNA_text[:DYKNA_text.index("==Approved nominations==")+1]

DYKNA_footer = []

nominations = []

def process_dyknx(DYKNX_text,source):

date = None

unapproved_noms = []

approved_noms = []

for line in DYKNX_text:

if "===Articles created/expanded on " in line:

datestr = f"{line[32:-3]}, {current_year}"

date = datetime.datetime.strptime(datestr,"%B %d, %Y").replace(tzinfo=datetime.timezone.utc)

if date>now:

datestr = f"{line[32:-3]}, {current_year-1}"

date = datetime.datetime.strptime(datestr,"%B %d, %Y").replace(tzinfo=datetime.timezone.utc)

if "{{Template:Did you know nominations/" in line or ("{{Did you know nominations/" in line and "{{Did you know nominations/YOUR ARTICLE TITLE" not in line):

if date is None:

continue

line = line.replace("{{Did you know nominations","{{Template:Did you know nominations")

try:

nom = Nomination(line[2:-2],date,source)

if nom.is_open:

(approved_noms if nom.is_approved else unapproved_noms).append(nom)

if nom.source != nom.is_approved:

new[nom.is_approved] += 1

print(f" moved to {'DYKNA' if nom.is_approved else 'DYKN'}!")

else:

closed[nom.source] += 1

print(f" closed!")

except Exception as e:

print(line,e)

return approved_noms,unapproved_noms

approved_noms_DYKN,unapproved_noms_DYKN = process_dyknx(DYKN_text,0)

approved_noms_DYKNA,unapproved_noms_DYKNA = process_dyknx(DYKNA_text,1)

approved_noms = approved_noms_DYKN+approved_noms_DYKNA

unapproved_noms = unapproved_noms_DYKN+unapproved_noms_DYKNA

nom_titles = [nom.page.title() for nom in approved_noms+unapproved_noms]

pending_category = pwb.Category(site,"Category:Pending DYK nominations")

total_text = DYKN.text+DYKNA.text+pwb.Page(site,"Wikipedia:April Fools' Main Page/Did you know").text

for nompage in pending_category.articles():

if "Template talk:" in nompage.title():

continue

if nompage.title()[9:] in total_text:

continue

date = nompage.oldest_revision.timestamp

date = datetime.datetime(date.year,date.month,date.day,0,0,0,tzinfo=datetime.timezone.utc)

nomination = Nomination(nompage,date,-1)

if nomination.is_open:

(approved_noms if nomination.is_approved else unapproved_noms).append(nomination)

else:

print(nomination,"is closed??")

continue

retranscluded[nomination.is_approved] += 1

print(f" retranslcuded to {'DYKNA' if nomination.is_approved else 'DYKN'}!")

approved_noms.sort(key=lambda nom:nom.date)

unapproved_noms.sort(key=lambda nom:nom.date)

def reconstruct_page(header,footer,noms,approved):

res = header

last_date = None

for nom in noms:

if nom.date != last_date:

if not approved:

if last_date is not None:

res.append("")

if last_date

res.append("==Current nominations==")

date_str = nom.date.strftime('%B')+" "+str(int(nom.date.strftime("%d")))

res.append(f"===Articles created/expanded on {date_str}===")

if not approved:

res.append("")

last_date = nom.date

res.append("{{"+nom.page.title()+"}}")

res += footer

return "\n".join(res)

DYKN.text = reconstruct_page(DYKN_header,DYKN_footer,unapproved_noms,False)

DYKNA.text = reconstruct_page(DYKNA_header,DYKNA_footer,approved_noms,True)

summaries = ],[

for i in range(2):

summaries[i].append(f"moving {new[i]} nomination{'s' if new[i]>1 else ''} from Template talk:Did you know{'/Approved") if new[i]>0 else None

summaries[i].append(f"retranscluding {retranscluded[i]} nomination{'s' if retranscluded[i]>1 else ''}") if retranscluded[i]>0 else None

summaries[i].append(f"removing {closed[i]} closed nomination{'s' if closed[i]>1 else ''}") if closed[i]>0 else None

summaries[i] = ", ".join(summaries[i]) if len(summaries[i])>0 else "general cleanup"

DYKN.save(summary=summaries[0]+tag,minor=False)

DYKNA.save(summary=summaries[1]+tag,minor=False)