Merge branch 'devel'
authorRalf Ertzinger <ralf@skytale.net>
Sat, 29 Jun 2013 14:42:45 +0000 (16:42 +0200)
committerRalf Ertzinger <ralf@skytale.net>
Sat, 29 Jun 2013 14:42:45 +0000 (16:42 +0200)
libvideosite.pm
videosite/YouTubeGrabber.pm

index 7c77a3b..34191b8 100644 (file)
@@ -34,6 +34,8 @@ my $getter;
 my %builtin_config = ();
 my $builtin_config_path;
 my $builtin_config_default;
+my $config_cache = 1;
+my %config_cache = ();
 our $error;
 
 #
@@ -124,6 +126,10 @@ my $videosite_commands = {
     'nodebug' => sub {
         _cmd_nodebug(@_);
     },
+
+    'cache' => sub {
+        _cmd_cache(@_);
+    },
 };
 
 #
@@ -301,36 +307,53 @@ sub _load_modules($) {
 #
 sub _config_get {
     my $path = shift;
+    my $dotpath = join('.', @{$path});
     my $value;
 
-    $value = $remote_api->{config_get}->($path);
-    _debug("config: getting %s=%s", join('.', @{$path}), $value);
+    if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{value})) {
+        $value = $config_cache{$dotpath}->{value};
+    } else {
+        $value = $remote_api->{config_get}->($path);
+        $config_cache{$dotpath} = {value => $value, has => 1};
+
+    }
 
+    _debug("config: getting %s=%s", $dotpath, $value);
     return $value;
 }
 
 sub _config_set {
     my $path = shift;
+    my $dotpath = join('.', @{$path});
     my $value = shift;
 
-    _debug("config: setting %s=%s", join('.', @{$path}), $value);
+    _debug("config: setting %s=%s", $dotpath, $value);
+    $config_cache{$dotpath} = {value => $value, has => 1};
     return $remote_api->{config_set}->($path, $value);
 }
 
 sub _config_has {
     my $path = shift;
+    my $dotpath = join('.', @{$path});
     my $b;
 
-    $b = $remote_api->{config_has}->($path);
-    _debug("config: testing %s (%s)", join('.', @{$path}), $b?'true':'false');
+    if ($config_cache && exists($config_cache{$dotpath}) && exists($config_cache{$dotpath}->{has})) {
+        $b = $config_cache{$dotpath}->{has};
+    } else {
+        $b = $remote_api->{config_has}->($path);
+        $config_cache{$dotpath}->{has} = $b;
+    }
 
+    _debug("config: testing %s (%s)", $dotpath, $b?'true':'false');
     return $b;
 }
 
 sub _config_del {
     my $path = shift;
+    my $dotpath = join('.', @{$path});
 
-    _debug("config: removing %s", join('.', @{$path}));
+    _debug("config: removing %s", $dotpath);
+    delete($config_cache{$dotpath});
     $remote_api->{config_del}->($path);
 }
 
@@ -907,6 +930,31 @@ sub _cmd_nodebug {
     }
 }
 
+#
+# Display or clear the content of the config cache
+#
+sub _cmd_cache {
+    my $event = shift;
+    my $subcmd = shift;
+
+    $subcmd = 'list' unless defined($subcmd);
+    $subcmd = lc($subcmd);
+
+    if ($subcmd eq 'list') {
+        _io("Content of config cache:");
+        foreach (sort(keys(%config_cache))) {
+            if (exists($config_cache{$_}->{value})) {
+                _io("%s => %s", $_, $config_cache{$_}->{value});
+            } else {
+                _io("%s present", $_);
+            }
+        }
+    } elsif ($subcmd eq 'clear') {
+        _debug("Clearing config cache");
+        %config_cache = ();
+    }
+}
+
 
 #
 # Return the list of loaded grabbers.
@@ -1086,6 +1134,10 @@ sub register_api {
         $builtin_config_default = $a->{_config_default}->();
     }
 
+    if (exists($a->{_config_cache})) {
+        $config_cache = $a->{_config_cache}->();
+    }
+
     @outputstack = ({io => $remote_api->{'io'}, window => ""});
 
     return 1;
index 28f5007..423184c 100644 (file)
@@ -96,15 +96,28 @@ sub _parse {
     $self->debug("Matched id %s from pattern %s", $id, $pattern);
 
     $res = $self->_parse_by_video_info($url, $id);
-    if (defined($res) && ref($res)) {
-        return $res;
+    if (defined($res)) {
+         if (ref($res)) {
+            return $res;
+         } else {
+             $self->debug("_parse_by_video_info failed with status noretry");
+             return undef;
+         }
     } else {
+        $self->debug("_parse_by_video_info failed with status retry");
         $res = $self->_parse_by_scrape($url, $id);
     }
 
     return $res;
 }
 
+#
+# Try to get video information by using the API call.
+#
+# Returns hashref on success
+# Returns undef on retryable error (try to scrape the website)
+# Returns 0 on non-retryable error 
+#
 sub _parse_by_video_info {
     my $self = shift;
     my $url = shift;
@@ -144,11 +157,19 @@ sub _parse_by_video_info {
     # Decode content
     $content = $self->decode_querystring($content);
 
+    $self->debug("Decoded get_video_info: %s", Dumper($content));
+
     if ($content->{'status'} ne 'ok') {
         $self->debug("Non OK status code found: %s", $content->{'status'});
         return undef;
     }
 
+    # Check if this is live content
+    if (exists($content->{ps}) and ($content->{ps} eq 'live')) {
+        $self->error("Video URL seems to point to a live stream, cannot save this");
+        return 0;
+    }
+
     if (exists($content->{'fmt_url_map'})) {
         # Decode fmt_url_map
         $urls = $self->decode_hexurl($content->{'fmt_url_map'});