Szerkesztő:Atobot/articlecandidates.py


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

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Update the article candidates list."""

import re
from datetime import datetime
#from typing import List, Dict
import pywikibot
from pywikibot import date, pagegenerators

class ArticleCandidateBot:
    templatePage = u"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", u"|lista1=%s\n" % self.getArticleListForTemplatePage(freshArticles, existingTimestamps), cjPage.text)
        newPageText = re.sub(r"\|lista2=.*?\n", u"|lista2=%s\n" % self.getArticleListForTemplatePage(oneMonthArticles, existingTimestamps), newPageText)
        newPageText = re.sub(r"\|lista3=.*?\n", u"|lista3=%s\n" % self.getArticleListForTemplatePage(twoWeekArticles, existingTimestamps), newPageText)
        newPageText = re.sub(r"\|lista4=.*?\n", u"|lista4=%s\n" % self.getArticleListForTemplatePage(timeoutArticles, existingTimestamps), newPageText)

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

    def getArticleListForTemplatePage(self, articleList, existingTimestamps):
        ret = u""

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

    def getArticleCandidates(self):
        category = pywikibot.Category(self.site, u"Kategória:Feljavításra váró cikkjelöltek")
        for article in category.members():
            articleTitle = article.title()
            if articleTitle.startswith(u"Wikipédia:Feljavításra váró cikkjelöltek/"):
                yield articleTitle[41:]

    def getExistingArticleCandidateDatestamps(self, cjPageText):
        ret = {}

        pattern = re.compile(u"\\{\\{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()