Skip to content

Instantly share code, notes, and snippets.

@zph
Created July 31, 2013 01:59
Show Gist options
  • Save zph/6118730 to your computer and use it in GitHub Desktop.
Save zph/6118730 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
require 'json'
require 'CSV'
require 'open-uri'
json_files = Dir.glob("*.json")
content = json_files.map do |file|
JSON.parse open(file).read
end
options = { headers: ["safe_title", "transcript", "alt"], col_sep: "~" }
CSV.open("output.csv", "w+", options) do |csv|
content.each do |row|
row_values = [row["safe_title"], row["transcript"], row["alt"]]
csv << row_values
end
end
require'pry';binding.pry
#!/usr/bin/env ruby
#
#
require 'json'
require 'open-uri'
require 'pry'
nonstandard_url = "http://xkcd.com/info.0.json"
# start_url = "http://xkcd.com/1/info.0.json"
# end_url = "http://xkcd.com/1243/info.0.json"
starting_integer = 1
ending_integer = 2 # 1243
sequential_urls = starting_integer.upto(ending_integer).map do |i|
"http://xkcd.com/#{i}/info.0.json"
end
urls = [nonstandard_url, sequential_urls].flatten
content = urls.map do |url|
response = JSON.parse open(url).read
response["url"] = url
response
end
content.each_with_index do |comic, index|
filename = "#{index}.json"
puts filename
File.write(filename, comic.to_json)
end
require'pry';binding.pry
# take each json
#
#
#
# #{"month"=>"7",
# "num"=>1244,
# "link"=>"",
# "year"=>"2013",
# "news"=>"",
# "safe_title"=>"Six Words",
# "transcript"=>"",
# "alt"=>"Ahem. We are STRICTLY an Orbiter shop.",
# "img"=>"http://imgs.xkcd.com/comics/six_words.png",
# "title"=>"Six Words",
# "day"=>"29",
# "url"=>"http://xkcd.com/info.0.json"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment