# (c) 2007 by Ralf Ertzinger # 2008-2009,2011-2012 by Christian Garbs # 2010 by Maximilian Rehkopf # # licensed under GNU GPL v2 # # A getter which will download the media to a local file storage # in the background using wget # package videosite::AsyncWgetFileGetter; use videosite::FileGetter; @ISA = qw(videosite::FileGetter); use strict; use File::Basename; use File::Temp qw(tempfile); use MIME::Base64; sub new { my $class = shift; my $self = $class->SUPER::new( NAME => 'asyncwgetfilegetter', @_, ); return bless($self, $class); } sub get { my $self = shift; my $video = shift; # 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 }; } 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)); $self->debug('Going to download %s to %s (%s)', $dlurl, $dlfile, $tmpfile); $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; }