Change new() function in plugins to avoid having to call _prepare_parameters() in...
[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     return bless($self, $class);
27 }
28
29 sub _parse {
30     my $self = shift;
31     my $url = shift;
32     my $pattern = shift;
33     my $content;
34     my $metadata = {};
35     my $p;
36     my $tag;
37     my $xml = undef;
38     my $ua = $self->ua();
39
40     $url =~ m|$pattern|;
41     $url = $1;
42
43     $metadata->{'URL'} = $url;
44     $metadata->{'TYPE'} = 'video';
45     $metadata->{'SOURCE'} = $self->{"NAME"};
46     $metadata->{'ID'} = undef;
47     $metadata->{'TITLE'} = undef;
48     $metadata->{'DLURL'} = undef;
49
50     unless(defined($content = $self->simple_get($url, $ua))) {
51         $self->error('Could not download page');
52         return undef;
53     }
54
55     $p = HTML::TokeParser->new(\$content);
56     while ($tag = $p->get_tag('link')) {
57         if (($tag->[0] eq 'link') and
58             exists($tag->[1]->{'rel'}) and
59             ($tag->[1]->{'rel'} eq 'alternate') and
60             exists($tag->[1]->{'type'}) and
61             ($tag->[1]->{'type'} eq 'application/rss+xml')) {
62
63             $xml = $tag->[1]->{'href'};
64             $self->debug("Found RSS link: %s", $xml);
65         }
66     }
67
68     unless(defined($xml)) {
69         $self->error("Could not find RSS link");
70         return undef;
71     }
72
73     # Download the XML file containing the stream information
74     $ua->max_redirect(7);
75     unless(defined($content = $self->simple_get(sprintf('http://blip.tv%s', $xml), $ua))) {
76         $self->error('Could not download XML metadata');
77         return undef;
78     }
79
80     $xml = XML::Simple::XMLin($content, KeepRoot => 1);
81     $metadata->{'DLURL'} = $xml->{'rss'}->{'channel'}->{'item'}->{'enclosure'}->{'url'};
82     $metadata->{'TITLE'} = $xml->{'rss'}->{'channel'}->{'item'}->{'title'};
83     $metadata->{'ID'} = $xml->{'rss'}->{'channel'}->{'item'}->{'blip:posts_id'};
84
85     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
86         $self->error('Could not determine download URL');
87         return undef;
88     }
89
90     return $metadata;
91 }
92
93 1;