class Postdata::Downloader

Constants

JIGYOSYO_PAGE_URL
JIGYOSYO_ZIP_URL
KEN_ALL_PAGE_URL
KEN_ALL_ZIP_URL

Public Class Methods

download(url, file, force_download, logger) click to toggle source
# File lib/postdata/downloader.rb, line 40
def self.download(url, file, force_download, logger)
  msg = "Download #{url} -> #{file}"
  use_cache = !force_download && FileTest.file?(file)
  if use_cache
    msg << " File exists."
  else
    open(url) do |http|
      File.open(file, 'wb') do |f|
        f.write(http.read)
      end
    end
  end
  msg << " Done."
  logger.info(msg) if logger
end
download_all(dir = '', force_download = false, logger = nil) click to toggle source
# File lib/postdata/downloader.rb, line 56
def self.download_all(dir = '', force_download = false, logger = nil)
  zip_ymd =  get_update_date(KEN_ALL_PAGE_URL)
  zip_filename = dir + "/ken_all_#{zip_ymd}.zip"
  download(KEN_ALL_ZIP_URL, zip_filename, force_download, logger)

  jigyo_ymd = get_update_date(JIGYOSYO_PAGE_URL)
  jigyo_filename = dir + "/jigyosyo_#{jigyo_ymd}.zip"
  download(JIGYOSYO_ZIP_URL, jigyo_filename, force_download, logger)
  return zip_filename, zip_ymd, jigyo_filename, jigyo_ymd
end
format_month_or_day(str) click to toggle source
# File lib/postdata/downloader.rb, line 16
def self.format_month_or_day(str)
  if str.length < 2
    return '0' + str
  end
  str
end
get_update_date(url) click to toggle source
# File lib/postdata/downloader.rb, line 23
    def self.get_update_date(url)
      content = open(url, "r:UTF-8").read
#      p content
      if content !~ />(\d+)年(\d+)月(\d+)日更新/
        raise RuntimeError, "更新日が取得できません";
      end
      year = $1
      month = $2
      day = $3

      ymd = year + format_month_or_day(month) + format_month_or_day(day)
      if (ymd.length != 8)
        raise RuntimeError, "日付が不正です: #{ymd}"
      end
      ymd
    end
heisei_to_seireki(heisei) click to toggle source
# File lib/postdata/downloader.rb, line 12
def self.heisei_to_seireki(heisei)
  2000 + heisei.to_i - 12
end