如何向 WWW::Mechanize 添加进度条?

发布于 2024-08-15 07:04:16 字数 179 浏览 3 评论 0原文

我有以下代码:

$mech->get($someurl, ":content_file" => "$i.flv");

所以我获取 url 的内容并将其保存为 flv 文件。我想每秒打印出剩余的下载量。在 WWW::Mechanize 中有什么方法可以实现这一点吗?

I have the following code:

$mech->get($someurl, ":content_file" => "$i.flv");

So I'm getting the contents of a url and saving it as an flv file. I'd like to print out every second or so how much of the download is remaining. Is there any way to accomplish this in WWW::Mechanize?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-08-22 07:04:16

WWW::Mechanize 表示 get 方法是 LWP::UserAgent get 的“行为良好”重载。查看 LWP::UserAgent 的文档,您可以提供一个 content_cb 键,该键会在下载文件的每个块中调用:

$mech->get( $someurl, ":content_cb" => \&callback );
sub callback
{ 
  my( $data, $response, $proto ) = @_;
  # save $data to $i.flv
  # print download notification
 }

WWW::Mechanize says that the get method is a "well-behaved" overload of LWP::UserAgent get. Looking at the docs for LWP::UserAgent, you can provide a content_cb key which is called with each chunk of the downloaded file:

$mech->get( $someurl, ":content_cb" => \&callback );
sub callback
{ 
  my( $data, $response, $proto ) = @_;
  # save $data to $i.flv
  # print download notification
 }
情丝乱 2024-08-22 07:04:16

非常感谢彼得·科瓦奇的回答引导我找到正确的答案。结果比我预想的要复杂一些,所以我决定(恐怖)回答我自己的问题。

正如 Peter 所示,我可以像这样设置回调:

$m->get($u, ":content_cb" => \&callback);

但现在我无法使用 :content_file 值保存内容,因为我只能选择两者之一。回调函数传递了数据,我最终将其写入文件。

我还得到一个响应对象,其中包含弗里多指出的内容的总大小。因此,通过保留迄今为止收到的内容的运行总数并将其除以总内容,我可以找出已下载内容的百分比。这是完整的回调函数:

open (VID,">$i.flv") or die "$!";
$total = 0;
sub callback
{
    my( $data, $response, $proto ) = @_;
    print VID "$data"; # write data to file
    $total+= length($data);
    $size = $response->header('Content-Length');
    print floor(($total/$size)*100),"% downloaded\n"; # print percent downloaded
}

我希望对某人有所帮助。

Many thanks to Peter Kovacs' answer for leading me to the correct answer. It turned out to be a bit more elaborate than I'd expected though so I decided to (horror) answer my own question.

As Peter showed, I can set a callback like so:

$m->get($u, ":content_cb" => \&callback);

But now I can't save the content using the :content_file value, because I can only choose one of the two. The callback function gets passed the data, and I ended up writing that to a file instead.

I also get a response object which contains the total size of the content as friedo pointed out. So by keeping a running total of content received so far and dividing it by the total content I can find out what percent of the content has been downloaded. Here's the full callback function:

open (VID,">$i.flv") or die "$!";
$total = 0;
sub callback
{
    my( $data, $response, $proto ) = @_;
    print VID "$data"; # write data to file
    $total+= length($data);
    $size = $response->header('Content-Length');
    print floor(($total/$size)*100),"% downloaded\n"; # print percent downloaded
}

I hope that helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文