Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ruby中通过HTTP下载延迟文件?_Ruby_Download_Delay_Open Uri - Fatal编程技术网

如何在Ruby中通过HTTP下载延迟文件?

如何在Ruby中通过HTTP下载延迟文件?,ruby,download,delay,open-uri,Ruby,Download,Delay,Open Uri,我使用以下Ruby函数通过HTTP下载各种文件: def http_download(uri, filename) bytes_total = nil begin uri.open( read_timeout: 500, content_length_proc: lambda { |content_length| bytes_total = content_length }, progress_proc: lambd

我使用以下Ruby函数通过HTTP下载各种文件:

def http_download(uri, filename)
  bytes_total = nil
  begin
    uri.open(
      read_timeout: 500,
      content_length_proc: lambda { |content_length|
        bytes_total = content_length
      },
      progress_proc: lambda { |bytes_transferred|
        if bytes_total
          print("\r#{bytes_transferred} of #{bytes_total} bytes")
        else
          print("\r#{bytes_transferred} bytes (total size unknown)")
        end
      }
    ) do |file|
      open filename, 'w' do |io|
        file.each_line do |line|
          io.write line
        end
      end
    end
  rescue => e
    puts e
  end
end
我还想从下载文件(,)。但是,存在某种延迟设置。当我在浏览器中单击下载链接时,需要一点时间才能显示下载窗口。我想文件需要在服务器上处理后才能送达

如何修改脚本以将延迟考虑在内


我正在运行Ruby 2.2.2。

以下是根据帖子和评论所做的修改:

require 'open-uri'

def http_download(uri, filename)
  bytes_total = nil
  index = 1
  begin
    open(
      uri,
      read_timeout: 500,
      content_length_proc: lambda { |content_length|
        bytes_total = content_length
      },
      progress_proc: lambda { |bytes_transferred|
        if bytes_total
          print("\r#{bytes_transferred} of #{bytes_total} bytes")
        else
          print("\r#{bytes_transferred} bytes (total size unknown)")
        end
      }
    ) do |io|
      # if "application/json" == io.content_type
      if io.is_a? StringIO
        raise " --> Failed, server is processing. Retry the request ##{index}"
      else # Tempfile
        puts "\n--> Succeed, writing to #{filename}"
        File.open(filename, 'w'){|wf| wf.write io.read}
      end
    end
  rescue => e
    puts e
    return if e.is_a? OpenURI::HTTPError # Processing error

    index += 1
    return if index > 10

    sleep index and retry
  end
end

嗨,你的Ruby版本是什么?我正在使用Ruby 2.2.2运行您的代码,出现了一个异常“private method‘open’called”。我已经打开了网站,你点击“下载数据集”按钮了吗?我没有遇到你提到的延迟,只是网站url附加了
.csv
.kml
.zip
和几个查询参数。是的,是最新版本。是的,链接隐藏在“下载数据集”按钮下。也许,我点击后,他们会在服务器上缓冲它。也许可以尝试另一个数据集。我发现,对于您第一次请求的数据集,它返回一个表示状态的JSON,例如,
{status:“processing”,processing\u time:0,count:0}
{status:“processing”,processing\u time:9.58,count:0}
。处理后,您将下载该文件。因此,我认为您可以添加一个判断,然后在处理部分重试。正确。你加法官是什么意思?太棒了!我注意到的一个小例子是,当下载是一个JSON文件时——我添加了上面的链接。太棒了!我在()上尝试了脚本。出于某种原因,它在那里不起作用,尽管它似乎是相同的延迟机制。你能明白原因吗?对不起,我已经完成了我的工作,我想我不能解决你所有的问题。明白。虽然在我看来是同一台服务器安装。这就是为什么奇怪的是数据没有返回。