From 67f30a7e2600945d4b38a987d19869228e5fb859 Mon Sep 17 00:00:00 2001 From: Christian Garbs Date: Wed, 30 Sep 2015 21:49:17 +0200 Subject: [PATCH] refactor FileGetter: extract _download method - remove code duplication - allow easier implementation of asynchronous getters --- videosite/FileGetter.pm | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/videosite/FileGetter.pm b/videosite/FileGetter.pm index 48278dd..5e54935 100644 --- a/videosite/FileGetter.pm +++ b/videosite/FileGetter.pm @@ -31,8 +31,6 @@ sub get { my $video = shift; my $dlfile; my $dirname; - my $ua; - my $res; $dlfile = sprintf($self->_getval('FILEPATTERN'), $self->_encode($video->{'SOURCE'}), @@ -50,31 +48,39 @@ sub get { if (exists($video->{'CONNECTOR'})) { $self->selectconn($video->{'CONNECTOR'}); } - $ua = $self->ua(); - $self->debug('Going to download %s to %s', $video->{'DLURL'}, $dlfile); - $res = $ua->mirror($video->{'DLURL'}, $dlfile); - if (!$res->is_success()) { - $self->error('Could not download %s to %s (%s)', $video->{'DLURL'}, $dlfile, $res->code()); - return 0; - } + $self->_download( $video->{'DLURL'}, $dlfile, $video ) + or return 0; if (exists($video->{'DLURL_AUDIO'})) { - $dlfile = $dlfile . ".audio"; - $self->debug('Going to download %s to %s', $video->{'DLURL_AUDIO'}, $dlfile); - - $res = $ua->mirror($video->{'DLURL_AUDIO'}, $dlfile); - if (!$res->is_success()) { - $self->error('Could not download %s to %s (%s)', $video->{'DLURL_AUDIO'}, $dlfile, $res->code()); - return 0; - } + $self->_download( $video->{'DLURL_AUDIO'}, $dlfile . '.audio', $video ) + or return 0; } return 1; } +sub _download { + my $self = shift; + my $dlurl = shift; + my $dlfile = shift; + my $video = shift; + my $res; + + $self->debug('Going to download %s to %s', $dlurl, $dlfile); + + $res = $self->ua()->mirror($dlurl, $dlfile); + + if (!$res->is_success()) { + $self->error('Could not download %s to %s (%s)', $dlurl, $dlfile, $res->code()); + return 0; + } + + return 1; +} + sub _encode { my $self = shift; my $s = shift; -- 1.8.3.1