def main():
    '''
    The main loop
    '''
    wikipedia.setSite(wikipedia.getSite(u'commons', u'commons'))
    conn = None
    cursor = None
    (conn, cursor) = connectDatabase()

    imagerecat.initLists()
    generator = None;
    genFactory = pagegenerators.GeneratorFactory()

    mark = True

    for arg in wikipedia.handleArgs():
	if arg.startswith('-dontmark'):
	    mark = False
        elif arg.startswith('-page'):
            if len(arg) == 5:
                generator = [wikipedia.Page(wikipedia.getSite(), wikipedia.input(u'What page do you want to use?'))]
            else:
                generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])]
	elif arg.startswith('-yesterday'):
	    generator = [wikipedia.Page(wikipedia.getSite(), u'Category:Media_needing_categories_as_of_' + getYesterday())]
        else:
            generator = genFactory.handleArg(arg)
    if generator:
        for page in generator:
	    if((page.namespace() == 14) and (page.title().startswith(u'Category:Media needing categories as of'))):
		wikipedia.output(u'Working on ' + page.title())
		for (image, gals, cats) in getImagesToCategorize(cursor, page.titleWithoutNamespace()):
		    categorizeImage(image, gals, imagerecat.applyAllFilters(cats))
		if (mark):
		    categoriesChecked(page.title())
def cleanUpCategories(description =''):
    '''
    Filter the categories in the description using the functions in imagerecat
    '''
    #Get the list of current categories
    categoryPages = wikipedia.getCategoryLinks(description, wikipedia.getSite())

    #Make it a list of strings (not page objects)
    categories = []
    for cat in categoryPages:
	categories.append(cat.titleWithoutNamespace())

    #Strip the categories of the current description
    description = wikipedia.removeCategoryLinks(description, wikipedia.getSite())    

    #Filter the list of categories
    categories = imagerecat.applyAllFilters(categories)
    
    #If we have a category, remove the uncat template
    if not (categories==''):
	description = description.replace(u'{{subst:unc}}', u'')

    #Add the categories to the description again
    description = description + u'\n'
    for category in categories:
	#print u'Category : ' + category
	description = description + u'[[Category:' + category + u']]\n'
    return description
def replaceCategory (image = None, parents = [], newcat = u''):
    '''
    Remove all parent categories and add newcat
    '''
    result = 0
    newcats = []
    if not newcat == u'':    
        currentCats = imagerecat.getCurrentCats(image)
	workingCategories = currentCats
	workingCategories.append(newcat)
	# Adding parents if the category filter is lagging.
	# The bot often works on new categories. In these cases the filter does know the parent categories
	workingCategories = workingCategories + parents
        for cat in imagerecat.applyAllFilters(workingCategories):
	    #Now remove those parents again
	    if cat not in parents:
                newcats.append(cat)
	if not(set(currentCats)==set(newcats)):
	    newtext = wikipedia.removeCategoryLinks(image.get(), image.site()) + u'\n'
	    for category in newcats:
	        newtext = newtext + u'[[Category:' + category + u']]\n'
	    comment = u'Moving image to (a subcategory of) [[Category:' + newcat + u']] and trying to filter categories'
	    wikipedia.output(image.title())
	    wikipedia.showDiff(image.get(), newtext)
	    image.put(newtext, comment)
            result = 1
    return result
Esempio n. 4
0
def cleanUpCategories(description=''):
    '''
    Filter the categories in the description using the functions in imagerecat
    '''
    #Get the list of current categories
    categoryPages = wikipedia.getCategoryLinks(description,
                                               wikipedia.getSite())

    #Make it a list of strings (not page objects)
    categories = []
    for cat in categoryPages:
        categories.append(cat.titleWithoutNamespace())

    #Strip the categories of the current description
    description = wikipedia.removeCategoryLinks(description,
                                                wikipedia.getSite())

    #Filter the list of categories
    categories = imagerecat.applyAllFilters(categories)

    #If we have a category, remove the uncat template
    if not (categories == ''):
        description = description.replace(u'{{subst:unc}}', u'')

    #Add the categories to the description again
    description = description + u'\n'
    for category in categories:
        #print u'Category : ' + category
        description = description + u'[[Category:' + category + u']]\n'
    return description
Esempio n. 5
0
 def getNewCategories(self, imagepage):
     '''
     Get a categories for the image
     Dont forget to filter
     '''
     result = u''
     (commonshelperCats, usage, galleries) = imagerecat.getCommonshelperCats(imagepage)
     newcats = imagerecat.applyAllFilters(commonshelperCats)
     for newcat in newcats:
         result = result + u'[[Category:' + newcat + u']] '
     return result
 def getNewCategories(self, imagepage):
     '''
     Get a categories for the image
     Dont forget to filter
     '''
     result = u''
     (commonshelperCats, usage, galleries) = imagerecat.getCommonshelperCats(imagepage)
     newcats = imagerecat.applyAllFilters(commonshelperCats)
     for newcat in newcats:
         result = result + u'[[Category:' + newcat + u']] '
     return result
 def getNewCategories(self, imagepage):
     """
     Get a categories for the image
     Dont forget to filter
     """
     result = u""
     (commonshelperCats, usage, galleries) = imagerecat.getCommonshelperCats(imagepage)
     newcats = imagerecat.applyAllFilters(commonshelperCats)
     for newcat in newcats:
         result += u"[[Category:" + newcat + u"]] "
     return result
Esempio n. 8
0
def getDescription(photoInfo=None,
                   panoramioreview=False,
                   reviewer=u'',
                   override=u'',
                   addCategory=u''):
    '''
    Build description for the image.
    '''

    desc = u''
    desc = desc + u'{{Information\n'
    desc = desc + u'|description=%(photo_title)s\n'
    desc = desc + u'|date=%(upload_date)s (upload date)\n'
    desc = desc + u'|source=[%(photo_url)s Panoramio]\n'
    desc = desc + u'|author=[%(owner_url)s?with_photo_id=%(photo_id)s %(owner_name)s] \n'
    desc = desc + u'|permission=\n'
    desc = desc + u'|other_versions=\n'
    desc = desc + u'|other_fields=\n'
    desc = desc + u'}}\n'
    if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'):
        desc = desc + u'{{Location dec|%(latitude)s|%(longitude)s|source:Panoramio}}\n'
    desc = desc + u'\n'
    desc = desc + u'=={{int:license-header}}==\n'

    if override:
        desc = desc + override
    else:
        if photoInfo.get(u'license') == u'by-sa':
            desc = desc + u'{{Cc-by-sa-3.0}}\n'
        if panoramioreview:
            desc = desc + u'{{Panoramioreview|%s|{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY2}}}}\n' % (
                reviewer, )
        else:
            desc = desc + u'{{Panoramioreview}}\n'

    desc = desc + u'\n'
    cats = u''
    if addCategory:
        desc = desc + u'\n[[Category:%s]]\n' % (addCategory, )
        cats = True

    # Get categories based on location
    if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'):
        cats = imagerecat.getOpenStreetMapCats(photoInfo.get(u'latitude'),
                                               photoInfo.get(u'longitude'))
        cats = imagerecat.applyAllFilters(cats)
        for cat in cats:
            desc = desc + u'[[Category:%s]]\n' % (cat, )
    if not cats:
        desc = desc + u'{{subst:Unc}}\n'

    return desc % photoInfo
def getDescription(photoInfo=None, panoramioreview=False, reviewer=u'',
                     override=u'', addCategory=u''):
    '''
    Build description for the image.
    '''

    desc = u''
    desc = desc + u'{{Information\n'
    desc = desc + u'|description=%(photo_title)s\n'
    desc = desc + u'|date=%(upload_date)s (upload date)\n'
    desc = desc + u'|source=[%(photo_url)s Panoramio]\n'
    desc = desc + u'|author=[%(owner_url)s?with_photo_id=%(photo_id)s %(owner_name)s] \n'
    desc = desc + u'|permission=\n'
    desc = desc + u'|other_versions=\n'
    desc = desc + u'|other_fields=\n'
    desc = desc + u'}}\n'
    if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'):
        desc = desc + u'{{Location dec|%(latitude)s|%(longitude)s|source:Panoramio}}\n'
    desc = desc + u'\n'
    desc = desc + u'=={{int:license-header}}==\n'

    if override:
        desc = desc + override
    else:
        if photoInfo.get(u'license')==u'by-sa':
            desc = desc + u'{{Cc-by-sa-3.0}}\n'
        if panoramioreview:
            desc = desc + u'{{Panoramioreview|%s|{{subst:CURRENTYEAR}}-{{subst:CURRENTMONTH}}-{{subst:CURRENTDAY2}}}}\n' % (reviewer,)
        else:
            desc = desc + u'{{Panoramioreview}}\n'

    desc = desc + u'\n'
    cats = u''
    if addCategory:
        desc = desc + u'\n[[Category:%s]]\n' % (addCategory,)
        cats = True

    # Get categories based on location
    if photoInfo.get(u'latitude') and photoInfo.get(u'longitude'):
        cats=imagerecat.getOpenStreetMapCats(photoInfo.get(u'latitude'), photoInfo.get(u'longitude'))
        cats=imagerecat.applyAllFilters(cats)
        for cat in cats:
            desc = desc + u'[[Category:%s]]\n' % (cat,)
    if not cats:
        desc = desc + u'{{subst:Unc}}\n'

    return desc % photoInfo
Esempio n. 10
0
def main():
    '''
    The main loop
    '''
    wikipedia.setSite(wikipedia.getSite(u'commons', u'commons'))
    conn = None
    cursor = None
    (conn, cursor) = connectDatabase()

    imagerecat.initLists()
    generator = None
    genFactory = pagegenerators.GeneratorFactory()

    mark = True

    for arg in wikipedia.handleArgs():
        if arg.startswith('-dontmark'):
            mark = False
        elif arg.startswith('-page'):
            if len(arg) == 5:
                generator = [
                    wikipedia.Page(
                        wikipedia.getSite(),
                        wikipedia.input(u'What page do you want to use?'))
                ]
            else:
                generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])]
        elif arg.startswith('-yesterday'):
            generator = [
                wikipedia.Page(
                    wikipedia.getSite(),
                    u'Category:Media_needing_categories_as_of_' +
                    getYesterday())
            ]
        else:
            generator = genFactory.handleArg(arg)
    if generator:
        for page in generator:
            if ((page.namespace() == 14) and (page.title().startswith(
                    u'Category:Media needing categories as of'))):
                wikipedia.output(u'Working on ' + page.title())
                for (image, gals, cats) in getImagesToCategorize(
                        cursor, page.titleWithoutNamespace()):
                    categorizeImage(image, gals,
                                    imagerecat.applyAllFilters(cats))
                if (mark):
                    categoriesChecked(page.title())