User:He!ko/Category-tree 2 book

From Strategic Planning
Jump to navigation Jump to search
#!/usr/bin/env python
import urllib
import json


api_url = "http://strategy.wikimedia.org/w/api.php"

# how to retrieve cat members
# ?action=query&list=categorymembers&cmlimit=500&cmtitle=Category:Proposals%20by%20subject

strip_ns = lambda t: t.split(':')[1]

def dump_cat(name, seen):
    print ';%s' % strip_ns(name).encode('utf8')

    # fetch members
    url = api_url +  '?action=query&format=json&list=categorymembers&cmlimit=500&cmtitle=' + name # FIXME

    res = json.loads(urllib.urlopen(url).read())
#    print res

    members = res['query']['categorymembers']

    # filter already seen titles
    members = [m for m in members if not m['title'] in seen]
    seen.update(set(m['title'] for m in members))

    pages_ns = 106 # proposals on strategy #FIXME
    cat_ns = 14
    pages = [x['title'] for x in members if x['ns'] == pages_ns]
    cats = [x['title'] for x in members if x['ns'] == cat_ns]

    


    for p in pages:
        if '/' in p: 
            continue # skip translations
        p = p.encode('utf8')
        print ':[[%s|%s]]' % (p, strip_ns(p))

    for c in cats:
        dump_cat(c, seen)

if __name__ == '__main__':
    seen = set()
    dump_cat('Category:Proposals by subject', seen)