92 lines
2.4 KiB
Ruby
Executable file
92 lines
2.4 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
require 'fic_tracker'
|
|
require 'optparse'
|
|
require 'ostruct'
|
|
|
|
options = OpenStruct.new
|
|
optparse = OptParse.new do |opts|
|
|
opts.banner = 'Usage: fic_tracker [OPTIONS...]'
|
|
|
|
opts.on '-b', '--backend=BACKEND', 'The backend to use' do |backend|
|
|
options.backend = backend
|
|
end
|
|
|
|
opts.on '-s', '--story=STORY', 'The story to download' do |story|
|
|
options.story = story
|
|
end
|
|
|
|
opts.on '-c', '--chapter=CHAPTER', 'The chapter to download' do |chapter|
|
|
options.chapter = chapter
|
|
end
|
|
|
|
opts.on '-f', '--format=FORMAT', 'The format to download (epub, html, markdown)' do |format|
|
|
options.format = format.to_sym
|
|
end
|
|
|
|
opts.on '-o', '--output=FILE', 'The resulting file to save the download into' do |output|
|
|
options.output = output
|
|
end
|
|
|
|
opts.on '--only-changed', 'Only store the story if it has been changed since the last update' do
|
|
options.only_changed = true
|
|
end
|
|
|
|
opts.separator ''
|
|
|
|
|
|
opts.on '-C', '--config=FILE', 'Specify a configuration file to read' do |config|
|
|
options.config = config
|
|
end
|
|
|
|
opts.on '-h', '--help', 'Show this text' do
|
|
puts opts
|
|
exit
|
|
end
|
|
|
|
opts.on '-q', '--quiet', 'Run quietly' do
|
|
options.log_level = :error
|
|
end
|
|
|
|
opts.on '-v', '--verbose', 'Run verbosely, can be specified twice for debug output' do
|
|
if %i[info debug].include?(options.log_level)
|
|
options.log_level = :debug
|
|
else
|
|
options.log_level = :info
|
|
end
|
|
end
|
|
|
|
opts.on '-V', '--version', 'Prints the version and exits' do
|
|
puts FicTracker::VERSION
|
|
exit
|
|
end
|
|
end
|
|
optparse.parse!
|
|
|
|
unless options.backend && options.format && options.story
|
|
puts "Backend, format, and sotry must be provided\n"
|
|
puts optparse
|
|
exit 1
|
|
end
|
|
|
|
FicTracker.logger.level = options.log_level || :warn
|
|
FicTracker.configure!
|
|
|
|
backend = FicTracker::Backends.get(options.backend)
|
|
|
|
slug = options.story
|
|
slug = backend.parse_slug(slug) if backend.respond_to? :parse_slug
|
|
|
|
story = FicTracker::Models::Story.find(backend_name: backend.name, slug:) || FicTracker::Models::Story.new(backend:, slug:)
|
|
before = story.etag
|
|
story.ensure_fully_loaded
|
|
data = nil
|
|
|
|
options.output ||= "#{story.safe_name}.#{options.format}"
|
|
if !options.only_changed || story.etag != before
|
|
FicTracker.logger.info "Saving to #{options.output}"
|
|
File.open(options.output, 'w') { |f| FicTracker::Renderers.render(options.format, story, io: f) }
|
|
end
|
|
|
|
story.save_changes
|