Improve CLI sync and output

This commit is contained in:
Alexander Olofsson 2024-10-18 07:32:10 +02:00
parent aa0eaaa40a
commit 22b7208bdd
Signed by: ace
GPG key ID: D439C9470CB04C73
3 changed files with 29 additions and 2 deletions

View file

@ -9,13 +9,18 @@ class FicTracker::Cli < Thor
desc 'sync', 'Update all tracked stories' desc 'sync', 'Update all tracked stories'
method_option :render, type: :boolean, aliases: :r, desc: 'Render updated stories' method_option :render, type: :boolean, aliases: :r, desc: 'Render updated stories'
method_option :cron, type: :boolean, desc: 'Limit simultaenous actions, to reduce load when running as a cron job'
def sync def sync
setup! setup!
should_render = options[:render].nil? ? FicTracker::Config.dig(:cli, :render, default: false) : options[:render] should_render = options[:render].nil? ? FicTracker::Config.dig(:cli, :render, default: false) : options[:render]
prepare_render! if should_render prepare_render! if should_render
puts "Updating#{ should_render ? ' and rendering' : ''} stories." puts "Updating#{ should_render ? ' and rendering' : ''} stories."
FicTracker::Models::Story.needing_content_refresh.each do |story| to_update = FicTracker::Models::Story.needing_content_refresh
to_update = to_update.to_a.sample 10 if options[:cron]
to_update.each do |story|
next unless story.needs_content_refresh? || story.needs_metadata_refresh?
puts " Updating #{story} ..." puts " Updating #{story} ..."
before = story.etag before = story.etag
story.ensure_fully_loaded story.ensure_fully_loaded

View file

View file

@ -35,6 +35,28 @@ class FicTracker::Cli::Story < Thor
end end
end end
desc 'download STORY...', 'Download stories without adding them to the tracker'
def download(*stories)
setup!
stories.each do |story|
puts "Downloading #{story} ..."
bend = FicTracker::Backends.find_backend story
if bend.nil?
puts " Can't download, no available backends."
next
end
bend = FicTracker::Backends.get(bend)
slug = story
slug = bend.parse_slug(story) if bend.respond_to? :parse_slug
story = FicTracker::Models::Story.new(backend: bend, slug:)
story.ensure_fully_loaded
render_story! story
end
end
desc 'del STORY...', 'Remove stories from tracker' desc 'del STORY...', 'Remove stories from tracker'
def del(*stories) def del(*stories)
setup! setup!
@ -71,7 +93,7 @@ class FicTracker::Cli::Story < Thor
setup! setup!
info = [['Slug', 'Story', 'Chapters', 'Completed', 'Last Updated'], ['----','-----','--------','---------','------------']] info = [['Slug', 'Story', 'Chapters', 'Completed', 'Last Updated'], ['----','-----','--------','---------','------------']]
FicTracker::Models::Story.order_by(:name).each do |story| FicTracker::Models::Story.order_by(:name).each do |story|
info << [story.slug, story.name, story.chapters.size, story.completed?, (story.updated_at || story.published_at).strftime("%F")] info << ["#{story.backend_name}/#{story.slug}", story.name, story.chapters.size, story.completed?, (story.updated_at || story.published_at).strftime("%F")]
end end
print_table info print_table info
end end