Skip to content

Instantly share code, notes, and snippets.

@khopkins218
Created April 16, 2015 20:24
Show Gist options
  • Save khopkins218/42d4e3d3c70b57ad27c4 to your computer and use it in GitHub Desktop.
Save khopkins218/42d4e3d3c70b57ad27c4 to your computer and use it in GitHub Desktop.
NPR Story Puller
require 'rest-client'
response = JSON.parse(RestClient.get("http://api.npr.org/query?id=10001&fields=title,teaser,audio,artist&requiredAssets=audio&dateType=story&sort=dateDesc&output=JSON&numResults=20&apiKey=MDE4ODc1MDMwMDE0MjkxNDI3MDc0NTgzNg001"))
stories = {}
response["list"]["story"].each_with_index do |story, index|
stories[index + 1] = {
title: story['title']['$text'],
audio_file: story['audio'].first['format']['mp3'].first['$text']
}
end
puts "Welcome to the NPR Music Latest & Greatest\n\nThis program shows the last 20 stories on NPR Music with audio available for download, and lets you quickly download any of them.\n\nHere are the 20 most recent stories:"
stories.map do |index, story|
puts "#{index}. #{story[:title]}"
end
quit = false
while !quit
puts "\nEnter the number of the audio file you'd like to download, or enter 0 to leave the program."
download = gets.chomp.to_i
if download != 0 && stories.has_key?(download)
system('open', stories[download][:audio_file])
elsif download == 0
quit = true
else
puts "\nSorry, that's not a valid selection."
end
end
puts "Thanks for using NPR Music Latest & Greatest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment