-
-
Save libkoi/684227f1d4975048d51a97d385e14e7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pywikibot | |
from tqdm import tqdm | |
import re | |
from datetime import datetime | |
import requests | |
import json | |
site = pywikibot.Site('zh', 'wikipedia', user='Crystal-bot') | |
site.login() | |
s_curtime = datetime.utcnow() | |
s_curtime_ts = s_curtime.strftime('%Y-%m-%d %H:%M:%S') | |
print("==========") | |
print(f"[INFO] {s_curtime_ts} Searching articles with \"dead_end\" template.") | |
template = pywikibot.Page(pywikibot.Link('Lead_missing', default_namespace=10, source=site)) | |
gen = template.getReferences(only_template_inclusion=True, namespaces=0) | |
count = 0 | |
matched = 0 | |
for page in (gen): | |
r = requests.get('https://zh.wikipedia.org/api/rest_v1/page/summary/' + page.title()) | |
try: | |
count += 1 | |
data = json.loads(r.text) | |
if data['extract'] != "": | |
matched += 1 | |
page.text = re.sub(r"\{\{(([Ll]ead|Intro)[- ]?missing|缺少序言)\|.*?\}\}\n?", r"", page.text) | |
#page.save("机器人: 移除序言不为空条目中的[[Template:Lead missing]]模板") | |
except: | |
pass | |
e_curtime = datetime.utcnow() | |
e_curtime_ts = e_curtime.strftime('%Y-%m-%d %H:%M:%S') | |
elapse = e_curtime - s_curtime | |
print(f"[INFO] {e_curtime_ts} Found {str(matched)} articles in {str(count)} articles, replaced \"dead_end\" template in {elapse.total_seconds():.2f} seconds, sleeping...") | |
print("==========\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment