refactor FileGetter: extract _download method
authorChristian Garbs <mitch@cgarbs.de>
Wed, 30 Sep 2015 19:49:17 +0000 (21:49 +0200)
committerChristian Garbs <mitch@cgarbs.de>
Tue, 6 Oct 2015 11:47:48 +0000 (13:47 +0200)
- remove code duplication
- allow easier implementation of asynchronous getters

videosite/FileGetter.pm

index 48278dd..5e54935 100644 (file)
@@ -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;