public
Last active

  • Download Gist
igrigorik revised this gist . 1 changed file with 5 additions and 1 deletion. View gist @ 417fa06
ted-postrank.rb
6 
... 41 42 43 44               ... 41 42 43     44 45 46 47 48  
@@ -41,4 +41,8 @@
data.each do |a|
csv << [a['title'], a['url'], *columns.map{|c| a[c] || 0}]
end
-end
\ No newline at end of file
+end
+
+
+# Blog post: http://blog.postrank.com/2010/05/and-the-most-engaging-ted-talk-is/
+# Google spreadsheet data: https://spreadsheets0.google.com/ccc?key=tWri7T3f4Ex6-uVU8i9-FFQ&hl=en
\ No newline at end of file
igrigorik revised this gist . 1 changed file with 1 addition and 0 deletions. View gist @ 8ed07b3
ted-postrank.rb
1 
... 30 31 32   33 34 35 ... 30 31 32 33 34 35 36
@@ -30,6 +30,7 @@
puts "processed page #{page}"
end
+# output a CSV file with the results
FasterCSV.open("ted.csv", "w") do |csv|
columns = data.collect{|d| d.keys}.flatten.uniq.sort
columns.delete('title')
igrigorik created this gist . View gist @ 59cffb1
ted-postrank.rb
43 
...                                                                                         ... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  
@@ -0,0 +1,43 @@
+require 'rubygems'
+require 'nokogiri'
+require 'open-uri'
+require 'digest/md5'
+require 'fastercsv'
+require 'json'
+require 'curb'
+require 'pp'
+
+data = []
+(1..69).each do |page|
+
+ # fetch ted talks from each page
+ doc = Nokogiri.parse(open("http://www.ted.com/talks/list/page/#{page}").read)
+ talks = doc.search('dd a').inject({}) do |hash,a|
+ hash["http://www.ted.com#{a.attributes['href'].value}"] = a.attributes['title'].value
+ hash
+ end
+
+ # fetch postrank metrics data
+ metrics = Curl::Easy.http_post('http://api.postrank.com/v2/entry/metrics?appkey=TEDdemo',
+ talks.keys.map{|t| "url[]=#{Digest::MD5.hexdigest(t)}"}.join("&"))
+ metrics.perform
+ metrics = JSON.parse(metrics.body_str)
+
+ talks.keys.each do |url|
+ data.push({'title' => talks[url], 'url' => url}.merge(metrics[Digest::MD5.hexdigest(url)]))
+ end
+
+ puts "processed page #{page}"
+end
+
+FasterCSV.open("ted.csv", "w") do |csv|
+ columns = data.collect{|d| d.keys}.flatten.uniq.sort
+ columns.delete('title')
+ columns.delete('url')
+
+ csv << ["Title", "URL", *columns]
+
+ data.each do |a|
+ csv << [a['title'], a['url'], *columns.map{|c| a[c] || 0}]
+ end
+end
\ No newline at end of file
Something went wrong with that request. Please try again.