# 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/>.#fromabcimportABCMeta,abstractmethodfrompywikibotimportPage,Timestampfrompywikibot.pagegeneratorsimportPreloadingGeneratorfromtqdmimporttqdmfromtqdm.contrib.loggingimportlogging_redirect_tqdmfromqiuwenbot.botimportget_page,loginfromqiuwenbot.qwloggerimportqwloggerfromqiuwenbot.utilsimportarchieve_page
[docs]classTask(metaclass=ABCMeta):"""A task to be done. Parameters ---------- user : str Username. password : str Password. pages : dict Pages to operate. logging_page : str, optional Page to log the task, by default None summary : str, optional Summary of the task, by default emptry string """def__init__(self,user:str,password:str,pages:dict,logging_page:str=None,summary:str="",):"""Initialize."""self.site=login(user,password)iflogging_pageisnotNone:self.logging_page=get_page(logging_page,self.site)else:self.logging_page=Noneifpages["type"]=="all":ifpages.get("restart",False):last_item=self.logging_page.text.strip().split("\n")[-1]title=last_item.split("-")[0].strip()[4:-2]qwlogger.info("restart from %s"%title)else:title=""self.pages=self.site.allpages(namespace=pages.get("namespace",0),start=title)elifpages["type"]=="new":start=pages.get("start",None)ifstartisnotNone:start=Timestamp.fromISOformat(start)end=pages.get("end",None)ifendisnotNone:end=Timestamp.fromISOformat(end)self.pages=(change[0]forchangeinself.site.newpages(returndict=True,namespaces=pages.get("namespace",0),start=start,end=end,))elifpages["type"]in("link","template"):template=get_page(pages["name"],self.site)self.pages=template.getReferences(namespaces=pages.get("namespace",None))elifpages["type"]=="page":self.pages=[get_page(pages["name"],self.site)]else:raiseRuntimeError("Unsupported pages type")self.summary=summary
[docs]@abstractmethoddefdo(self,page:Page)->bool:"""Do the task."""raiseNotImplementedError
[docs]deflogging(self,title:str)->None:"""Log the removing operator. Parameters ---------- title : str title of the modified page """ifself.logging_pageisnotNone:iflen(self.logging_page.text.split("\n"))>2000:self.logging_page=archieve_page(self.logging_page,self.site)self.logging_page.text+="\n# [[%s]] - ~~~~~"%titleself.logging_page.save(self.summary,asynchronous=True)
[docs]defsubmit(self):"""Submit the task."""withlogging_redirect_tqdm():n_modified=tqdm(position=1,desc="Modified pages")forpageintqdm(PreloadingGenerator(self.pages),desc="Scanned pages"):ifself.do(page):n_modified.update(1)self.logging(page.title())