require 'open-uri'

require_relative '../../config/environment'

def save_the_children(parent, children)
  children.each do |c|
    next unless c["text"]
    comment = Comment.create(parent: parent, body: Nokogiri::HTML(c["text"]).text)
    save_the_children(comment, c["children"])
  end
end

task :hackernews, :id do |t, args|
  exit if args[:id].blank?

  url = "https://hn.algolia.com/api/v1/items/"+ args[:id]

  data = JSON.parse(open(url).read)

  article = Comment.create(
    parent: nil,
    body: data["title"],
    source: "https://news.ycombinator.com/item?id="+args[:id]
  )

  save_the_children(article, data["children"])
end

Implement hacker news collection task
Implement hacker news collection task

Motivation:
-so that the top-notch chat over at HN can be collected and investigated

Change Explanation
-implement simple :hackernews task to collect the comment for a given ID
-comment nesting is retained
