BlipTV: Fix grabber
authorRalf Ertzinger <ralf@skytale.net>
Sat, 11 Feb 2012 17:17:47 +0000 (18:17 +0100)
committerRalf Ertzinger <ralf@skytale.net>
Sat, 11 Feb 2012 17:17:47 +0000 (18:17 +0100)
videosite/BlipTVGrabber.pm

index 86bbffd..b59a1f7 100644 (file)
@@ -8,9 +8,7 @@ package videosite::BlipTVGrabber;
 use videosite::GrabberBase;
 @ISA = qw(videosite::GrabberBase);
 
-use URI;
-use URI::QueryParam;
-use HTML::Parser;
+use HTML::TokeParser;
 use XML::Simple;
 use Data::Dumper;
 
@@ -21,8 +19,7 @@ sub new {
     my $self = $class->SUPER::new();
 
     $self->{'NAME'} = 'bliptv';
-    $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/file/(\d+)(?:\?\S+)?)',
-                           '(http://blip\.tv/play/(\w+)$)'];
+    $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/\S+/\S+)'];
 
     bless($self, $class);
     $self->_prepare_parameters();
@@ -36,10 +33,8 @@ sub _parse {
     my $pattern = shift;
     my $content;
     my $metadata = {};
-    my $p = HTML::Parser->new(api_version => 3);
-    my @accum;
-    my @text;
-    my $e;
+    my $p;
+    my $tag;
     my $xml = undef;
     my $ua = $self->ua();
 
@@ -47,76 +42,46 @@ sub _parse {
     $url = $1;
 
     $metadata->{'URL'} = $url;
-    $metadata->{'ID'} = $2;
     $metadata->{'TYPE'} = 'video';
     $metadata->{'SOURCE'} = $self->{"NAME"};
+    $metadata->{'ID'} = undef;
     $metadata->{'TITLE'} = undef;
     $metadata->{'DLURL'} = undef;
 
-    if ($pattern eq $self->{'PATTERNS'}->[0]) {
-        # blip.tv/file pattern
-        unless(defined($content = $self->simple_get(sprintf('http://blip.tv/file/%s', $2), $ua))) {
-            $self->error('Could not download page');
-            return undef;
-        }
-
-        $p->handler(start => \@accum, "tagname, attr");
-        $p->handler(text => \@text, "text");
-        $p->report_tags(qw(script));
-        $p->utf8_mode(1);
-        $p->parse($content);
-
-        # Look for the post id in the javascript code
-        foreach $e (@text) {
-            if ($e->[0] =~ m|player.setPostsId\((\d+)\)|s) {
-                $xml = $1;
-            }
-        }
-    } elsif ($pattern eq $self->{'PATTERNS'}->[1]) {
-        my $r;
-        my $u;
-
-        $ua->max_redirect(0);
-        $r = $ua->get(sprintf('http://blip.tv/play/%s', $2));
+    unless(defined($content = $self->simple_get($url, $ua))) {
+        $self->error('Could not download page');
+        return undef;
+    }
 
-        unless(defined($r)) {
-            $self->error('Could not download page');
-            return undef;
-        }
-        unless($r->is_redirect()) {
-            $self->error('Expected redirect, but got none');
-            return undef;
-        }
-        $u = URI->new($r->header('Location'));
-        $u = $u->query_param("file");
+    $p = HTML::TokeParser->new(\$content);
+    while ($tag = $p->get_tag('link')) {
+        if (($tag->[0] eq 'link') and
+            exists($tag->[1]->{'rel'}) and
+            ($tag->[1]->{'rel'} eq 'alternate') and
+            exists($tag->[1]->{'type'}) and
+            ($tag->[1]->{'type'} eq 'application/rss+xml')) {
 
-        unless(defined($u)) {
-            $self->error('Did not find file parameter in URL');
-            return undef;
+            $xml = $tag->[1]->{'href'};
+            $self->debug("Found RSS link: %s", $xml);
         }
-        $u =~ m|^http://blip.tv/rss/flash/(\d+)$|;
-        $xml = $1;
-
-    } else {
-        $self->error("Don't know how to handle that pattern yet");
-        return undef;
     }
 
-    unless(defined($xml)) { 
-        $self->error("Could not find post id");
+    unless(defined($xml)) {
+        $self->error("Could not find RSS link");
         return undef;
     }
 
     # Download the XML file containing the stream information
     $ua->max_redirect(7);
-    unless(defined($content = $self->simple_get(sprintf('http://blip.tv/rss/flash/%s', $xml), $ua))) {
+    unless(defined($content = $self->simple_get(sprintf('http://blip.tv%s', $xml), $ua))) {
         $self->error('Could not download XML metadata');
         return undef;
     }
 
     $xml = XML::Simple::XMLin($content, KeepRoot => 1);
     $metadata->{'DLURL'} = $xml->{'rss'}->{'channel'}->{'item'}->{'enclosure'}->{'url'};
-    $metadata->{'TITLE'} = $xml->{'rss'}->{'channel'}->{'item'}->{'media:title'};
+    $metadata->{'TITLE'} = $xml->{'rss'}->{'channel'}->{'item'}->{'title'};
+    $metadata->{'ID'} = $xml->{'rss'}->{'channel'}->{'item'}->{'blip:posts_id'};
 
     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
         $self->error('Could not determine download URL');