Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Created January 1, 2014 21:22
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 yorickpeterse/8211644 to your computer and use it in GitHub Desktop.
Save yorickpeterse/8211644 to your computer and use it in GitHub Desktop.
Counting the amount of title tags of a sub-reddit. Whipped together to see which ones on /r/r4r get the most comments.
require 'httpclient'
require 'json'
url = 'http://www.reddit.com/r/r4r.json'
after = nil
posts = []
client = HTTPClient.new
tags = {
'[M4F]' => 0,
'[M4M]' => 0,
'[F4F]' => 0,
'[F4M]' => 0,
'[R4R]' => 0,
'[R4F]' => 0,
'[R4M]' => 0
}
50.times do |n|
puts "Requesting page #{n + 1} with after=#{after.inspect}"
response = JSON(client.get(url, :query => {:after => after}).body)
after = response['data']['after']
posts |= response['data']['children'].map { |child| child['data'] }
# Stop when there are no more pages left to process.
break unless after
end
# Count the total amount of comments per tag found in the title.
posts.each do |post|
tags.each do |tag, _|
if post['title'] =~ /#{Regexp.escape(tag)}/
tags[tag] += post['num_comments']
end
end
end
# Sort the results in descending order based on the amount of comments.
tags.sort { |left, right| right[1] <=> left[1] }.each do |(tag, amount)|
puts "#{tag}: #{amount}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment