clone your own copy | download snapshot

Snapshots | iceberg

Inside this repository

Download raw (1.3 KB)

#!/usr/bin/env python
# print the urls of all the images in a category of Wikimedia Commons
# example:
# $ python "Category:ScottForesman-raw"

# pipe to wget for download:
# $ python [category] | wget -i - --wait 1

import sys
import json
import urllib2
from urllib import quote

def make_api_query(category, q_continue=""):
    if q_continue:
        q_continue = '&gcmcontinue=' + q_continue
    url = '' + category + q_continue + '&gcmlimit=500&prop=imageinfo&iiprop=url&format=json'
    print url
    request = json.loads(urllib2.urlopen(url).read())
    if 'error' in request:
    for page in request['query']['pages'].values():
            print page['imageinfo'][0]['url']
        except KeyError: pass
    # there is a maximum of 500 results in one request, for paging
    # we use the query-continue value:
    if 'query-continue' in request:
        q_continue = quote(request['query-continue']['categorymembers']['gcmcontinue'])
        make_api_query(category, q_continue)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        sys.exit("usage: python [category]")