Skip to content

Instantly share code, notes, and snippets.

@cockscomb
Last active September 30, 2015 15:37
Show Gist options
  • Save cockscomb/1817932 to your computer and use it in GitHub Desktop.
Save cockscomb/1817932 to your computer and use it in GitHub Desktop.
iOS Developer Library の日本語ドキュメント PDF を一括ダウンロードする Ruby スクリプト。Ruby 1.9.x 対応。
#! /usr/bin/env ruby
# encoding: UTF-8
require 'uri'
require 'json'
require 'open-uri'
require 'openssl'
require 'nokogiri'
def get_documents_info()
index_uri = 'https://developer.apple.com/jp/devcenter/ios/library/japanese.html'
open(index_uri, "r", {:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}) do |index_file|
index = Nokogiri::HTML(index_file)
index.xpath('//table[@id="documentsTable"]/tr').each do |tr|
document = {}
document['id'] = tr.attr('id')
document['title'] = tr.xpath('td[@class="title"]/div[@class="jp"]/a').first.content
document['pdf'] = URI.join(index_uri, tr.xpath('td[@class="title"]/div[@class="jp"]/a/@href').first.value)
begin
document['date'] = Date.parse(tr.xpath('td[@class="docRevisionDate"]/div[@class="jp"]').first.content)
rescue ArgumentError => error
document['date'] = Date.today
end
yield document
end
end
end
def download_file(uri, path)
open(uri, "r", {:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}) do |remote_file|
open(path, 'w+b') do |local_file|
local_file.write(remote_file.read)
end
end
end
index_file_name = 'index.json'
if File.exist?(index_file_name) then
index_file = open(index_file_name, 'r:utf-8')
index = JSON.load(index_file)
index_file.close
elsif
index = {}
end
get_documents_info do |document|
downloaded_file = index[document['id']]
if downloaded_file == nil or document['date'] > Date.parse(downloaded_file['date']) then
begin
download_file(document['pdf'], "#{document['title']}.pdf")
rescue
puts "#{document['title']} has not been downloaded."
else
puts "#{document['title']} has been downloaded."
index[document['id']] = document
JSON.dump(index, open(index_file_name, 'w:utf-8'))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment