User:XXN/tag nowcommons.py
< User:XXN
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
////////// Core version /////////
On gerrit: https://gerrit.wikimedia.org/r/#/c/323785
Bot tag local files available at Commons with the NowCommons template.
"""
#
# (C) Multichill, 2011
# (C) xqt, 2013
# Ported from compat to core by XXN in 2016.
#
# Distributed under the terms of the MIT license.
#
#from __future__ import absolute_import, unicode_literals
__version__ = '$Id: e9ddae2c4412b27b6e3b6c95a71afb4a833431b7 $'
import sys
import pywikibot
from pywikibot import i18n, pagegenerators
from PIL import Image
from scripts.imagecopy import nowCommonsTemplate
skips = {
'_default': [u'NowCommons'],
'wikipedia': {
'en': [u'NowCommons',
u'CommonsNow',
u'Nowcommons',
u'NowCommonsThis',
u'Nowcommons2',
u'NCT',
u'Nowcommonsthis',
u'Moved to commons',
u'Now Commons',
u'Now at commons',
u'Db-nowcommons',
u'WikimediaCommons',
u'Now commons',
u'Do not move to Commons',
u'KeepLocal',
u'Keeplocal',
u'NoCommons',
u'Nocommons',
u'NotMovedToCommons',
u'Nmtc',
u'Not moved to Commons',
u'Notmovedtocommons',
],
'fy': [u'NowCommons',
u'Nowcommons',
],
'ro': [u'NowCommons',
u'Now Commons',
u'AcumCommons',
u'FOP Romania',
],
'ru': [u'NowCommons',
u'Now Commons',
u'NoCommons',
u'FoP-Russia',
u'FoP-Ukraine',
],
'uk': [u'NowCommons',
u'Now Commons',
u'NoCommons',
u'NoFoP',
],
},
}
class NoEnoughData(pywikibot.Error):
""" Error class for when the user doesn't specified all the data needed """
def tagNowCommons(page):
imagepage = pywikibot.FilePage(page.site, page.title())
site = page.site
language = site.code#site.language()
family = site.family.name
if not imagepage.fileIsOnCommons():
if family in skips and language in skips[family]:
localskips = skips[family][language]
else:
localskips = skips['_default']
for template in imagepage.templates():
if template in localskips:
pywikibot.output(
u'The file %s is already tagged with NowCommons'
% imagepage.title())
return
imagehash = imagepage.latest_file_info.sha1
commons = pywikibot.Site(u'commons', u'commons')#getSite
duplicates = commons.getFilesFromAnHash(imagehash)
if duplicates:
duplicate = duplicates.pop()
pywikibot.output(u'Found duplicate image at %s' % duplicate)
comment = i18n.twtranslate(imagepage.site,
'commons-file-now-available',
{'localfile': imagepage.title(
withNamespace=False),
'commonsfile': duplicate})
if site.lang in nowCommonsTemplate:
template = nowCommonsTemplate[site.lang]
else:
template = nowCommonsTemplate['_default']
newtext = template % (duplicate,) + '\n' + imagepage.get()
pywikibot.showDiff(imagepage.get(), newtext)
try:
imagepage.put(newtext, comment)
except pywikibot.LockedPage:
return
def main(args):
generator = None
genFactory = pagegenerators.GeneratorFactory()
for arg in pywikibot.handleArgs():
genFactory.handleArg(arg)
generator = genFactory.getCombinedGenerator()
if not generator:
raise NoEnoughData(
'You have to specify the generator you want to use for the script!')
pregenerator = pagegenerators.PreloadingGenerator(generator)
for page in pregenerator:
if page.exists() and page.namespace() == 6 and \
not page.isRedirectPage():
tagNowCommons(page)
if __name__ == "__main__":
try:
main(sys.argv[1:])
finally:
pywikibot.stopme()