Python:Daily deleter.py

From Botwiki

Jump to: navigation, search
import time, locale, re, sys, os
sys.path.append(os.environ['HOME']+'/pywikipedia')
from optparse import OptionParser
 
CATEGORIES = {
        u'CAT:NS' : {
            'prefix'  : u'Category:Images with unknown source as of ',
            'days'    : 7,
            'summary' : u'Image with unknown source. ([[WP:CSD#I4]])',
            },
        u'CAT:NT' : {
            'prefix'  : u'Category:Images with no copyright tag as of ',
            'days'    : 7,
            'summary' : u'Image with no copyright tag. ([[WP:CSD#I4]])',
            },
        u'CAT:ORFU' : {
            'prefix'  : u'Category:Orphaned fairuse images as of ',
            'days'    : 7,
            'summary' : u'Unused unfree copyrighted image. ([[WP:CSD#I5]])',
            },
        }
 
WHITELIST = [
        'Example',
        'OrphanBot',
        #List of users allowed to tag images
        ]
 
def approved_edit(Edit, editors, daysMin, daysMax):
    EditAge = time.time() - time.mktime(time.strptime(Edit[1],'%H:%M, %d %B %Y'))
    return daysMin*86400 <= EditAge and EditAge <= daysMax*86400 and Edit[2] in editors
 
class del_callback(object):
    def __init__ (self, editors, days):
        self.editors = editors
        self.days = days
 
    def __call__(self, Page):
        text = Page.get()
 
        if re.search('{{(PD|GFDL|L?GPL|cc|money|free)[^}]*}}',text,re.IGNORECASE):
            wikipedia.output(u'Image is tagged with a free license tag.')
            return False
 
        ImagePage = wikipedia.ImagePage(Page.site(),Page.title())
        LinkCount = len([x for x in ImagePage.usingPages() if x.namespace()==0])
        if LinkCount > 0:
            wikipedia.output(u'Image is not orphaned.')
            return False
 
        wikipedia.output(u'Image is orphaned.')
 
        Edit = Page.getVersionHistory()[0]
        if not approved_edit(Edit, self.editors, self.days-2, self.days*3):
            wikipedia.output(u'Last edit by an approved user [[User:%s]] or not within allowed window.' % Edit[2])
            return False
 
        return True
 
def main(options, args):
    locale.setlocale(locale.LC_TIME,('en_US','utf-8')) #English month names
 
    for short,cat in CATEGORIES.iteritems():
        if options.category and options.category != short:
            continue
 
        if options.day:
            day = options.day
        else:
            day = time.strftime('%d %B %Y',time.gmtime(time.time()-cat['days']*24*60*60))
 
        day = cat['prefix'] + re.sub('^0','',day)
 
        if options.threads:
            num_threads = int(options.threads)
        else:
            num_threads = 1
 
        CB = del_callback(WHITELIST,cat['days'])
        if not catclear.main(day,cat['summary'],CB,num_threads):
            return
 
if __name__ == '__main__':
    parser = OptionParser(usage='usage: %prog [options]')
 
    parser.add_option('-d', '--day', dest='day',
            help='work in a specific DAY', metavar='DAY')
    parser.add_option('-c', '--category', dest='category',
            help='only work in the specified CATEGORY', metavar='CATEGORY')
    parser.add_option('-t', '--threads', dest='threads',
            help='number of concurrent threads', metavar='THREADS')
 
    (options, args) = parser.parse_args()
 
    try:
        import wikipedia, catclear
        main(options, args)
    finally:
        wikipedia.stopme()
Personal tools