require 'json'
require 'yaml'
require 'curb'
require 'front_matter_parser'

stories = JSON.parse File.read 'stories.json'

stories.each do |year, storiesByYear|
	storiesByYear.each do |story|
		story.delete '_highlightResult'
		id = story['objectID']
		url = story['url']
		
		fn = "_stories/#{year}/#{id}.md"

		next if url.nil?

		# We don't want PDF for now
		if url.split(//).last(4).join === '.pdf'
			File.delete fn if File.exist? fn
		end


		if File.exists? fn
			parsed = FrontMatterParser::Parser.parse_file(fn)
			if parsed.front_matter.nil?
				content = "#{story.to_yaml}\n---\n#{parsed.content}"
				File.open(fn, "w") { |file| file.write content }
			end
		else
			puts "[DL] #{story['url']}"
		end
	end
end