Skip to content

Instantly share code, notes, and snippets.

@trougnouf
Created March 25, 2022 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trougnouf/c51663022827946cfa904e083fa4b8c9 to your computer and use it in GitHub Desktop.
Save trougnouf/c51663022827946cfa904e083fa4b8c9 to your computer and use it in GitHub Desktop.
Add QualityImage template on promoted images missed by the QI bot. (Wikimedia Commons)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Add QualityImage template on promoted images missed by the QI bot. (Wikimedia Commons)
usage: python wikiqi.py "PAGENAME" <USERNAME> "PASSWORD"
egusage: python wikiqi.py "Commons:Quality images candidates/Archives March 21 2022" trougnouf "1234"
@author: trougnouf
"""
import simplemediawiki
import sys
def get_wikitext(wiki, page: str):
'''
args:
page: title (eg: "File:Waterwilg (DSC 2539).jpg")
'''
res = wiki.call({'action': 'parse', 'page': page, 'prop': 'wikitext'})
return res['parse']['wikitext']['*']
def get_token(wiki):
res = wiki.call({'action': 'query', 'meta': 'tokens'})
return res['query']['tokens']['csrftoken']
if __name__ == '__main__':
if '--help' in sys.argv or '-h' in sys.argv:
exit('usage: python wikiqi.py "PAGENAME" <USERNAME> <PASSWORD>')
page_name = sys.argv[1]
username = sys.argv[2]
password = sys.argv[3]
user_agent = simplemediawiki.build_user_agent('TrougnoufsWCBot', 1, 'http://trougnouf.com')
apiurl = 'https://commons.wikimedia.org/w/api.php'
wiki = simplemediawiki.MediaWiki(apiurl, user_agent=user_agent)
wiki.login(username, password)
page_wikitext = get_wikitext(wiki, page_name)
assessments = page_wikitext.split('File:')
promoted_files = []
for assessment in assessments:
if 'Promotion|' in assessment:
promoted_files.append(assessment.split('|')[0])
for promoted_file in promoted_files:
page_name = 'File:'+promoted_file
page_wikitext = get_wikitext(wiki, page_name)
if '{{QualityImage}}' not in page_wikitext:
wiki.call({'action': 'edit', 'title': page_name, 'appendtext': '\n{{QualityImage}}\n', 'token': get_token(wiki), 'bot': True, 'minor': True, 'summary': 'TrougnoufsWCBot: add quality assessment'})
print(f'Added quality assessment to {page_name}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment