Source code for scripts.imageuncat

# -*- coding: utf-8 -*-
Program to add uncat template to images without categories at commons.

See to add these images to categories.

This script is working on the given site, so if the commons should be handled,
the site commons should be given and not a Wikipedia or similar.

# (C) Pywikibot team, 2008-2019
# Distributed under the terms of the MIT license.
from __future__ import absolute_import, division, unicode_literals

from datetime import timedelta

import pywikibot
from pywikibot.exceptions import ArgumentDeprecationWarning
from pywikibot import pagegenerators
from import issue_deprecation_warning

docuReplacements = {
    '&params;': pagegenerators.parameterHelp,

# Probably unneeded because these are hidden categories. Have to figure it out.
ignoreCategories = ['[[Category:CC-BY-SA-3.0]]',
                    '[[Category:Media for cleanup]]',
                    '[[Category:Media lacking a description]]',
                    '[[Category:Media lacking author information]]',
                    '[[Category:Media lacking a description]]',
                    '[[Category:Self-published work]]']

# Don't bother to put the template on a image with one of these templates
skipTemplates = ['Delete',
                 'No license',
                 'No permission since',
                 'No source',
                 'No source since',

# Ignore templates in this long list when looking for relevant categories
ignoreTemplates = ['1000Bit',
                   '1922 cyc',
                   'Agência Brasil',
                   'Anonymous work',
                   'Attribution Entomart',
                   'Autorisation photos aériennes Francis Leroy',
                   'Blue Marble',
                   'Botev license',
                   'Botev relicensed',
                   'Civertan license',
                   'Coloured SVG boxes',
                   'Common Public License',
                   'Convert to SVG',
                   'Copyright by Wikimedia',
                   'Copyright jalpeyrie',
                   'Copyrighted free use',
                   'Copyrighted free use provided that',
                   'Copyrighted IOC',
                   'Created by ForrestSjap',
                   'Created with Inkscape',
                   'Credit line',
                   'Croatian currency',
                   'Debora Cordeiro',
                   'EEK banknote',
                   'EEK coin',
                   'Elephants Dream',
                   'En icon',
                   'Erin Silversmith Licence',
                   'EST-Land Board',
                   'EU location',
                   'Euro coin common face',
                   'Europe location',
                   'Faroe stamps',
                   'Fr icon',
                   'Free screenshot',
                   'Gakk Copyright',
                   'GFDL or cc-by-nc-sa',
                   'GFDL or cc-by-nc-sa/2.5',
                   'GFDL-Institut de Qualitat Agroalimentària',
                   'Giovannino Copyright',
                   'GPLv2 only',
                   'Grays Anatomy plate',
                   'Halibutt Copyright GFDL',
                   'Halibutt Copyright Ogg',
                   'Heraldic Badge',
                   'Hiuppo Copyright GFDL',
                   'Icelandic currency',
                   'Icelandic stamp',
                   'Information Picswiss',
                   'Insignia Catalonia',
                   'Insignia Navarre',
                   'It icon',
                   'James K. Lindsey permission',
                   'Joergens.mi licence',
                   'JPEG version of PNG',
                   'Kameno doba',
                   'LibriVox public domain',
                   'Location dec',
                   'Location dms',
                   'Map of Japan-Shigenobu AOKI',
                   'Master son',
                   'Money-EU banknote',
                   'Newsflash Photo',
                   'Nl icon',
                   'Nn icon',
                   'No icon',
                   'Norges Golfforbund',
                   'Norwegian coat of arms',
                   'Nrbelex Photo',
                   'Nrbelex Photo 400D',
                   'NYC Subway map',
                   'Object location',
                   'Olessi Copyright Ogg',
                   'Open Beelden',
                   'Open Font',
                   'Originally uploaded',
                   'Otrs pending',
                   'OTRS pending',
                   'Own work',
                   'PD Colombia',
                   'PD patents',
                   'PD-Abdul Hamid',
                   'PD-Challenger Report',
                   'PD-collective anonymous work',
                   'PD-copyright holder',
                   'PD-DBZ stamps',
                   'PD-Deutsche Bundespost stamps',
                   'PD-Edison Records',
                   'PD-EU-no author disclosure',
                   'PD-Fröléens konversationslexikon',
                   'PD-GDR stamps',
                   'PD-German Empire stamps',
                   'PD-German postmarks',
                   'PD-German stamps',
                   'PD-Google books',
                   'PD-National Photo Company',
                   'PD-Nordens Flora',
                   'PD-North Korea',
                   'PD-ROC-Traffic Indicating Lines',
                   'PD-ROC-Traffic Signs',
                   'PD-Saudi Arabia',
                   'PD-South Korea',
                   'PD-United Arab Emirates',
                   'PD-US-no notice',
                   'PD-US-not renewed',
                   'PD-US-patent-no notice',
                   'PD-USGov-Federal Reserve',
                   'PD-USGov-Military award',
                   'PD-USGov-Military-Air Force',
                   'PD-USGov-Military-Air Force Auxiliary',
                   'PD-USGov-Military-Coast Guard',
                   'PD-USGov-Military-National Guard',
                   'PD-Van Vechten',
                   'Personality rights',
                   'Photos by Bagn Bygdesamling',
                   'Photos by Kulturhistorisk museum',
                   'Photos by Stortinget',
                   'Photos by the Norwegian Museum of Cultural History',
                   'Pl icon',
                   'PNG with JPEG version',
                   'Polish coats of arms by Tadeusz Gajl',
                   'Politas FZ30',
                   'Pressefotos Die Gruenen',
                   'Pt icon',
                   'RTCNCA License',
                   'Seattle Neighborhood Atlas disclaimer',
                   'Second Life',
                   'Simple English',
                   'Solkoll 2D',
                   'Solkoll 3D',
                   'South Korean currency',
                   'Stan Shebs photo',
                   'Statistics Netherlands map',
                   'Stielers Handatlas 1891',
                   'Supported by Wikimedia France',
                   'Svensk porträttgalleri',
                   'SVG-Map Africa by Slomox',
                   'Swiss Government Portrait',
                   'Texas State Highway',
                   'UN map',
                   'Vector version available',
                   'Wikimedia relicensing',
                   'Wulfstan GFDL',
                   'XGSC image',

puttext = ('\n{{Uncategorized|year={{subst:CURRENTYEAR}}|'
putcomment = 'Please add categories to this image'

[docs]def uploadedYesterday(site): """ Return a pagegenerator containing all the pictures uploaded yesterday. DEPRECATED. Only used by a deprecated option. """ today = pywikibot.Timestamp.utcnow() yesterday = today + timedelta(days=-1) for logentry in site.logevents( logtype='upload', start=yesterday, end=today, reverse=True ): yield
[docs]def isUncat(page): """ Do we want to skip this page. If we found a category which is not in the ignore list it means that the page is categorized so skip the page. If we found a template which is in the ignore list, skip the page. """ pywikibot.output('Working on ' + page.title()) for category in page.categories(): if category not in ignoreCategories: pywikibot.output('Got category ' + category.title()) return False for template_with_trail in page.templates(): # Strip of trailing garbage template = template_with_trail.title().rstrip('\n').rstrip() if template in skipTemplates: # Already tagged with a template, skip it pywikibot.output('Already tagged, skip it') return False elif template in ignoreTemplates: # template not relevant for categorization pywikibot.output('Ignore ' + template) else: pywikibot.output('Not ignoring ' + template) return False return True
[docs]def addUncat(page): """ Add the uncat template to the page. @param page: Page to be modified @type page: pywikibot.Page """ newtext = page.get() + puttext pywikibot.showDiff(page.get(), newtext) try: page.put(newtext, putcomment) except pywikibot.EditConflict: # Skip this page pass except pywikibot.LockedPage: # Skip this page pass return
[docs]def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ generator = None local_args = pywikibot.handle_args(args) site = pywikibot.Site() if site.code != 'commons' or != 'commons': pywikibot.warning('This script is primarily written for Wikimedia ' 'Commons, but has been invoked with site {0}. It ' 'might work for other sites but there is no ' 'guarantee that it does the right thing.' .format(site)) choice = pywikibot.input_choice( 'How do you want to continue?', (('Continue using {0}'.format(site), 'c'), ('Switch to Wikimedia Commons', 's'), ('Quit', 'q')), automatic_quit=False) if choice == 's': site = pywikibot.Site('commons', 'commons') elif choice == 'q': return False gen_factory = pagegenerators.GeneratorFactory(site) for arg in local_args: param_arg, sep, param_value = arg.partition(':') if param_value == '': param_value = None if arg.startswith('-yesterday'): generator = uploadedYesterday(site) issue_deprecation_warning( 'The usage of "-yesterday"', '-logevents:"upload,,YYYYMMDD,YYYYMMDD"', 2, ArgumentDeprecationWarning, since='20160305') elif arg.startswith('-recentchanges'): if param_value is None: arg = arg + ':120,70' issue_deprecation_warning( '-recentchanges without parameters', '-recentchanges:offset,duration', 2, ArgumentDeprecationWarning, since='20160320') gen_factory.handleArg(arg) else: gen_factory.handleArg(arg) generator = gen_factory.getCombinedGenerator(gen=generator, preload=True) if not generator: else: site.login() for page in generator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page)
if __name__ == '__main__': main()