Skip to content

Commit

Permalink
Fetch big pages from registry API
Browse files Browse the repository at this point in the history
  • Loading branch information
andylolz committed Jun 6, 2018
1 parent cf2c635 commit e00381d
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,35 @@

import requests
import os

# Fetch a list of all organisations - returns json
r = requests.get('http://iatiregistry.org/api/3/action/organization_list')
import json

#Make a directory to save the data about each publisher
try:
os.makedirs(os.path.join('data','ckan_publishers'))
except OSError:
pass

page_size = 100
url = 'https://iatiregistry.org/api/3/action/organization_list'
params = {
'all_fields': 'true',
'include_extras': 'true',
'include_tags': 'true',
'include_groups': 'true',
'include_users': 'true',
'limit': page_size,
}

# Loop through the organisation list json, saving a file of information about each publisher
for publisher in r.json()['result']:
r2 = requests.get('http://iatiregistry.org/api/3/action/organization_show', params={'id':publisher})
with open(os.path.join('data', 'ckan_publishers', publisher+'.json'), 'w') as fp:
fp.write(r2.text)
page = 0
while True:
params['offset'] = page_size * page
res = requests.get(url, params=params).json()['result']
if res == []:
break
for publisher in res:
name = publisher.get('name')
output = {'result': publisher}
with open(os.path.join('data', 'ckan_publishers', name + '.json'), 'w') as fp:
_ = json.dump(output, fp)
page += 1

0 comments on commit e00381d

Please sign in to comment.