ruby reddit_comments.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby reddit_comments.rb相关的知识,希望对你有一定的参考价值。

# Dump comments user has made on reddit to a file
# Usage: ./reddit_comments.rb User [Filename]

#!/usr/bin/env ruby
User = ARGV[0]
OutputFile = ARGV[1] || "#{User}.json"

require "rubygems"
require "json"
require "open-uri"

def parse_page(url)
  puts url
  JSON.parse(open(url).read)["data"]["children"].map{|x| x["data"]}
end

BaseUrl = "http://reddit.com/user/#{User}/comments/.json"

@page = parse_page(BaseUrl)

@comments = @page 

while !@page.empty?
  @page = parse_page(BaseUrl + "?after=#{@comments.last["name"]}")
  @comments += @page
end

File.open(OutputFile, "w"){|o| o.puts JSON.pretty_generate(@comments)}

以上是关于ruby reddit_comments.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee