Szerkesztő:Atobot/articlecandidates.py

A Wikipédiából, a szabad enciklopédiából

Forrás: https://github.com/qcz/huwiki-bots/blob/master/scripts/articlecandidates.py

#!/usr/bin/python
"""Update the article candidates (drafts) list."""

import re
from datetime import datetime
from typing import Iterator, List, Dict
import pywikibot
from pywikibot import date
from pywikibot.pagegenerators import CategorizedPageGenerator

class ArticleCandidateBot:
    templatePage = "Sablon:Cikkjelöltek"

    def run(self):
        self.site = pywikibot.Site()

        cjPage = pywikibot.Page(self.site, self.templatePage)

        existingTimestamps = self.getExistingArticleCandidateDatestamps(cjPage.text)

        today = datetime.today().replace(hour=0, minute=0, second=0)
        threemonths = today
        if threemonths.month > 3:
            threemonths = threemonths.replace(month=threemonths.month-3)
        else:
            threemonths = threemonths.replace(year=threemonths.year-1,
                    month=threemonths.month+9)

        freshArticles = []
        twoWeekArticles = []
        oneMonthArticles = []
        timeoutArticles = []

        for candidate in self.getArticleCandidates():
            timestamp = today
            if candidate in existingTimestamps:
                timestamp = existingTimestamps[candidate]
            else:
                existingTimestamps[candidate] = timestamp

            rem = (timestamp - threemonths).days
            if rem > 31:
                freshArticles.append(candidate)
            elif rem > 14:
                oneMonthArticles.append(candidate)
            elif rem > 0:
                twoWeekArticles.append(candidate)
            else:
                timeoutArticles.append(candidate)

        newPageText = re.sub(r"\|lista1=.*?\n", "|lista1=%s\n" % self.getArticleListForTemplatePage(freshArticles, existingTimestamps), cjPage.text)
        newPageText = re.sub(r"\|lista2=.*?\n", "|lista2=%s\n" % self.getArticleListForTemplatePage(oneMonthArticles, existingTimestamps), newPageText)
        newPageText = re.sub(r"\|lista3=.*?\n", "|lista3=%s\n" % self.getArticleListForTemplatePage(twoWeekArticles, existingTimestamps), newPageText)
        newPageText = re.sub(r"\|lista4=.*?\n", "|lista4=%s\n" % self.getArticleListForTemplatePage(timeoutArticles, existingTimestamps), newPageText)

        cjPage.text = newPageText
        cjPage.save(summary="Bot: Cikkjelöltek listájának frissítése", minor=False)

    def getArticleListForTemplatePage(
                                self, 
                                articleList: List[str], 
                                existingTimestamps: Dict[str, datetime.date]
                                ) -> str:
        ret = "''nincs ilyen cikkjelölt''"
        if len(articleList) > 0:
            ret = ", ".join(map(lambda x: "{{Cikkjelölt-hivatkozás|%s|%s}}" % (x, datetime.strftime(existingTimestamps[x], "%Y-%m-%d")), articleList))
        return ret

    def getArticleCandidates(self) -> Iterator[str]:
        # Double check for category and namespace, redundant if there is no mess
        category = pywikibot.Category(self.site, "Feljavításra váró cikkjelöltek")
        for article in CategorizedPageGenerator(category, namespaces=118):
            yield article.title(with_ns=False)

    def getExistingArticleCandidateDatestamps(self, cjPageText: str) -> Dict[str, datetime.date]:
        ret = {}

        pattern = re.compile("\\{\\{Cikkjelölt-hivatkozás\\|(.*?)\\|(\\d{4}-\\d{2}-\\d{2})\\}\\}")
        matches = pattern.findall(cjPageText)
        for match in matches:
            try:
                dt = datetime.strptime(match[1], "%Y-%m-%d")
                ret[match[0]] = dt
            except ValueError:
                pass

        return ret

def main(*args):
    pywikibot.handle_args(args)
    ArticleCandidateBot().run()

if __name__ == "__main__":
    main()