`
juliancg65
  • 浏览: 15897 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

对于RailsCasts的异想天开

阅读更多
随便写写 呵呵
主要是熟悉下nokogiri这个包

  #####################################################
  # Download RailsCasts Tool by jenas
  #
  #####################################################
  require 'rubygems'
  require 'open-uri'
  require 'nokogiri'
  
  time = Time.now
  threads = []
  urls = []
  exports = "http://feeds.feedburner.com/railscasts"
  filepath = "C:\\tmp\\" #本地文件保存地址
  data = open(exports){|f|f.read}
  @xml_doc  = Nokogiri::XML(data)
  @xml_doc.css("enclosure").each do |item|
    urls << item[:url]
  end
  
  for tmp in urls  
    threads << Thread.new(tmp.to_s.split("/")[4]) do |thread|
      begin
      output = open(filepath+"#{thread}.mov","wb")
      open(tmp.to_s) do |f| 
        output.write(f.read) 
        puts f.read.to_s.length
      end
      print thread, "-completed"
      rescue
         print "Error! with-" , thread
      end
    end
  end
  
  threads.each {|thr| thr.join}
  puts " 下载完成,共耗时:#{Time.now - time}秒"


如果机器上面有wget,可以用以下代码来替换。
imgurls.each do |url|
  exec = <<EndString
     cd #{filepath}
     wget -b -q #{url}
EndString
    system exec
  end

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics