Skip to content

Instantly share code, notes, and snippets.

@libkoi
Created June 9, 2022 13:26
Show Gist options
  • Save libkoi/684227f1d4975048d51a97d385e14e7b to your computer and use it in GitHub Desktop.
Save libkoi/684227f1d4975048d51a97d385e14e7b to your computer and use it in GitHub Desktop.
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