https://gist.github.com/hay/665734

#https://opendata.stackexchange.com/questions/13381/wikimedia-commons-api-image-by-category

#https://commons.wikimedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Specilla_circularia,_Johannes_Hudde&cmlimit=100&cmtype=file&format=json

#Deze!!
#https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Specilla_circularia,_Johannes_Hudde&gcmlimit=500&gcmtype=file&prop=imageinfo&iiprop=url&format=json
        
#https://commons.wikimedia.org/w/api.php?
#action=query
#&generator=categorymembers
#&gcmtitle=Category:Specilla_circularia,_Johannes_Hudde
#&gcmlimit=500
#&gcmtype=file
#&prop=imageinfo
#&iiprop=url
#&format=json

import os
import json
import requests
import urllib.request
import shutil


#==============

commonscat="Category:Specilla_circularia,_Johannes_Hudde"
commonscat_stripped=commonscat[9:]
print(commonscat_stripped)

link="https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=" \
+ commonscat + "&gcmlimit=500&gcmtype=file&prop=imageinfo&iiprop=url&format=json"


with urllib.request.urlopen(link) as url:
    commonsdict = json.loads(url.read().decode())["query"]["pages"]
    keyslist = [*commonsdict] #https://stackoverflow.com/questions/16819222/how-to-return-dictionary-keys-as-a-list-in-python
    #print(commonsdict)
    
for i in range(len(commonsdict)):
    #print(keyslist[i])
    filetitle=commonsdict[keyslist[i]]['title'][5:] #File:Specilla circularia ... strip "File:"
    print(filetitle)
    fileurl=commonsdict[keyslist[i]]['imageinfo'][0]["url"]
    print(fileurl)
    
    #https://stackoverflow.com/questions/3042757/downloading-a-picture-via-urllib-and-python
    
    #TODO: 
    # 0 filter only images (png,jpg,jpeg,tiff,gif), so do not include pdf files
    # 1 Collect all downloaded images in output folder 'commonscat_stripped'
    # 2 zip that folder
    # 3 Make a donload funct for that zip file

    if not os.path.exists(commonscat_stripped):
        os.mkdir(commonscat_stripped)
        
    if not os.path.exists(os.path.join(commonscat_stripped,filetitle)):     
        f = open(os.path.join(commonscat_stripped,filetitle),'wb')
        if filetitle.lower().endswith(('.png', '.jpg', '.jpeg','.tiff')):
            f.write(requests.get(fileurl).content)
        f.close()
        shutil.make_archive(commonscat_stripped, 'zip', commonscat_stripped)
Specilla_circularia,_Johannes_Hudde
Specilla circularia Johannes Hudde 1656 KB KW GW A108988 pag 0-1.jpg
https://upload.wikimedia.org/wikipedia/commons/3/3c/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988_pag_0-1.jpg
Specilla circularia Johannes Hudde 1656 KB KW GW A108988 pag 2-3.jpg
https://upload.wikimedia.org/wikipedia/commons/b/be/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988_pag_2-3.jpg
Specilla circularia Johannes Hudde 1656 KB KW GW A108988 pag 4-5.jpg
https://upload.wikimedia.org/wikipedia/commons/9/9f/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988_pag_4-5.jpg
Specilla circularia Johannes Hudde 1656 KB KW GW A108988 pag 6-7.jpg
https://upload.wikimedia.org/wikipedia/commons/a/a4/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988_pag_6-7.jpg
Specilla circularia Johannes Hudde 1656 KB KW GW A108988 pag 8-9.jpg
https://upload.wikimedia.org/wikipedia/commons/9/95/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988_pag_8-9.jpg
Specilla circularia Johannes Hudde 1656 KB KW GW A108988.pdf
https://upload.wikimedia.org/wikipedia/commons/8/84/Specilla_circularia_Johannes_Hudde_1656_KB_KW_GW_A108988.pdf