1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # 2008-2009,2011-2012 by Christian Garbs <mitch@cgarbs.de>
3 # 2010 by Maximilian Rehkopf <otakon@gmx.net>
5 # licensed under GNU GPL v2
7 # A getter which will download the media to a local file storage
8 # in the background using wget
11 package videosite::AsyncWgetFileGetter;
13 use videosite::FileGetter;
14 @ISA = qw(videosite::FileGetter);
18 use File::Temp qw(tempfile);
23 my $self = $class->SUPER::new(
24 NAME => 'asyncwgetfilegetter',
28 return bless($self, $class);
35 # saving the environment has nice race conditions... :(
38 if (exists($video->{'CONNECTOR'})) {
39 my $schemas = $video->{'CONNECTOR'}->{'schemas'};
40 foreach my $schemakey(keys(%{$schemas})) {
41 $self->debug("Setting %s_proxy to %s", $schemakey, $schemas->{$schemakey});
42 my $envkey = $schemakey.'_proxy';
43 $saved_env{ $envkey } = $ENV{ $envkey };
44 $ENV{ $envkey } = $schemas->{$schemakey};
48 # dispatch to super class method
49 $self->SUPER::get($video);
52 foreach my $envkey (keys %saved_env) {
53 $self->debug("Restoring environment: %s=%s", $envkey, $saved_env{ $envkey} );
54 $ENV{ $envkey } = $saved_env{ $envkey };
67 my $useragent = qq{ --user-agent='Mozilla/4.0 (compatible; MSIE 5.0; Linux) Opera 5.0 [en]' };
69 $dlfile =~ s/'/'"'"'/g; # escape ' as '"'"' (end current 'string', new "string" containing a ', start new 'string')
71 my (undef, $tmpfile) = tempfile('videosite.tmp.XXXXXXXXXXXX', DIR => dirname($dlfile));
73 $self->debug('Going to download %s to %s (%s)', $dlurl, $dlfile, $tmpfile);
75 $cookie = qq{ --header='Cookie: $video->{'COOKIE'}'} if (defined $video->{'COOKIE'});
76 my $cmdline = qq{ ( wget -q -O'$tmpfile' $useragent $cookie '$dlurl' && mv '$tmpfile' '$dlfile' && chmod =rw '$dlfile' && touch '$dlfile' || rm -f '$tmpfile' ) & };
77 $self->debug("Going to execute: %s", $cmdline);