Created
November 5, 2012 11:15
-
-
Save jamiecobbett/4016699 to your computer and use it in GitHub Desktop.
List GOV.UK content slugs by section
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'open-uri' | |
require 'cgi' | |
def subsection_uris | |
uri = URI.parse("https://contentapi.production.alphagov.co.uk/tags.json?type=section") | |
json = JSON.parse(uri.open.read) | |
subsections = json["results"].select do |section_tag| | |
! section_tag["parent"].nil? | |
end | |
subsections.map do |subsection| | |
subsection["content_with_tag"]["id"] | |
end | |
end | |
subsection_uris.each do |subsection_uri| | |
begin | |
uri = URI.parse(subsection_uri) | |
f = uri.open | |
json = JSON.parse(f.read) | |
artefact_slugs = json["results"].map do |artefact| | |
artefact["web_url"].split("/")[-1] | |
end | |
puts ([CGI.unescape(subsection_uri.split("tag=")[-1])] + artefact_slugs).join(",") | |
rescue OpenURI::HTTPError => e | |
puts "Encountered #{e.message} for #{uri}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment