Skip to content

Instantly share code, notes, and snippets.

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 = ''
after = nil
posts = []
client =
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
# 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']
# 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}"
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.