This is a script to access the Stack Exchange API for the genealogy site and create a list of user dictionaries. It contains a snippet at end to validate. The data obtained is stored in YAML format in a file for use by other programs to analyze the data.
'''
This is a script to access the stackexchange api
for the genealogy site
and create a list of user dictionaries
'''
# use requests module
# - see http://docs.python-requests.org/en/latest/user/install/#install
import requests
# use YAML to store output for use by other programs
# see http://pyyaml.org/wiki/PyYAML
import yaml
OUTFILENAME = "users.yaml"
# use se api and access genealogy site
# see https://api.stackexchange.com/docs/users for api info
URL ='https://api.stackexchange.com/2.1/users'
url_params = {
'site' : 'genealogy',
'pagesize' : 100,
'order' : 'desc',
'sort' : 'reputation',
}
page = 1
not_done = True
user_list = []
# replies are paginated so loop thru until none left
while not_done:
url_params['page'] = page
# get next page of users
api_response = requests.get(URL,params=url_params)
json_data = api_response.json()
# pull the list of users out of the json answer
user_list.extend( json_data['items'] )
# show progress each time thru loop
print api_response.url
#note only so many queries allowed per day
print '\tquota remaining: %s' % json_data['quota_remaining']
print "\t%s users after page %s" % (len(user_list),page)
# prepare for next iteration if needed
page += 1
not_done = json_data['has_more']
# output list of users to a file in yaml, a format easily readable by humans and parsed
# note safe_dump is used for security reasons
# since dump allows executable python code
# Sidebenefit of safe_dump is cleaner text
outFile = open(OUTFILENAME,"w")
yaml.safe_dump(user_list, outFile)
outFile.close()
# validate it wrote correctly
# note this shows example of how to read
infile = open(OUTFILENAME)
readList = yaml.safe_load(infile)
infile.close()
print 'wrote list of %d users as yaml file %s' % (len(readList),OUTFILENAME)
I wrote this code because I wanted the data it gets from the genealogy site but also:
- to learn APIs in general
- to learn the Stack Exchange API
- to get back into programming which I hadn't done much lately
- I thought I'd try YAML
Since it's such a simple script I didn't bother with objects, but I would be interested in how it could be remade in the functional programming paradigm which I'm not as familiar with.