Class: Hackernews::ArticleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/hackernews/article_extractor.rb

Constant Summary collapse

COMMAND =
"trafilatura"
TIMEOUT =
30

Instance Method Summary collapse

Instance Method Details

#extract(url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hackernews/article_extractor.rb', line 11

def extract(url)
  url = url.to_s.strip
  raise ArgumentError, "story has no article URL" if url.empty?

  stdout, stderr, status = Timeout.timeout(TIMEOUT) do
    Open3.capture3(COMMAND, "--markdown", "--images", "--no-comments", "--no-tables", "-u", url)
  end

  unless status.success?
    message = stderr.strip.empty? ? stdout.strip : stderr.strip
    raise "trafilatura extraction failed: #{message}"
  end

  markdown = stdout.strip
  raise "trafilatura did not find readable article content" if markdown.empty?

  {url: url, markdown: markdown}
rescue Errno::ENOENT
  raise "trafilatura is required. Install it separately and ensure `trafilatura` is on PATH."
rescue Timeout::Error
  raise "article extraction timed out"
end