Loading AI tools
来自维基百科,自由的百科全书
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pywikibot
import json
import collections
site = pywikibot.Site()
#--------------------------------
jsonpage = pywikibot.Page(site, u"User:Sz-iwbot/rater/aliases.json")
jsontext = jsonpage.text
aliasesdict = json.loads(jsontext)
print('Loading data....')
#检查现有数据,删除不存在的重定向,修复错误的重定向
print('Check current data....')
for item in list(aliasesdict.keys()):
tempalte = "Template:"+item
tpage = pywikibot.Page(site, tempalte)
if tpage.exists():
if tpage.isRedirectPage():
#print(item+ ' is RedirectPage')
tname = tpage.getRedirectTarget().title(with_ns=False)
aliasesdict[tpage.title(with_ns=False)] = tname
#print (tpage.title(with_ns=False), tname)
else:
del aliasesdict[item]
print (item + ' not RedirectPage, DELETE!')
else:
del aliasesdict[item]
print (item + ' not exists, DELETE!')
print('Current data checked!')
#aliases_order = collections.OrderedDict(sorted(aliasesdict.items(),key=lambda t:t[1]))
#jsonpage.text = json.dumps(aliases_order)
#jsonpage.save('WP aliases update')
#---------------------------
tlist = []
categorys = ['Category:含质量评级的专题横幅','Category:不含质量评级的专题横幅','Category:使用评级分类的WPBannerMeta横幅','Category:专题标志模板','Category:自定义等级的WPBannerMeta模板']
#获取所有专题模板
print('Get all banner template...')
for cat in categorys:
catpage = pywikibot.Category(site, cat)
for t in catpage.articles(namespaces=10):
templatename = t.title()
if '/' not in templatename:
tlist.append(templatename)
#print(templatename)
#去重
clean_tlist = list(set(tlist))
#找到专题模板的重定向页面
print('Get Redirect Page...')
for banner in clean_tlist:
banner_page = pywikibot.Page(site, banner)
if banner_page.exists():
if banner_page.isRedirectPage():
banner_page = banner_page.getRedirectTarget()
redirects = banner_page.backlinks(filter_redirects=True)
for r in redirects:
#修复pywikibot API获取错误重定向页问题
if r.getRedirectTarget()== banner_page:
aliasesdict[r.title(with_ns=False)] = banner_page.title(with_ns=False)
print(r.title(with_ns=False)+ ' redirect to '+ banner_page.title(with_ns=False))
#排序
aliases_order = collections.OrderedDict(sorted(aliasesdict.items(),key=lambda t:t[1]))
jsonpage.text = json.dumps(aliases_order)
jsonpage.save('WP aliases update')
#print (clean_tlist)
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.