Bump Perl prerequisite to 5.10.1
[www-quvi.git] / examples / get_media.pl
blob06ee222c62bf45ba6aaa8fbff69be1c117933b4f
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;
32 use constant AGENT => 'Mozilla/5.0';
33 use constant URL => 'http://vimeo.com/26721145';
35 # Quvi options.
37 my $o = new WWW::Quvi::Options;
38 $o->{user_agent} = AGENT;
40 # Query media.
42 print STDERR "Querying media...\n";
44 my $q = new WWW::Quvi::Query;
45 $q->set_opts($o);
47 my $m = $q->parse(URL);
48 croak "error: $q->{errmsg}\n" unless $q->{ok};
50 # Save to file.
52 my $fn = "$m->{page_title}.$m->{file_suffix}";
53 print STDERR "Saving to $fn...\n";
55 my $ua = new LWP::UserAgent;
56 $ua->env_proxy;
57 $ua->agent(AGENT);
59 open my $fh, ">", "$fn" or croak "$?";
60 my $bytes_received = 0;
62 my $res =
63 $ua->request(HTTP::Request->new(GET => $m->{url}),
64 sub {
65 my ($chunk,$res) = @_;
66 $bytes_received += length($chunk);
67 printf STDERR "%d%% - ",
68 100*$bytes_received/$m->{content_length};
69 print STDERR "$bytes_received bytes received\r";
70 print $fh $chunk;
71 });
73 close $fh;
74 print "\n", $res->status_line, "\n";
76 # vim: set ts=2 sw=2 tw=72 expandtab: