Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.