Skip to content

Instantly share code, notes, and snippets.

@libkoi
Last active June 5, 2022 15:15
Show Gist options
  • Save libkoi/993f9bc17bee51b607834f2a39bfd279 to your computer and use it in GitHub Desktop.
Save libkoi/993f9bc17bee51b607834f2a39bfd279 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import re
import pywikibot
from datetime import datetime
LIMIT = 50000000
site = pywikibot.Site('commons', 'commons', user='Crystal-bot')
site.login()
page = pywikibot.Page(pywikibot.Link('LargeImage', default_namespace=10, source=site))
gen = page.getReferences(only_template_inclusion=True, namespaces=6)
curtime = datetime.utcnow()
curtime_ts = curtime.strftime('%Y-%m-%d %H:%M:%S')
count = 0
match = 0
replaced = 0
for fp in (gen):
# if not isinstance(fp, pywikibot.page._filepage.FilePage):
# continue
# if replaced >= 10:
# print("Test completed.")
# break
count += 1
try:
if fp.latest_file_info['width'] * fp.latest_file_info['height'] < LIMIT:
match += 1
curr = re.sub(r"\{\{([Ll]arge ?[Ii]mage|[Tt]ooLarge|[Ii]nteractiveViewer|[Ll]I)\}\}\n?", "", fp.text)
if curr != fp.text:
replaced += 1
fp.text = curr
print(f"{str(replaced)}/{str(count)}: Replacing in {str(fp.title())}")
fp.save("Bot: Remove [[Template:LargeImage]] from files smaller than 50MP", botflag=True)
except:
pass
print("========")
print(f"{curtime_ts}: Found {str(match)}, replaced {str(replaced)} in {str(count)} files.")
print("========")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment