fix quoting in AsyncWgetFileGetter again
[videosite.git] / videosite / BlipTVGrabber.pm
1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for blip.tv
5
6 package videosite::BlipTVGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use HTML::TokeParser;
12 use XML::Simple;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'bliptv',
21         _SELFTESTURL => 'http://blip.tv/rebelliouspixelscom/buffy-vs-edward-twilight-remixed-2274024',
22         _SELFTESTTITLE => 'Buffy vs Edward (Twilight Remixed)',
23         PATTERNS => ['(http://(?:[-a-zA-Z0-9_.]+\.)*blip.tv/\S+/\S+)'],
24         @_,
25     );
26
27     return bless($self, $class);
28 }
29
30 sub _parse {
31     my $self = shift;
32     my $url = shift;
33     my $pattern = shift;
34     my $content;
35     my $metadata = {};
36     my $p;
37     my $tag;
38     my $xml = undef;
39     my $ua = $self->ua();
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'TYPE'} = 'video';
46     $metadata->{'SOURCE'} = $self->{"NAME"};
47     $metadata->{'ID'} = undef;
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     unless(defined($content = $self->simple_get($url, $ua))) {
52         $self->error('Could not download page');
53         return undef;
54     }
55
56     $p = HTML::TokeParser->new(\$content);
57     while ($tag = $p->get_tag('link')) {
58         if (($tag->[0] eq 'link') and
59             exists($tag->[1]->{'rel'}) and
60             ($tag->[1]->{'rel'} eq 'alternate') and
61             exists($tag->[1]->{'type'}) and
62             ($tag->[1]->{'type'} eq 'application/rss+xml')) {
63
64             $xml = $tag->[1]->{'href'};
65             $self->debug("Found RSS link: %s", $xml);
66         }
67     }
68
69     unless(defined($xml)) {
70         $self->error("Could not find RSS link");
71         return undef;
72     }
73
74     # Download the XML file containing the stream information
75     $ua->max_redirect(7);
76     unless(defined($content = $self->simple_get(sprintf('http://blip.tv%s', $xml), $ua))) {
77         $self->error('Could not download XML metadata');
78         return undef;
79     }
80
81     $xml = XML::Simple::XMLin($content, KeepRoot => 1);
82     $metadata->{'DLURL'} = $xml->{'rss'}->{'channel'}->{'item'}->{'enclosure'}->{'url'};
83     $metadata->{'TITLE'} = $xml->{'rss'}->{'channel'}->{'item'}->{'title'};
84     $metadata->{'ID'} = $xml->{'rss'}->{'channel'}->{'item'}->{'blip:posts_id'};
85
86     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
87         $self->error('Could not determine download URL');
88         return undef;
89     }
90
91     return $metadata;
92 }
93
94 1;