#!/usr/bin/python
# -*- coding: utf-8 -*-
import pywikibot
import json
import collections
site = pywikibot.Site()
#--------------------------------
jsonpage = pywikibot.Page(site, u"User:Sz-iwbot/rater/projects.json")
jsontext = jsonpage.text
projectsdict = json.loads(jsontext)
print('Loading data....')
#检查现有数据,删除不存在的页面,修复错误的重定向
print('Check current data....')
for item in list(projectsdict.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)
projectsdict[tname] = projectsdict[item]
del projectsdict[item]
print (item + ' is RedirectPage, FIX!')
else:
del projectsdict[item]
print (item + ' not exists, DELETE!')
print('Current data checked!')
#---------------------------
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))
#将表中没有的模板列入
for banner in clean_tlist:
banner_page = pywikibot.Page(site, banner)
if banner_page.exists():
if banner_page.isRedirectPage():
banner_page = banner_page.getRedirectTarget()
if banner_page.title(with_ns=False) not in projectsdict:
projectsdict[banner_page.title(with_ns=False)] = []
print('Add ' + banner)
#排序
projects_order = collections.OrderedDict(sorted(projectsdict.items(),key=lambda t:t[0]))
jsonpage.text = json.dumps(projects_order)
jsonpage.save('WP projects update')
#print (clean_tlist)
Wikiwand in your browser!
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.