Skip to content

Instantly share code, notes, and snippets.

@nbashaw
Last active December 13, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nbashaw/4972965 to your computer and use it in GitHub Desktop.
Save nbashaw/4972965 to your computer and use it in GitHub Desktop.
require 'json'
require 'csv'
hnh = JSON.parse(File.read('hackersandhustlers2.json'))
names = []
# Make a list of all the unique names in hnh
hnh.each { |post| names << post['from']['name'] }
ppl = []
names = names.uniq!
# For each name, iterate over all posts and tally the number of likes
names.each do |name|
likes = 0
hnh.each do |post|
# If they made the post, add to their like count
if post['from']['name'] == name
likes += if post['likes'] then post['likes']['count'] else 0 end
end
# If they commented, add to their like count
if post['comments']['count'] > 0
begin
post['comments']['data'].each do |comment|
if comment['from']['name'] == name
likes += comment['likes'] ? comment['likes'] : 0
end
end
rescue
# Some posts with only 1 comment don't have any comment data
end
end
end
ppl << {name: name, likes: likes}
end
# Sort by likes
ppl = ppl.sort_by {|person| person[:likes]}
ppl = ppl.reverse
# Generate a CSV string
csv_string = CSV.generate do |csv|
ppl.each do |person|
if person[:likes] > 10
csv << [person[:name], person[:likes]]
end
end
end
# Write to a file
File.open('most_likes_3.csv', 'w') {|f| f.write(csv_string)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment