Szerkesztő:BinBot/deletebot.py
Megjelenés
Ez a script a WP:T és a WP:C körüli karbantartásokat igyekszik ellátni: lezárt tmb-k törlése, be nem linkelt (al)lapok belinkelése stb. Részletek a vitalapon.
"""A törlés és a szerzői jogsértés körüli teendők automatizálása."""
import re
from datetime import date
import pywikibot as p
from pywikibot.date import monthName
site = p.Site()
datum = date.today()
day = int(datum.strftime('%d'))
month = int(datum.strftime('%m'))
today = f'{monthName("hu", month).title()} {day}.'
monthList = [monthName('hu', index) for index in range(1, 13)]
months = '|'.join(monthList)
ts = re.compile(r'(\d{4})\. (' + months + r') (\d\d?)\., \d\d:\d\d \(CES?T\)')
class DeleteTMBbot():
"""Removes closed talks from WP:T and puts a {{ta}} if neccessary."""
def __init__(self):
self.pageName = 'Wikipédia:Törlésre javasolt lapok'
self.page = p.Page(site, self.pageName)
self.text = self.page.get()
def run(self):
self.process(self.listPages())
def listPages(self):
regex = re.compile(r'(?i)\{\{törlés link\|(.*?)\}\}')
titleList = regex.findall(self.text)
pageList = [
p.Page(site, f'{self.pageName}/{title}') for title in titleList]
return pageList
def process(self, pageList):
comment1 = '{{ta}} sablon pótlása'
oldClosed = []
for page in pageList:
text = page.get()
if text.strip().startswith('{{tt}}'):
if not '{{ta}}' in text:
page.put(text + '\n{{ta}}', comment1, botflag=False)
oldEnough = self.oldEnough(text)
if oldEnough:
oldClosed.append(
page.title().replace(
'Wikipédia:Törlésre javasolt lapok/', ''))
comment2 = '2-3 napnál régebben lezárt megbeszélések törlése: '
comment2 += ', '.join(oldClosed)
newtext = self.text
for title in oldClosed:
title = '{{törlés link|' + title + '}}\n'
newtext = newtext.replace(title, '')
# Clear empty sections:
newtext = re.sub(r'\n==.+==(?:\n\s*)?(\n==.+==\n)', r'\1', newtext)
if newtext != self.text:
self.page.put(newtext, comment2, botflag=False, minor=False)
def oldEnough(self, text):
"""Determine if days elapsed from closing the talk >= 3."""
closeDate = ts.search(text)
if not closeDate:
return False
year = int(closeDate.group(1))
month = monthList.index(closeDate.group(2)) + 1
day = int(closeDate.group(3))
closedAt = date(year, month, day)
if str(datum - closedAt) > '3 days':
return True
return False
class InsertTMBbot():
"""Inserts deletion talk subpages onto WP:T."""
pass
class TMBhunterbot():
"""Searches for {{törlés}} templates without subpage."""
pass
class Copyviobot():
"""Inserts articles with copyvio template onto WP:C."""
def __init__(self):
self.cat = p.Category(site, 'Szerzői jog valószínű megsértése')
self.page = p.Page(site, 'Wikipédia:Szerzőijog-sértés')
def run(self):
articlesList = self.articles()
if articlesList:
bot = Listerbot(self.page, articlesList)
bot.run()
def articles(self):
return list(self.cat.articles())
class Listerbot():
"""Lists the articles to the current date on the appropriate page."""
def __init__(self, page, articles):
self.page = page # A page object where we list the articles.
self.articles = articles # A list of page objects.
self.linkedArticles = list(self.page.linkedPages())
self.missingList = []
self.text = ''
self.start = 0
self.comment = '<!-- E FÖLÉ NE ÍRJ, ALÁ VEZESD FEL A JELÖLTEKET DÁTUM SZERINT! -->\n'
self.commentLength = len(self.comment)
def run(self):
for article in self.articles:
if article not in self.linkedArticles:
self.missingList.append(article)
if self.missingList:
self.listForPutting = ''
for article in self.missingList:
self.listForPutting += \
f'* [[{article.title()}]] A jelölő helyett: ~~~~\n'
self.text = self.page.get()
self.listOnPage()
def listOnPage(self):
self.start = self.mainSectionComment()
if not self.start:
return # Avoid index errors.
title, lastDate = self.firstSection()
# We cannot write under the main title because of comments.
# So we write above the first outdated section, if neccessary.
if lastDate.lower() == today.lower():
# Write under the title.
newtext = self.text.replace(
title,
f'{title}{self.listForPutting}')
else:
# Write above the title.
newtext = self.text.replace(
title,
f'== {today} ==\n{self.listForPutting}\n{title}')
self.page.put(
newtext,
summary='Botteszt a be nem linkelt jogsértő oldalak listázására, új napra, több lappal.',
minor = False,
botflag = False)
def mainSectionComment(self):
"""There is a comment under the = Main section =. Find it."""
try:
return self.text.index(self.comment)
except ValueError:
# Structure of the page has changed?
return 0 # Can't be real index.
def firstSection(self):
"""
Find the first == Section == after mainSectionComment.
The actual date shuld be in the first section.
"""
index = self.start + self.commentLength
remainder = self.text[index:]
titleReg = re.compile(r'== ?(.+?) ?== ?\n')
title = titleReg.search(self.text).group()
lastDate = titleReg.search(self.text).group(1)
return title, lastDate
def main():
cbot = Copyviobot()
cbot.run()
dbot = DeleteTMBbot()
dbot.run()
if __name__ == '__main__':
main()