Generate a dictionary with diagrams that should be SVG.

version = 'ShouldBeSVG 0.2.1'

import pywikibot
from pywikibot import pagegenerators

def getUsage(cat):

    gen = pagegenerators.CategorizedPageGenerator(cat,recurse=1,namespaces=6)


    # Generate a dictionary with diagrams that should be SVG.
    usageCounts = {}

    for page in gen:
        mimetype = pywikibot.FilePage(page).latest_file_info.mime
        if mimetype != 'image/svg+xml':
            try:
                usage = pywikibot.FilePage.globalusage(page)
                l = len(list(usage))
                usageCounts[page] = l
            except (pywikibot.NoUsername, pywikibot.PageRelatedError):
                print('Skipping', page)


    # Sort from greatest to least
    usageCountsSorted = sorted(usageCounts, key=usageCounts.__getitem__, reverse=True)

    # Count the global usage for the top 200 files
    i = 0
    j = 200
    gallery = 'Total number of scanned files: {}\n'.format(len(list(usageCounts)))
    gallery += '<gallery showfilename=yes >\n'
    for page in usageCountsSorted:
        if i < j:
            i += 1
            url = page.full_url()
            title = page.title()
            count = usageCounts[page]
            gallery += '{title}|{i}. Used {count} times.\n'.format(title=title, i=i, count=count)
    gallery += '</gallery>\n[[Category:Images that should use vector graphics]]\n[[Category:Diagram images that should use vector graphics]]'
    return gallery

def savePage(target, text):
    site = pywikibot.Site('commons', 'commons')
    target.text = text
    target.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')

galleryWikitext = getUsage(cat)
#savePage(target, galleryWikitext)
print(galleryWikitext)
WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
Skipping [[commons:File:Article length at newbie edit.png]]
WARNING: Site could not be defined for global usage for [[commons:File:EnwikipediaGom.PNG]]: {'title': 'Wikipedia:Training/Meta', 'wiki': 'test2.wikipedia.org', 'url': 'https://test2.wikipedia.org/wiki/Wikipedia:Training/Meta', 'pageid': '18915', 'ns': '4'}.
WARNING: Site could not be defined for global usage for [[commons:File:EnwikipediaGom.PNG]]: {'title': 'Wikipedia:Training/For_students/How_a_ragtag_band_created_Wikipedia_video', 'wiki': 'test2.wikipedia.org', 'url': 'https://test2.wikipedia.org/wiki/Wikipedia:Training/For_students/How_a_ragtag_band_created_Wikipedia_video', 'pageid': '19201', 'ns': '4'}.
WARNING: Site could not be defined for global usage for [[commons:File:Haifahorizontal.jpg]]: {'title': 'Carmelit', 'wiki': 'wikimania2011.wikimedia.org', 'url': 'https://wikimania2011.wikimedia.org/wiki/Carmelit', 'pageid': '947', 'ns': '0'}.
WARNING: Site could not be defined for global usage for [[commons:File:Haifahorizontal.jpg]]: {'title': 'Carmelit/de', 'wiki': 'wikimania2011.wikimedia.org', 'url': 'https://wikimania2011.wikimedia.org/wiki/Carmelit/de', 'pageid': '1282', 'ns': '0'}.
WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
Skipping [[commons:File:Highly invested editor prop.png]]
WARNING: Site could not be defined for global usage for [[commons:File:Open data worlds.png]]: {'title': 'Fringe/Open_Data_Weekend', 'wiki': 'wikimania2014.wikimedia.org', 'url': 'https://wikimania2014.wikimedia.org/wiki/Fringe/Open_Data_Weekend', 'pageid': '3867', 'ns': '0'}.
WARNING: Site could not be defined for global usage for [[commons:File:Wikipedian-in-Residence Scope Diagram Page 1.png]]: {'title': 'Meetups/Wikipedians_in_residence', 'wiki': 'wikimania2016.wikimedia.org', 'url': 'https://wikimania2016.wikimedia.org/wiki/Meetups/Wikipedians_in_residence', 'pageid': '10162', 'ns': '0'}.
WARNING: Site could not be defined for global usage for [[commons:File:Wikipedian-in-Residence Scope Diagram Page 1.png]]: {'title': 'Meetups/Wikipedians_in_residence/en', 'wiki': 'wikimania2016.wikimedia.org', 'url': 'https://wikimania2016.wikimedia.org/wiki/Meetups/Wikipedians_in_residence/en', 'pageid': '11499', 'ns': '0'}.
---------------------------------------------------------------------------
PageRelatedError                          Traceback (most recent call last)
<ipython-input-1-edc61c3a5746> in <module>
     10 
     11 for page in gen:
---> 12     mimetype = pywikibot.FilePage(page).latest_file_info.mime
     13     if mimetype != 'image/svg+xml':
     14         try:

/srv/paws/pwb/pywikibot/page.py in latest_file_info(self)
   2470         """
   2471         if not len(self._file_revisions):
-> 2472             self.site.loadimageinfo(self, history=True)
   2473         latest_ts = max(self._file_revisions)
   2474         return self._file_revisions[latest_ts]

/srv/paws/pwb/pywikibot/site.py in loadimageinfo(self, page, history, url_width, url_height, url_param)
   3120                 raise PageRelatedError(
   3121                     page,
-> 3122                     'loadimageinfo: Query on %s returned no imageinfo')
   3123 
   3124         return (pageitem['imageinfo']

PageRelatedError: loadimageinfo: Query on [[commons:File:Estradiol.Cycle-es.jpg]] returned no imageinfo
version = 'ShouldBeSVG 0.2.1'

import pywikibot
from pywikibot import pagegenerators

def getUsage(cat):

    gen = pagegenerators.CategorizedPageGenerator(cat,recurse=1,namespaces=6)


    # Generate a dictionary with diagrams that should be SVG.
    usageCounts = {}

    page = pywikibot.Page(site, 'File:Estradiol.Cycle-es.jpg')
    mimetype = pywikibot.FilePage(page).latest_file_info.mime
    if mimetype != 'image/svg+xml' and page != pywikibot.Page(site, 'File:Estradiol.Cycle-es.jpg'):
        try:
            usage = pywikibot.FilePage.globalusage(page)
            l = len(list(usage))
            usageCounts[page] = l
        except pywikibot.NoUsername:
            print('Skipping', page)
        except PageRelatedError:
            print('Skipping', page)
            


    # Sort from greatest to least
    usageCountsSorted = sorted(usageCounts, key=usageCounts.__getitem__, reverse=True)

    # Count the global usage for the top 200 files
    i = 0
    j = 200
    gallery = 'Total number of scanned files: {}\n'.format(len(list(usageCounts)))
    gallery += '<gallery showfilename=yes >\n'
    for page in usageCountsSorted:
        if i < j:
            i += 1
            url = page.full_url()
            title = page.title()
            count = usageCounts[page]
            gallery += '{title}|{i}. Used {count} times.\n'.format(title=title, i=i, count=count)
    gallery += '</gallery>\n[[Category:Images that should use vector graphics]]\n[[Category:Diagram images that should use vector graphics]]'
    return gallery

def savePage(target, text):
    site = pywikibot.Site('commons', 'commons')
    target.text = text
    target.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')

galleryWikitext = getUsage(cat)
#savePage(target, galleryWikitext)
print(galleryWikitext)
---------------------------------------------------------------------------
PageRelatedError                          Traceback (most recent call last)
<ipython-input-5-d8210023abbb> in <module>
     53 target = pywikibot.Page(site, 'Top 200 diagram images that should use vector graphics')
     54 
---> 55 galleryWikitext = getUsage(cat)
     56 #savePage(target, galleryWikitext)
     57 print(galleryWikitext)

<ipython-input-5-d8210023abbb> in getUsage(cat)
     13 
     14     page = pywikibot.Page(site, 'File:Estradiol.Cycle-es.jpg')
---> 15     mimetype = pywikibot.FilePage(page).latest_file_info.mime
     16     if mimetype != 'image/svg+xml' and page != pywikibot.Page(site, 'File:Estradiol.Cycle-es.jpg'):
     17         try:

/srv/paws/pwb/pywikibot/page.py in latest_file_info(self)
   2470         """
   2471         if not len(self._file_revisions):
-> 2472             self.site.loadimageinfo(self, history=True)
   2473         latest_ts = max(self._file_revisions)
   2474         return self._file_revisions[latest_ts]

/srv/paws/pwb/pywikibot/site.py in loadimageinfo(self, page, history, url_width, url_height, url_param)
   3120                 raise PageRelatedError(
   3121                     page,
-> 3122                     'loadimageinfo: Query on %s returned no imageinfo')
   3123 
   3124         return (pageitem['imageinfo']

PageRelatedError: loadimageinfo: Query on [[commons:File:Estradiol.Cycle-es.jpg]] returned no imageinfo

Copyright 2019 AntiCompositeNumber

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.