Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autopurge: add some logging and lower the parallel purges #7

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 52 additions & 13 deletions facenapalmscripts/autopurge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,59 @@
from pathlib import Path
import pywikibot
import pywikibot.exceptions
import logging

# manually load the auth
curdir = Path(__file__).parent.parent.absolute()
userfile = curdir / "user-config.py"
exec(compile(userfile.read_text(), str(userfile), 'exec'), vars(pywikibot.config))
exec(compile(userfile.read_text(), str(userfile), "exec"), vars(pywikibot.config))

def process_purge(site, catname, limit=500):

def process_purge(site, catname, limit=50):
"""Purge all pages from category and return status for logging."""
members = list(pywikibot.Category(site, catname).members())
length = len(members);
logging.info(
"Starting purge: Processing %d members for category %s", len(members), catname
)
length = len(members)
for i in range(0, length, limit):
if not site.purgepages(members[i:i+limit]):
return "неожиданный ответ сервера"
try:
if not site.purgepages(members[i : i + limit]):
return "неожиданный ответ сервера"
except Exception as error:
logging.error(
"Failed trying to purge members %d to %d for category %s: %s",
i,
i + limit,
catname,
str(error),
)
raise

logging.info(" purged from %d to %d", i, i + limit)
logging.info("Purged %d members", len(members))
return str(length)


def process_hourly(site):
"""Purge all hourly-purged pages and return log part."""
return "срочных: " + process_purge(site, "К:Википедия:Страницы с ежечасно очищаемым кэшем")
logging.info("Starting hourly purge...")
return "срочных: " + process_purge(
site, "К:Википедия:Страницы с ежечасно очищаемым кэшем"
)


def process_daily(site):
"""Purge all daily-purged pages and return log part."""
return "ежедневных: " + process_purge(site, "К:Википедия:Страницы с ежедневно очищаемым кэшем")
logging.info("Starting daily purge...")
return "ежедневных: " + process_purge(
site, "К:Википедия:Страницы с ежедневно очищаемым кэшем"
)


def process_null(site):
"""Purge all daily-nulledited pages and return log part."""
logging.info("Starting null purge...")
catname = "К:Википедия:Страницы с ежедневно совершаемой нулевой правкой"
members = list(pywikibot.Category(site, catname).members())
errors = 0
Expand All @@ -51,22 +79,30 @@ def process_null(site):
errors += 1
return "нулевых правок: " + str(len(members) - errors)


def log(site, respond):
"""Edit template status page."""
page = pywikibot.Page(site, "Шаблон:Очищать кэш/статус")
page.text = "~~~~. Обработано " + "; ".join(respond) + "<noinclude>\n[[Категория:Шаблоны:Подстраницы шаблонов]]\n</noinclude>"
page.text = (
"~~~~. Обработано "
+ "; ".join(respond)
+ "<noinclude>\n[[Категория:Шаблоны:Подстраницы шаблонов]]\n</noinclude>"
)
page.save("Отчёт.")

KEYS = {
"--hourly": process_hourly,
"--daily": process_daily,
"--null": process_null
}

KEYS = {"--hourly": process_hourly, "--daily": process_daily, "--null": process_null}

DISABLE_LOG = "--nolog"


def main():
"""Get console arguments and call corresponding fucntions."""
logging.basicConfig(
# add timestamps to logs
format="%(asctime)s %(levelname)-8s %(message)s",
level=logging.INFO,
)
if len(sys.argv) == 1:
return
args = sys.argv[1:]
Expand All @@ -79,5 +115,8 @@ def main():
if DISABLE_LOG not in args:
log(site, respond)

logging.info("Finished")


if __name__ == "__main__":
main()