#! /usr/bin/env ruby require 'cablegator' require 'cablegator/wikileaks' dir_prefix = ARGV[0] || Dir.pwd puts "Downloading cables to #{File.expand_path(dir_prefix)}" doc = Nokogiri::HTML(WikiLeaks.home) doc.css(%{a[href^='/date']}).each do |link| page_with_cables = Nokogiri::HTML(WikiLeaks.get(link.attributes['href'].value)) page_with_cables.css(%{a[href^='/cable']}).each do |cable| cable_url = cable.attributes['href'].value file_location = File.expand_path(dir_prefix + cable_url) reference_id = File.basename(cable_url).gsub(File.extname(cable_url),'') if !File.exist?(file_location) FileUtils.mkdir_p(File.dirname(file_location)) STDOUT.puts "Downloading cable #{reference_id} to #{file_location}" File.open(file_location, 'w') { |f| f << WikiLeaks.get(cable_url) } else STDOUT.puts "You already have #{reference_id} in #{file_location}" end end end