Skip to content

Instantly share code, notes, and snippets.

@psy-q
Last active May 11, 2016 09:19
Show Gist options
  • Select an option

  • Save psy-q/25a98db5334585e48726bdb38b7877d5 to your computer and use it in GitHub Desktop.

Select an option

Save psy-q/25a98db5334585e48726bdb38b7877d5 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'json'
require 'pry'
require 'open-uri'
filename = ARGV[0]
if !filename || filename == ''
puts 'Give the path to JSON comments file as downloaded from e.g. https://www.reddit.com/r/RandomActsOfGaming/comments/4ijyqj.json'
exit 1
end
# As in: https://www.reddit.com/r/patientgamers/comments/3bqoq8
comment_json = JSON.parse(File.read(filename))
comment_nodes = comment_json.last['data']['children']
comment_data = comment_nodes.collect do |node|
node['data']
end
comments = comment_data.map do |data|
{ author: data['author'],
comment: data['body'] }
end
puts "Found #{comments.count} authors, selecting one randomly:\n\n"
winner = comments.sample
puts "And the winner is: /u/#{winner[:author]}, saying '#{winner[:comment]}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment