Created
June 15, 2014 12:43
-
-
Save anonymous/d31d53a5bf352738f9ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'nokogiri' | |
require 'open-uri' | |
Doc = Nokogiri::HTML(open "http://www.reddit.com/r/programming") | |
Entries = Doc.search("//div[@class='entry unvoted']") | |
.map{ |e| e.at_xpath(".//a[@class='comments may-blank']")} | |
.compact.map{|e| e.attributes['href'].value} | |
.map{|e| Nokogiri::HTML(open e)} | |
def get_comments page | |
xpath = "//div[@class='usertext-body may-blank-within']" | |
# First element is always the subreddit description | |
page.search(xpath).drop(1).map(&:text) | |
end | |
def count_haskells str | |
str.split(/[^a-zA-Z]/).delete_if{|w| not w.casecmp("haskell").zero?}.length | |
end | |
def generate_report | |
title_to_comments = Hash[Entries.map{|e| [e.title, get_comments(e)]}] | |
title_to_haskells = Hash[title_to_comments.map do |k, v| | |
hs = v.map{|comment| count_haskells comment}.inject(0, :+) | |
[k, hs] | |
end] | |
total_comments = title_to_comments.values.map(&:length).inject(0, :+) | |
total_haskells = title_to_haskells.values.inject(0, :+) | |
puts "Comments: #{total_comments}" | |
puts "Haskell mentions: #{total_haskells}" | |
title_to_haskells.each do |title, hs| | |
puts "#{title.chomp " : programming"}" | |
puts "\t #{hs} haskell mentions" | |
end | |
end | |
generate_report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment