Created
December 2, 2021 23:18
-
-
Save ryanwoldatwork/1b4c44f0e13934e6ef2241819adbfbfe to your computer and use it in GitHub Desktop.
This file contains 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 'open-uri' | |
require 'json' | |
require 'csv' | |
api_key = "YOUR-API-DATA-GOV-API-KEY-THAT-IS-ALSO-ENTERED-IN-TOUCHPOINTS-GOES-HERE" | |
url = "https://api.gsa.gov/analytics/touchpoints/v1/websites.json?API_KEY=#{api_key}" | |
# Make the API request | |
response = URI.open(url).read | |
# Parse the JSON into a Ruby array and grab the "data" key from the JSON response | |
website_json = JSON.parse(response)["data"] | |
# Select TTS and non-decommissioned sites into a new, smaller array | |
websites = website_json.select do |website| | |
# != Decommissioned, Sub-Office starts with "TTS*". | |
attrs = website["attributes"] | |
puts attrs["domain"] | |
if attrs["sub_office"].include?("TTS") && !attrs["production_status"].include?("decommiss") | |
true | |
else | |
false | |
end | |
end | |
CSV.open("sites.csv", "w") do |csv| | |
websites.each do |website| | |
csv << [ website["attributes"]["domain"] ] | |
end | |
end | |
# sites.csv will be written to this directory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment