|
| 1 | +require_relative '../base_command' |
| 2 | + |
| 3 | +module Readwise |
| 4 | + class CLI |
| 5 | + module Document |
| 6 | + class CreateCommand < BaseCommand |
| 7 | + CATEGORIES = %w[article email rss highlight note pdf epub tweet video].freeze |
| 8 | + def banner |
| 9 | + "Usage: readwise document create [options]" |
| 10 | + end |
| 11 | + |
| 12 | + def description |
| 13 | + "Sends HTML content to Readwise Reader API" |
| 14 | + end |
| 15 | + |
| 16 | + def add_options(opts) |
| 17 | + opts.on("-f", "--html-file=FILE", "HTML file path") do |file| |
| 18 | + options[:file] = file |
| 19 | + end |
| 20 | + |
| 21 | + opts.on("--title=TITLE", "Document title") do |title| |
| 22 | + options[:title] = title |
| 23 | + end |
| 24 | + |
| 25 | + opts.on("--author=AUTHOR", "Document author") do |author| |
| 26 | + options[:author] = author |
| 27 | + end |
| 28 | + |
| 29 | + opts.on("-u", "--url=URL", "Source URL (defaults to https://example.com/<filename>)") do |url| |
| 30 | + options[:url] = url |
| 31 | + end |
| 32 | + |
| 33 | + opts.on("--summary=SUMMARY", "Document summary") do |summary| |
| 34 | + options[:summary] = summary |
| 35 | + end |
| 36 | + |
| 37 | + opts.on("--notes=NOTES", "Personal notes") do |notes| |
| 38 | + options[:notes] = notes |
| 39 | + end |
| 40 | + |
| 41 | + opts.on("--location=LOCATION", "Document location: new, later, archive, feed (default: new)") do |location| |
| 42 | + unless %w[new later archive feed].include?(location) |
| 43 | + puts "Error: Invalid location. Must be one of: new, later, archive, feed" |
| 44 | + exit 1 |
| 45 | + end |
| 46 | + options[:location] = location |
| 47 | + end |
| 48 | + |
| 49 | + opts.on("--category=CATEGORY", "Document category: #{CATEGORIES.join(', ')}") do |category| |
| 50 | + unless CATEGORIES.include?(category) |
| 51 | + puts "Error: Invalid category. Must be one of: #{CATEGORIES.join(', ')}" |
| 52 | + exit 1 |
| 53 | + end |
| 54 | + options[:category] = category |
| 55 | + end |
| 56 | + |
| 57 | + opts.on("--tags=TAGS", "Comma-separated list of tags") do |tags| |
| 58 | + options[:tags] = tags.split(',').map(&:strip) |
| 59 | + end |
| 60 | + |
| 61 | + opts.on("--image-url=URL", "Image URL") do |image_url| |
| 62 | + options[:image_url] = image_url |
| 63 | + end |
| 64 | + |
| 65 | + opts.on("--published-date=DATE", "Published date (ISO 8601 format)") do |date| |
| 66 | + options[:published_date] = date |
| 67 | + end |
| 68 | + |
| 69 | + opts.on("--[no-]clean-html", "Clean HTML (default: true)") do |clean| |
| 70 | + options[:should_clean_html] = clean |
| 71 | + end |
| 72 | + |
| 73 | + opts.on("--saved-using=SOURCE", "Saved using source (default: cli)") do |source| |
| 74 | + options[:saved_using] = source |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def validate_arguments(args) |
| 79 | + unless options[:file] || options[:url] |
| 80 | + puts "Error: File path or URL is required" |
| 81 | + show_help |
| 82 | + exit 1 |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def run(args) |
| 87 | + html_file = options[:file] |
| 88 | + html_content = read_file(html_file) if html_file |
| 89 | + |
| 90 | + document_params = build_document_params(html_content, html_file) |
| 91 | + |
| 92 | + handle_api_error do |
| 93 | + client = get_api_client |
| 94 | + document_create = Readwise::DocumentCreate.new(**document_params) |
| 95 | + document = client.create_document(document: document_create) |
| 96 | + |
| 97 | + puts "Document created successfully!" |
| 98 | + puts "ID: #{document.id}" |
| 99 | + puts "Title: #{document.title}" |
| 100 | + puts "Location: #{document.location}" |
| 101 | + puts "URL: #{document.url}" if document.url |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + private |
| 106 | + |
| 107 | + def build_document_params(html_content, html_file) |
| 108 | + document_params = { |
| 109 | + html: html_content, |
| 110 | + location: options[:location] || 'new', |
| 111 | + should_clean_html: options.key?(:should_clean_html) ? options[:should_clean_html] : true, |
| 112 | + saved_using: options[:saved_using] || 'cli', |
| 113 | + url: options[:url] || "https://example.com/#{File.basename(html_file)}" |
| 114 | + } |
| 115 | + |
| 116 | + [:title, :author, :summary, :notes, :category, :tags, :image_url, :published_date].each do |key| |
| 117 | + document_params[key] = options[key] if options[key] |
| 118 | + end |
| 119 | + |
| 120 | + document_params |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
| 126 | + |
| 127 | +# Register this command |
| 128 | +Readwise::CLI::CommandRegistry.register('document', 'create', Readwise::CLI::Document::CreateCommand) |
0 commit comments