import pywikibot
import re
import os
import pymysql
site = pywikibot.Site('zh-classical', '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 zh_classicalwiki_p')
    cur.execute("""
        SELECT page_title, fp_quality
        FROM page
        LEFT JOIN flaggedpages ON page_id = fp_page_id
        WHERE page_namespace = 0
            AND page_is_redirect = 0
            AND fp_reviewed = 1
        ORDER BY page_title ASC
    """)
    res = cur.fetchall()
text = """{| class="wikitable sortable"
!頁面!!品質"""
for row in res:
    title = row[0].decode()
    if row[1] == 0:
        quality = '初定本'
    elif row[1] == 1:
        quality = '校正本'
    else:
        quality = '未知'
    text += "\n|-\n"
    text += "|[["+title+"]]"
    text += "||"+quality
text += "\n|}"
page = pywikibot.Page(site, "用戶:Xiplus/已被審核的頁面")
page.text = text
page.save(summary="更新", minor=False)
Page [[zh-classical:用戶:Xiplus/已被審核的頁面]] saved
INFO:pywiki:Page [[zh-classical:用戶:Xiplus/已被審核的頁面]] saved