import pywikibot
import re
import os
import pymysql
site = pywikibot.Site('zh', 'wikipedia')
site.login()
host = os.environ['MYSQL_HOST']
user = os.environ['MYSQL_USERNAME']
password = os.environ['MYSQL_PASSWORD']
conn = pymysql.connect(
    host=host,
    user=user,
    password=password,
    charset="utf8"
)
with conn.cursor() as cur:
    cur.execute('use zhwiki_p')
    cur.execute("""
        SELECT rc_namespace, page_is_redirect, COUNT(*) AS `count`
        FROM recentchanges
        LEFT JOIN page
        ON rc_namespace = page_namespace AND rc_title = page_title
        WHERE rc_new = 1
          AND rc_patrolled = 0
          AND page_is_redirect IS NOT NULL
        GROUP BY rc_namespace, page_is_redirect
    """)
    res = cur.fetchall()
cnttable = {}
for row in res:
    ns = row[0]
    redirect = row[1]
    cnt = row[2]
    if ns not in cnttable:
        cnttable[ns] = {0: 0, 1:0}
    cnttable[ns][redirect] = cnt
print(cnttable)
{0: {0: 516, 1: 1558}, 1: {0: 16, 1: 0}, 2: {0: 104, 1: 46}, 3: {0: 36, 1: 34}, 4: {0: 3, 1: 29}, 6: {0: 1, 1: 0}, 8: {0: 3, 1: 0}, 10: {0: 689, 1: 47}, 11: {0: 2, 1: 0}, 12: {0: 19, 1: 26}, 14: {0: 660, 1: 0}, 15: {0: 1, 1: 0}, 100: {0: 1, 1: 0}, 101: {0: 2, 1: 0}, 118: {0: 121, 1: 0}, 119: {0: 5, 1: 2}, 828: {0: 9, 1: 0}}
text = " "


page = pywikibot.Page(site, "Draft:沙盒")
page.text = text
page.save(summary="[[:en:Category:Yunnan]]及其子分类中未在中文维基百科创建的条目", minor=False)
{0: {0: 187, 1: 1595}, 1: {0: 1167, 1: 4}, 2: {0: 532, 1: 12}, 3: {0: 211, 1: 4}, 4: {0: 35, 1: 8}, 5: {0: 21, 1: 0}, 7: {0: 47, 1: 0}, 9: {0: 1, 1: 0}, 10: {0: 3445, 1: 49}, 11: {0: 45, 1: 0}, 14: {0: 685, 1: 2}, 15: {0: 422, 1: 0}, 100: {0: 8, 1: 0}, 118: {0: 111, 1: 0}, 119: {0: 15, 1: 0}, 828: {0: 72, 1: 0}, 829: {0: 2, 1: 0}}
Page [[zh:Draft:沙盒]] saved