Skip to content

Instantly share code, notes, and snippets.

@mikesname
Created September 20, 2016 12:40
Show Gist options
  • Save mikesname/c1fc743466bf88cad2adad515fa159f2 to your computer and use it in GitHub Desktop.
Save mikesname/c1fc743466bf88cad2adad515fa159f2 to your computer and use it in GitHub Desktop.
Fetch scope and content data for the EHRI v1 API
#!/usr/bin/env python
import sys, requests
if len(sys.argv) < 1:
print("usage: scopecontent.py <url>")
sys.exit(1)
def scope_content(url):
r = requests.get(url, headers={"Authorization": "Bearer EHRI-Dev"})
data = r.json()
for item in data["data"]:
try:
# fetch the ID and first description...
id = item["id"]
description = item["attributes"]["descriptions"][0]
# highlight the heading in bold using these weird escape codes
bold = "\033[1m"
boldend = "\033[0;0m"
print("%s%s [%s]%s" % (bold, description["name"], id, boldend))
print("")
print(description["scopeAndContent"])
print("------------------------------------------------------")
except Exception:
# no scope and content found...
pass
# fetch the next page of data...
if data.get("links") and data["links"].get("next"):
scope_content(data["links"]["next"])
scope_content(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment