by Ilya Sher
A modern programming language for DevOps

A "normal" programming language with added facilities and syntax for DevOps tasks: running external programs and small scale data manipulation.

#!/usr/bin/env ngs

# HackerNews API documentation: https://github.com/HackerNews/API

BASE_API_URL = 'https://hacker-news.firebaseio.com/v0'

# Use HN_STATE_FILE environment variable if set, default to ~/.hn_state.json
STATE_FILE = ENV.get('HN_STATE_FILE', ENV.HOME / '.hn_state.json')

# Command line arguments are automatically parsed and passed to main().
# No command line arguments are required because all the parameters have defaults.
# Example: ./hn.ngs --once
# Example: ./hn.ngs --interval 10
F main(interval:Int=60, once:Bool=false) {

	# Try to read last seen ID. If reading fails, "seen" will be null
	seen = try fetch(STATE_FILE)
	while true {
		newstories = ``curl -s "${BASE_API_URL}/newstories.json"`` # Arr of Int
		if not(seen) {
			# If we start fresh, display last 10 stories
			newstories .= limit(10)
		} else {
			# Only keep unseen stories
			newstories .= filter(X > seen)
		}
		for i in reverse(newstories) {
			item = ``curl -s "${BASE_API_URL}/item/${i}.json"``
			# Sometimes "item" is null
			not(item) continues
			echo("-" * 80)
			echo("${Time(item.time)} [${item.by}] [${item.score}] ${item.title}")
			for k in %[url text] {
				# Display item's .url / .text if exist
				item.Box(k).each(echo)
			}
		}
		# Update "seen" if we have any stories in "newstories"
		seen = newstories.Box(0).get(seen)
		store(STATE_FILE, seen)
		once breaks
		$(sleep $interval)
	}
}
Information updated 03/28/24
Note: This entry has not yet been moderated.
View Comments