MediaWiki

MediaWiki:BatchUpload.js

From Illustrations in German Translations of Mark Twain's Works

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
import csv
import os
import requests

# === KONFIGURATION ===
WIKI_API_URL = "https://illus.twainframe.org/api.php"
WIKI_USERNAME = ""
WIKI_PASSWORD = ""
CSV_PATH = r"D:\DRK und UNI\UNI\Englisch\VM lit\ILLIT\Job\Tutorial\Catalog.csv"
BILDER_VERZEICHNIS = r"D:\DRK und UNI\UNI\Englisch\VM lit\ILLIT\Job\Tutorial\01BilderUpload"

# === LOGIN ===
session = requests.Session()

# Login-Token holen
res = session.get(WIKI_API_URL, params={
    "action": "query",
    "meta": "tokens",
    "type": "login",
    "format": "json"
})
login_token = res.json()["query"]["tokens"]["logintoken"]

# Einloggen
res = session.post(WIKI_API_URL, data={
    "action": "login",
    "lgname": WIKI_USERNAME,
    "lgpassword": WIKI_PASSWORD,
    "lgtoken": login_token,
    "format": "json"
})
login_result = res.json()
print("Login-Ergebnis:", login_result)

# CSRF-Token holen
res = session.get(WIKI_API_URL, params={
    "action": "query",
    "meta": "tokens",
    "format": "json"
})
token = res.json()["query"]["tokens"]["csrftoken"]

# === BILDER HOCHLADEN ===
with open(CSV_PATH, newline='', encoding='utf-8-sig') as csvfile:
    reader = csv.DictReader(csvfile)
    # Feldnamen strippen, um Leerzeichen am Ende zu entfernen
    reader.fieldnames = [f.strip() for f in reader.fieldnames]

    for row in reader:
        filename = row['file name']
        title = row['Illustration Title']
        publication = row['Book']
        year = row['Year']
        illustrator = row['Illustrator']
        chapter = row['Chpt in this Ed.']
        illustration = row['Ill. in Chpt.']
        tags_raw = row['Tags'].strip().split()

        # Kategorien erstellen
        categories_list = [f"[[Category:tag:{tag[1:]}]]" for tag in tags_raw if tag.startswith("-")]
        categories_text = "\n".join(categories_list)

        # Tags für Template, ohne Bindestrich, kommagetrennt
        template_tags = ", ".join([tag[1:] for tag in tags_raw if tag.startswith("-")])

        file_path = os.path.join(BILDER_VERZEICHNIS, filename)
        if not os.path.isfile(file_path):
            print(f"Datei nicht gefunden: {file_path}")
            continue

        print(f"Lade hoch: {filename}")

        # Wikitext für die Bildseite
        wikitext = f"""{title}

{categories_text}

{{{{MediaInfo
 | title       = {title}
 | chapter     = {chapter}
 | illustration= {illustration}
 | illustrator = {illustrator}
 | year        = {year}
 | tags        = {template_tags}
 | publication = {publication}
}}}}
"""

        # Bild hochladen
        with open(file_path, 'rb') as f:
            files = {'file': (filename, f)}
            res = session.post(WIKI_API_URL, files=files, data={
                "action": "upload",
                "filename": filename,
                "comment": f"Batch-Upload: {title}",
                "text": wikitext,
                "token": token,
                "format": "json",
                "ignorewarnings": 1
            })
            print(res.json())