version='ShouldBeSVG 0.3.1'importpywikibotfrompywikibotimportpagegeneratorsimportdatetimedefgetUsage(cat):gen=pagegenerators.CategorizedPageGenerator(cat,recurse=1,namespaces=6)# Generate a dictionary with diagrams that should be SVG.usageCounts={}skipped=''forpageingen:try:mimetype=pywikibot.FilePage(page).latest_file_info.mimeexceptpywikibot.PageRelatedError:skipped+=pageprint('Skipping',page)else:ifmimetype!='image/svg+xml':try:usage=pywikibot.FilePage.globalusage(page)l=len(list(usage))usageCounts[page]=lexcept(pywikibot.NoUsername,pywikibot.PageRelatedError):skipped+=pageprint('Skipping',page)# Sort from greatest to leastusageCountsSorted=sorted(usageCounts,key=usageCounts.__getitem__,reverse=True)# Count the global usage for the top 200 filesi=0j=200totalScanned=len(list(usageCounts))gallery+='<gallery showfilename=yes >\n'forpageinusageCountsSorted:ifi<j:i+=1url=page.full_url()title=page.title()count=usageCounts[page]sortedPages+='{title}|{i}. Used {count} times.\n'.format(title=title,i=i,count=count)print('Scanning finished')returnsortedPagesdefconstructGallery(sortedPages,cat,totalScanned,version):date=datetime.date.today()cats="'''[[:{maincat}]]''' ({num} files) \n".format(maincat=cat.title(),num=cat.categoryinfo['files'])pageCats='{maincat}\n[[Category:Images that should use vector graphics]]'.format(maincat=cat.aslink())forsubcatincat.subcategories():"* [[:{subcat}]] ({num} files) \n".format(subcat=subcat.title(),num=subcat.categoryinfo['files'])skippedFiles=''forpageinskipped:skippedFiles+=page.title()gallery='''Last update: {{{{isodate|1={date}}}}}.This report includes the following categories while counting only the usage of each file in the main namespace.{cats}Total number of scanned files: {totalScanned}<gallery showfilename=yes>{sortedPages}</gallery>This report was generated by AntiCompositeBot {version}. The following files were skipped during the generation of this report:{skippedFiles}{pageCats}'''.format(date=date,cats=cats,totalScanned=totalScanned,sortedPages=sortedPages,skippedFiles=skippedFiles,pageCats=pageCats)defsavePage(target,text):site=pywikibot.Site('commons','commons')target.text=texttarget.save(summary='Updating gallery (Bot) ({version})'.format(version=version),botflag=False)site=pywikibot.Site('commons','commons')cat=pywikibot.Category(site,'Category:Diagram images that should use vector graphics')target=pywikibot.Page(site,'Top 200 diagram images that should use vector graphics')print('AntiCompositeBot {version} started at {starttime}'.format(version=version,starttime=datetime.datetime.now().isoformat()))getUsage(cat)constructGallery()#savePage(target, galleryWikitext)print(galleryWikitext)

AntiCompositeBot ShouldBeSVG 0.3.1 started at 2019-08-09T02:08:46.192383