fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite / BlipTVGrabber.pm
index 1b2349d..86084dd 100644 (file)
@@ -3,13 +3,12 @@
 #
 # Grabber for blip.tv
 
-package BlipTVGrabber;
+package videosite::BlipTVGrabber;
 
-use GrabberBase;
-@ISA = qw(GrabberBase);
+use videosite::GrabberBase;
+@ISA = qw(videosite::GrabberBase);
 
-use LWP::Simple qw(!get);
-use HTML::Parser;
+use HTML::TokeParser;
 use XML::Simple;
 use Data::Dumper;
 
@@ -17,15 +16,15 @@ use strict;
 
 sub new {
     my $class = shift;
-    my $self = $class->SUPER::new();
-
-    $self->{'NAME'} = 'bliptv';
-    $self->{'PATTERNS'} = ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/file/(\d+)(?:\?\S+)?)'];
-
-    bless($self, $class);
-    $self->_prepare_parameters();
-
-    return $self;
+    my $self = $class->SUPER::new(
+        NAME => 'bliptv',
+        _SELFTESTURL => 'http://blip.tv/rebelliouspixelscom/buffy-vs-edward-twilight-remixed-2274024',
+        _SELFTESTTITLE => 'Buffy vs Edward (Twilight Remixed)',
+        PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/\S+/\S+)'],
+        @_,
+    );
+
+    return bless($self, $class);
 }
 
 sub _parse {
@@ -34,54 +33,55 @@ 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();
 
     $url =~ m|$pattern|;
     $url = $1;
 
     $metadata->{'URL'} = $url;
-    $metadata->{'ID'} = $2;
     $metadata->{'TYPE'} = 'video';
     $metadata->{'SOURCE'} = $self->{"NAME"};
+    $metadata->{'ID'} = undef;
     $metadata->{'TITLE'} = undef;
     $metadata->{'DLURL'} = undef;
 
-    unless(defined($content = LWP::Simple::get(sprintf('http://blip.tv/file/%s', $2)))) {
+    unless(defined($content = $self->simple_get($url, $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);
+    $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')) {
 
-    # Look for the post id in the javascript code
-    foreach $e (@text) {
-        if ($e->[0] =~ m|player.setPostsId\((\d+)\)|s) {
-            $xml = $1;
+            $xml = $tag->[1]->{'href'};
+            $self->debug("Found RSS link: %s", $xml);
         }
     }
 
-    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
-    unless(defined($content = LWP::Simple::get(sprintf('http://blip.tv/rss/flash/%s', $xml)))) {
+    $ua->max_redirect(7);
+    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');