get_media.pl: Use "say"
[www-quvi.git] / examples / get_media.pl
blob01c318561f036b0c29f0a60810a1d3ae4f6c41e6
1 #!/usr/bin/env perl
3 # WWW::Quvi
4 # Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
22 # get_media.pl - Simple media downloader
23 # Note the use of custom user-agent string.
25 use warnings;
26 use strict;
28 use LWP::UserAgent;
29 use Carp qw(croak);
30 use WWW::Quvi;
31 use v5.10;
33 use constant AGENT => 'Mozilla/5.0';
34 use constant URL => 'http://vimeo.com/26721145';
36 # Quvi options.
38 my $o = new WWW::Quvi::Options;
39 $o->{user_agent} = AGENT;
41 # Query media.
43 say STDERR "Querying media...";
45 my $q = new WWW::Quvi::Query;
46 $q->set_opts($o);
48 my $m = $q->parse(URL);
49 croak "error: $q->{errmsg}\n" unless $q->{ok};
51 # Save to file.
53 my $fn = "$m->{page_title}.$m->{file_suffix}";
54 say STDERR "Saving to $fn...";
56 my $ua = new LWP::UserAgent;
57 $ua->env_proxy;
58 $ua->agent(AGENT);
60 open my $fh, ">", "$fn" or croak "$?";
61 my $bytes_received = 0;
63 my $res = $ua->request(
64 HTTP::Request->new(GET => $m->{url}),
65 sub {
66 my ($chunk, $res) = @_;
67 $bytes_received += length($chunk);
68 my $s = sprintf "%d%% - ",
69 100 * $bytes_received / $m->{content_length};
70 print STDERR "$s $bytes_received bytes received\r";
71 say $fh $chunk;
75 close $fh;
76 say "\n", $res->status_line;
78 # vim: set ts=2 sw=2 tw=72 expandtab: