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