Forked from cockscomb/ios_japanese_documents_downloader.rb
Last active
August 29, 2015 14:22
-
-
Save Arime9/7c1c5e855305244431aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! /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/documentation/' | |
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