47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
import os
|
|
import mwclient
|
|
import click
|
|
import sys
|
|
from tqdm import tqdm
|
|
|
|
from config import *
|
|
|
|
# Template strings
|
|
HIDDEN_COMMENT = "<!-- This page is auto-generated. Do not edit manually. -->"
|
|
CATEGORY_TEMPLATE = "\n".join(
|
|
"[[Category:{0}]]".format(cat) for cat in WIKI_CATEGORY_LIST
|
|
)
|
|
|
|
# Connect to MediaWiki
|
|
site = mwclient.Site(WIKI_HOST, path=WIKI_PATH)
|
|
site.login(WIKI_USERNAME, WIKI_PASSWORD)
|
|
|
|
|
|
@click.command()
|
|
@click.argument('files', nargs=-1)
|
|
def post(files):
|
|
if files[0] == "all":
|
|
files = []
|
|
for filename in tqdm(os.listdir(OUTDIR_MEDIAWIKI)):
|
|
if filename.endswith(".mediawiki"):
|
|
filepath = os.path.join(OUTDIR_MEDIAWIKI, filename)
|
|
files.append(filepath)
|
|
|
|
for filepath in files:
|
|
filename = os.path.basename(filepath)
|
|
|
|
article_name = WIKI_ARTICLE_PREFIX + filename.replace(".mediawiki", "")
|
|
|
|
with open(filepath, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
# Compose final content
|
|
final_content = f"{HIDDEN_COMMENT}\n\n{content}\n\n{CATEGORY_TEMPLATE}"
|
|
|
|
# Post to MediaWiki
|
|
page = site.pages[article_name]
|
|
page.save(final_content, summary="U+1F916 ROBOT FACE")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
post()
|