# (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
-# 2008-2009 by Christian Garbs <mitch@cgarbs.de>
+# 2008-2009,2011-2012 by Christian Garbs <mitch@cgarbs.de>
# 2010 by Maximilian Rehkopf <otakon@gmx.net>
+#
# licensed under GNU GPL v2
#
# A getter which will download the media to a local file storage
sub new {
my $class = shift;
- my $self = $class->SUPER::new();
-
- $self->{'NAME'} = 'asyncwgetfilegetter';
+ my $self = $class->SUPER::new(
+ NAME => 'asyncwgetfilegetter',
+ @_,
+ );
- bless($self, $class);
- $self->_prepare_parameters();
-
- return $self;
+ return bless($self, $class);
}
sub get {
my $self = shift;
my $video = shift;
- my $dlfile;
- my $dirname;
- my $cookie = "";
-
- $dlfile = sprintf($self->_getval('FILEPATTERN'),
- $self->_encode($video->{'SOURCE'}),
- $self->_encode($video->{'ID'}),
- $self->_encode($video->{'TITLE'}),
- $self->_encode($video->{'DLURL'}),
- $self->_encode($video)->{'URL'});
-
- $dirname = dirname($dlfile);
- if ($self->_diskfree($dirname) < $self->_getval('MINFREE')) {
- $self->error("Not enough free space to download");
- return 0;
+
+ # saving the environment has nice race conditions... :(
+ my %saved_env;
+
+ if (exists($video->{'CONNECTOR'})) {
+ my $schemas = $video->{'CONNECTOR'}->{'schemas'};
+ foreach my $schemakey(keys(%{$schemas})) {
+ $self->debug("Setting %s_proxy to %s", $schemakey, $schemas->{$schemakey});
+ my $envkey = $schemakey.'_proxy';
+ $saved_env{ $envkey } = $ENV{ $envkey };
+ $ENV{ $envkey } = $schemas->{$schemakey};
+ }
+ }
+
+ # dispatch to super class method
+ $self->SUPER::get($video);
+
+ # restore environment
+ foreach my $envkey (keys %saved_env) {
+ $self->debug("Restoring environment: %s=%s", $envkey, $saved_env{ $envkey} );
+ $ENV{ $envkey } = $saved_env{ $envkey };
}
- $self->debug('Going to download %s to %s', $video->{'DLURL'}, $dlfile);
+ return 1;
+}
+
+sub _download {
+ my $self = shift;
+ my $dlurl = shift;
+ my $dlfile = shift;
+ my $video = shift;
+ my $res;
+ my $cookie = '';
+ my $useragent = qq{ --user-agent='Mozilla/4.0 (compatible; MSIE 5.0; Linux) Opera 5.0 [en]' };
+
+ $dlfile =~ s/'/'"'"'/g; # escape ' as '"'"' (end current 'string', new "string" containing a ', start new 'string')
+
+ my (undef, $tmpfile) = tempfile('videosite.tmp.XXXXXXXXXXXX', DIR => dirname($dlfile));
- my (undef, $tmpfile) = tempfile('videosite.tmp.XXXXXXXXXXXX', DIR => $dirname);
+ $self->debug('Going to download %s to %s (%s)', $dlurl, $dlfile, $tmpfile);
- $cookie = "--header=\"Cookie: $video->{'COOKIE'}\"" if (defined $video->{'COOKIE'});
- my $cmdline = "( wget -q -O\"$tmpfile\" $cookie \"$video->{'DLURL'}\" && mv \"$tmpfile\" \"$dlfile\" && chmod =rw \"$dlfile\" || rm -f \"$tmpfile\" ) &";
- $self->debug(encode_base64($cmdline));
+ $cookie = qq{ --header='Cookie: $video->{'COOKIE'}'} if (defined $video->{'COOKIE'});
+ my $cmdline = qq{ ( wget -q -O'$tmpfile' $useragent $cookie '$dlurl' && mv '$tmpfile' '$dlfile' && chmod =rw '$dlfile' && touch '$dlfile' || rm -f '$tmpfile' ) & };
+ $self->debug("Going to execute: %s", $cmdline);
system($cmdline);
return 1;