fork download
  1. #!/usr/bin/ruby
  2. require 'narou'
  3. require 'fileutils'
  4. $: << File.join(Narou.get_script_dir, "lib")
  5. require 'downloader'
  6. require 'helper'
  7.  
  8. abort "Usage: $myepubsplit.rb novel_id split_size" if ARGV.size == 0
  9.  
  10. novel_id = Integer(ARGV[0])
  11. data = Downloader.get_data_by_target novel_id
  12. abort "novel_id #{novel_id} が見つかりません" if data == nil
  13.  
  14. narou_root = Narou.get_root_dir
  15. abort ".narou ディレクトリが見つかりません" if narou_root == nil
  16. narou_dir = File.join(narou_root, ".narou")
  17.  
  18. author, title = %w(author title).map {|k|
  19. Helper.replace_filename_special_chars(data[k], true)
  20. }
  21. novel_root = Downloader.get_novel_data_dir_by_target novel_id
  22.  
  23. novel_size = data['general_all_no']
  24. if ARGV.size < 2
  25. glob_pattern = File.join(novel_root, "raw", "*.txt")
  26. sum_text = Dir.glob(glob_pattern).inject(0){|sum, x| sum += File.stat(x).size }
  27. sum_text_mb = sum_text / 1024.0 / 1024
  28. puts "#{'%4d'%novel_id} | #{'%4d'%novel_size} | #{'%6.3f'%sum_text_mb} | #{data['author']} | #{data['file_title']}"
  29. exit
  30. end
  31.  
  32. split_size = Integer(ARGV[1])
  33. author_title = "[#{author}] #{title}"
  34. full_author_title = File.join(novel_root, author_title)
  35.  
  36. # backup
  37. latest_convert_yaml = File.join(narou_dir, "latest_convert.yaml")
  38. latest_convert_yaml_backup = File.join(narou_dir, "latest_convert.yaml.backup")
  39. converter_rb = File.join(novel_root, "converter.rb")
  40. chosalog_txt = File.join(novel_root, "調査ログ.txt")
  41. [latest_convert_yaml, latest_convert_yaml_backup, converter_rb, chosalog_txt].each {|target|
  42. FileUtils.cp(target, target + ".orig") unless File.exists?(target + ".orig")
  43. }
  44. exts = %w(.txt .epub .mobi .kepub.epub .zip)
  45. exts.each {|ext|
  46. full_author_title_ext = full_author_title + ext
  47. unless File.exists?(full_author_title_ext + ".orig")
  48. FileUtils.mv(full_author_title_ext, full_author_title_ext + ".orig") if File.exists?(full_author_title_ext)
  49. end
  50. }
  51.  
  52. # convert
  53. (1..novel_size).each_slice(split_size).each_with_index {|x, i|
  54. File.open(converter_rb, "wb") {|f|
  55. s = <<-"EOS"
  56. converter \"#{data['file_title']}\" do
  57. def before(io, text_type) super; @subtitles.select!{|t| #{x}.include?(t['index'].to_i)}; io; end
  58. def after(io, text_type) super; io; end
  59. end
  60. EOS
  61. f.write(s)
  62. }
  63. system("narou c #{novel_id}")
  64.  
  65. exts.each {|ext|
  66. full_author_title_ext = full_author_title + ext
  67. if File.exists?(full_author_title_ext)
  68. FileUtils.mv(full_author_title_ext,
  69. "#{author_title}_#{'%04d'%x[0]}-#{'%04d'%x[-1]}#{ext}")
  70. end
  71. }
  72. }
  73.  
  74. # recovery
  75. [latest_convert_yaml, latest_convert_yaml_backup, converter_rb, chosalog_txt].each {|target|
  76. FileUtils.mv(target + ".orig", target) if File.exists?(target + ".orig")
  77. }
  78. exts.each {|ext|
  79. full_author_title_ext = full_author_title + ext
  80. FileUtils.mv(full_author_title_ext + ".orig", full_author_title_ext) if File.exists?(full_author_title_ext + ".orig")
  81. }
  82.  
  83.  
Runtime error #stdin #stdout #stderr 0s 28984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog.rb:2:in `require': cannot load such file -- narou (LoadError)
	from prog.rb:2:in `<main>'