Skip to content

Instantly share code, notes, and snippets.

@jerodsanto
Created December 31, 2012 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerodsanto/4419913 to your computer and use it in GitHub Desktop.
Save jerodsanto/4419913 to your computer and use it in GitHub Desktop.
fetches the top content on Hacker News
require "open-uri"
require "cgi"
require "json"
SEARCH_API = URI "http://api.thriftdb.com/api.hnsearch.com/items/_search"
START_DATE = "2012-01-01"
END_DATE = "2012-12-31"
TOP_LIMIT = 10
def get(type, sortby)
params = {
"sortby" => sortby,
"limit" => TOP_LIMIT,
"filter[queries][]" => "create_ts:[#{START_DATE}T00:00:00Z TO #{END_DATE}T23:59:59Z]",
"filter[fields][type][]" => type
}
SEARCH_API.query = URI.encode_www_form params
return JSON.parse SEARCH_API.open.read
end
def deets(item)
by = item["username"]
on = Date.parse item["create_ts"]
url = item["url"]
id = item["id"]
points = item["points"]
comments = item["num_comments"]
content = item["title"] || item["text"]
"#{points} by #{by} on #{on}: #{content} : #{url} : #{id} : #{comments}"
end
def run(type, sortby)
response = get type, sortby
puts
puts "# #{type}s by #{sortby}"
puts
puts "## #{response['hits']} total #{type}s"
puts
response["results"].reverse.each do |result|
puts deets result["item"]
end
end
run "submission", "points desc"
run "submission", "num_comments desc"
run "comment", "points desc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment