version = 'ShouldBeSVG 0.3.1'

import pywikibot
from pywikibot import pagegenerators
import datetime

def getUsage(cat):

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


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

    for page in gen:
        try:
            mimetype = pywikibot.FilePage(page).latest_file_info.mime
        except pywikibot.PageRelatedError:
            skipped += page
            print('Skipping', page)
        else:
            if mimetype != 'image/svg+xml':
                try:
                    usage = pywikibot.FilePage.globalusage(page)
                    l = len(list(usage))
                    usageCounts[page] = l
                except (pywikibot.NoUsername, pywikibot.PageRelatedError):
                    skipped += page
                    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
    totalScanned = 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]
            sortedPages += '{title}|{i}. Used {count} times.\n'.format(title=title, i=i, count=count)
    print('Scanning finished')
    return sortedPages

def constructGallery(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())
    for subcat in cat.subcategories():
        "* [[:{subcat}]] ({num} files) \n".format(subcat=subcat.title(), num=subcat.categoryinfo['files'])
    skippedFiles = ''
    for page in skipped:
        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)
    

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')

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
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/srv/paws/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    378                 # Python 2.7, use buffering of HTTP responses
--> 379                 httplib_response = conn.getresponse(buffering=True)
    380             except TypeError:

TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-8-dbec4c877675> in <module>
     84 
     85 print('AntiCompositeBot {version} started at {starttime}'.format(version=version, starttime=datetime.datetime.now().isoformat()))
---> 86 getUsage(cat)
     87 constructGallery()
     88 #savePage(target, galleryWikitext)

<ipython-input-8-dbec4c877675> in getUsage(cat)
     24                 try:
     25                     usage = pywikibot.FilePage.globalusage(page)
---> 26                     l = len(list(usage))
     27                     usageCounts[page] = l
     28                 except (pywikibot.NoUsername, pywikibot.PageRelatedError):

/srv/paws/pwb/pywikibot/site.py in globalusage(self, page, total)
   3058                                 **args)
   3059 
-> 3060         for pageitem in query:
   3061             if not self.sametitle(pageitem['title'],
   3062                                   page.title(with_section=False)):

/srv/paws/pwb/pywikibot/data/api.py in __iter__(self)
   2981         """Yield results."""
   2982         self._previous_dicts = {}
-> 2983         for result in super(PropertyGenerator, self).__iter__():
   2984             yield result
   2985         for result in self._previous_dicts.values():

/srv/paws/pwb/pywikibot/data/api.py in __iter__(self)
   2805                 prev_limit, new_limit, previous_result_had_data)
   2806             if not hasattr(self, 'data'):
-> 2807                 self.data = self.request.submit()
   2808             if not self.data or not isinstance(self.data, dict):
   2809                 pywikibot.debug(

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   1983                                                                    paramstring)
   1984             rawdata, use_get = self._http_request(use_get, uri, body, headers,
-> 1985                                                   paramstring)
   1986             if rawdata is None:
   1987                 continue

/srv/paws/pwb/pywikibot/data/api.py in _http_request(self, use_get, uri, body, headers, paramstring)
   1742                 site=self.site, uri=uri,
   1743                 method='GET' if use_get else 'POST',
-> 1744                 body=body, headers=headers)
   1745         except Server504Error:
   1746             pywikibot.log('Caught HTTP 504 error; retrying')

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/comms/http.py in request(site, uri, method, params, body, headers, data, **kwargs)
    321 
    322     baseuri = site.base_url(uri)
--> 323     r = fetch(baseuri, method, params, body, headers, **kwargs)
    324     site.throttle.retry_after = int(r.response_headers.get('retry-after', 0))
    325     return r.text

/srv/paws/pwb/pywikibot/comms/http.py in fetch(uri, method, params, body, headers, default_error_handling, use_fake_user_agent, data, **kwargs)
    519             headers['user-agent'] = fake_user_agent()
    520 
--> 521     request = _enqueue(uri, method, params, body, headers, **kwargs)
    522     # if there's no data in the answer we're in trouble
    523     assert request._data is not None

/srv/paws/pwb/pywikibot/comms/http.py in _enqueue(uri, method, params, body, headers, data, **kwargs)
    475     request = threadedhttp.HttpRequest(
    476         uri, method, params, body, all_headers, callbacks, **kwargs)
--> 477     _http_process(session, request)
    478     return request
    479 

/srv/paws/pwb/pywikibot/comms/http.py in _http_process(session, http_request)
    388                                    headers=headers, auth=auth, timeout=timeout,
    389                                    verify=not ignore_validation,
--> 390                                    **http_request.kwargs)
    391     except Exception as e:
    392         http_request.data = e

/srv/paws/lib/python3.6/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    531         }
    532         send_kwargs.update(settings)
--> 533         resp = self.send(prep, **send_kwargs)
    534 
    535         return resp

/srv/paws/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    644 
    645         # Send the request
--> 646         r = adapter.send(request, **kwargs)
    647 
    648         # Total elapsed time of the request (approximately)

/srv/paws/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    447                     decode_content=False,
    448                     retries=self.max_retries,
--> 449                     timeout=timeout
    450                 )
    451 

/srv/paws/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    601                                                   timeout=timeout_obj,
    602                                                   body=body, headers=headers,
--> 603                                                   chunked=chunked)
    604 
    605             # If we're going to release the connection in ``finally:``, then

/srv/paws/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    381                 # Python 3
    382                 try:
--> 383                     httplib_response = conn.getresponse()
    384                 except Exception as e:
    385                     # Remove the TypeError from the exception chain in Python 3;

/usr/lib/python3.6/http/client.py in getresponse(self)
   1329         try:
   1330             try:
-> 1331                 response.begin()
   1332             except ConnectionError:
   1333                 self.close()

/usr/lib/python3.6/http/client.py in begin(self)
    295         # read until we get a non-100 response
    296         while True:
--> 297             version, status, reason = self._read_status()
    298             if status != CONTINUE:
    299                 break

/usr/lib/python3.6/http/client.py in _read_status(self)
    256 
    257     def _read_status(self):
--> 258         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    259         if len(line) > _MAXLINE:
    260             raise LineTooLong("status line")

/usr/lib/python3.6/socket.py in readinto(self, b)
    584         while True:
    585             try:
--> 586                 return self._sock.recv_into(b)
    587             except timeout:
    588                 self._timeout_occurred = True

/usr/lib/python3.6/ssl.py in recv_into(self, buffer, nbytes, flags)
   1010                   "non-zero flags not allowed in calls to recv_into() on %s" %
   1011                   self.__class__)
-> 1012             return self.read(nbytes, buffer)
   1013         else:
   1014             return socket.recv_into(self, buffer, nbytes, flags)

/usr/lib/python3.6/ssl.py in read(self, len, buffer)
    872             raise ValueError("Read on closed or unwrapped SSL socket.")
    873         try:
--> 874             return self._sslobj.read(len, buffer)
    875         except SSLError as x:
    876             if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:

/usr/lib/python3.6/ssl.py in read(self, len, buffer)
    629         """
    630         if buffer is not None:
--> 631             v = self._sslobj.read(len, buffer)
    632         else:
    633             v = self._sslobj.read(len)

KeyboardInterrupt: