User:SnowyCinema/fix gnome gnat.py

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
import pywikibot
import re

# Connect to Wikimedia Commons
site = pywikibot.Site('commons', 'commons')

# Category containing the files
category = pywikibot.Category(site, 'Category:Gnorm Gnat comic strips')

# Regex pattern to find dates in the format YYYY-MM-DD
date_pattern = re.compile(r'\d{4}-\d{2}-\d{2}')

# Iterate through each file in the category
for file_page in category.articles(namespaces='File'):
    # Extract the correct date from the file name
    # File names are in the format "File:Gnorm Gnat - YYYY-MM-DD.png"
    correct_date = file_page.title(with_ns=False).split(' - ')[1].split('.')[0]

    # Load the current text of the file page
    current_text = file_page.text

    # Find all dates in the text that match the format YYYY-MM-DD
    incorrect_dates = date_pattern.findall(current_text)

    # Replace each incorrect date with the correct date
    for incorrect_date in incorrect_dates:
        current_text = current_text.replace(incorrect_date, correct_date)

    # Save the edited text back to the file page
    file_page.text = current_text
    file_page.save(summary='Correcting dates in file description')

print("Script completed.")
Public domain I, the copyright holder of this work, release this work into the public domain. This applies worldwide.
In some countries this may not be legally possible; if so:
I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.