Source code for qiuwenbot.filter.expired_templates
# qiuwenbot, a bot to contribute to qiuwen.wiki# Copyright (C) 2022 Jinzhe Zeng## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <https://www.gnu.org/licenses/>.#fromdatetimeimportdatetime,timedeltafromqiuwenbot.filter.filterimportFilter,register_filterfromqiuwenbot.utilsimportdevide_parameters,get_template_regex
[docs]classRemoveExpiredTemplateFilter(Filter):"""Filter to remove a certain tag. Parameters ---------- tag : str Tag name to remove. """def__init__(self,template:str):self.template=templateself.template_regex=get_template_regex(template,end=r"\s*")
[docs]deffilter(self,text:str)->str:matched=self.template_regex.finditer(text)formminmatched:rm=Falseparams=mm.group("params")params_dict=devide_parameters(params)time=params_dict.get("time")iftimeisNone:rm=Trueelse:try:isotime=datetime.fromisoformat(time)exceptValueError:rm=Trueelse:# a month agoif(isotime.timestamp()<(datetime.now()-timedelta(days=31)).timestamp()):rm=Trueifrm:text=text.replace(mm.group(0),"")returntext
[docs]@register_filterclassRemoveExpiredCurrentFilter(RemoveExpiredTemplateFilter):"""Filter to remove {{current}}."""def__init__(self):super().__init__("current")
[docs]@register_filterclassRemoveExpiredDeadFilter(RemoveExpiredTemplateFilter):"""Filter to remove {{近期逝世}}."""def__init__(self):super().__init__("近期逝世")
[docs]@register_filterclassRemoveExpiredDeadFilter2(RemoveExpiredTemplateFilter):"""Filter to remove {{最近逝世}}."""def__init__(self):super().__init__("最近逝世")
[docs]@register_filterclassRemoveExpiredDeadFilter3(RemoveExpiredTemplateFilter):"""Filter to remove {{recent death}}."""def__init__(self):super().__init__("recent death")