Last active
December 23, 2015 08:29
-
-
Save jon-uw/6607742 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'net/http' | |
require 'rubygems' | |
require 'json' | |
require 'uri' | |
topicId='DOubanTA' | |
maxCount = 50 | |
# get topic count | |
#response = Net::HTTP.get_response('www.baidu.com', "/index.html"); | |
response = Net::HTTP.get_response('api.douban.com', "/v2/group/#{topicId}/topics"); | |
# puts response.body | |
topics = JSON.parse(response.body); | |
total = topics['total']; | |
puts "total topic count: #{total}" | |
topicHash = {} | |
topicList = [] | |
start = 0; | |
count = 100; | |
while start < total do | |
#url = URI.parse("http://api.douban.com/v2/group/#{topicId}/topics?start=#{start}&count=#{count}"); | |
#http = Net::HTTP.new(url.host, url.port) | |
#http.read_timeout = 50 | |
#http.open_timeout = 50 | |
#response = http.start() {|http| | |
# http.get(url.path) | |
#} | |
begin | |
response = Net::HTTP.get_response('api.douban.com', "/v2/group/#{topicId}/topics?start=#{start}&count=#{count}"); | |
start += count | |
rescue Exception => e | |
puts e | |
next | |
end | |
# response = Net::HTTP.get_response('api.douban.com', "/v2/group/#{topicId}/topics?start=#{start}&count=#{count}"); | |
puts "topics: #{start} - #{start + count} fetched." | |
topics = JSON.parse(response.body)['topics'] | |
topics.each do |topic| | |
id = topic['id'] | |
title = topic['title'] | |
created = topic['created'] | |
commentsCount = topic['comments_count'] | |
t = { :id => id, :title => title, :created => created, :commentsCount => commentsCount} | |
if topicList.size < maxCount | |
topicList.push(t) | |
else | |
topicList.sort! { |x, y| x[:commentsCount] <=> y[:commentsCount]} | |
if not topicList.include?(t) and commentsCount > topicList[0][:commentsCount] | |
topicList.delete_at(0) | |
topicList.insert(0, t) | |
end | |
end | |
end | |
end | |
topicList.sort! { |x, y| x[:commentsCount] <=> y[:commentsCount]} | |
topicList.reverse! | |
topicList.each do |v| | |
puts "#{v[:title]}\##{v[:id]}(#{v[:commentsCount]})" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment