Created
October 25, 2012 20:52
-
-
Save kumanan/3955314 to your computer and use it in GitHub Desktop.
Trending articles and topics
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
# SSH tunnel to trending service server and then run this script | |
# ssh -f -N -L 8000:localhost:8000 ec2-184-169-189-89.us-west-1.compute.amazonaws.com | |
require 'rest-client' | |
require 'json/pure' | |
require 'open-uri' | |
# Trending articles | |
puts "Trending articles\n\n" | |
ta_response = RestClient.get 'http://localhost:8000/api/trending.json/articles/weekly/' | |
ta = JSON.parse(ta_response) | |
trending_articles = ta['items'] | |
trending_articles.each do |article| | |
puts "\t" + article["name"] + " (" + article["visits"].to_s + ")" | |
end | |
# Trending topics | |
response = RestClient.get 'http://localhost:8000/api/trending.json/topics/weekly/' | |
r = JSON.parse(response) | |
topics = r['items'] | |
puts "\n\nTrending topics (" + topics.length.to_s + ")\n\n" | |
topics.each do |t| | |
topic = t['name'] | |
topic_encoded = URI::encode(topic).gsub("&", "%26") | |
# puts topic_encoded | |
articles_response = RestClient.get 'http://localhost:8000/api/trending.json/articles/weekly/?topic=' + topic_encoded | |
articles = JSON.parse(articles_response) | |
puts "\t" + topic + " => " + articles['totalCount'].to_s | |
end | |
# Trending articles by topic | |
puts "\n\nTrending articles by topic\n\n" | |
topics.each do |t| | |
topic = t['name'] | |
topic_encoded = URI::encode(topic).gsub("&", "%26") | |
# puts topic_encoded | |
articles_response = RestClient.get 'http://localhost:8000/api/trending.json/articles/weekly/?topic=' + topic_encoded | |
articles = JSON.parse(articles_response) | |
items = articles['items'] | |
puts "\n" + topic + " (" + articles['totalCount'].to_s + ")" | |
items.each do |item| | |
puts "\t" + item["name"] + " (" + item["visits"].to_s + ")" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment