- Add grabber for video.yahoo.com
[videosite.git] / videosite / YahooGrabber.pm
1 # (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for video.yahoo.com
5
6 package YahooGrabber;
7
8 use GrabberBase;
9 @ISA = qw(GrabberBase);
10
11 use LWP::Simple qw(!get);
12 use XML::Simple;
13 use HTML::Parser;
14 use Data::Dumper;
15
16 use strict;
17
18 sub new {
19     my $class = shift;
20     my $self = $class->SUPER::new();
21
22     $self->{'NAME'} = 'yahoo';
23     $self->{'PATTERNS'} = ['(http://video\.yahoo\.com/watch/\d+/(\d+))'];
24
25     bless($self, $class);
26     $self->_prepare_parameters();
27
28     return $self;
29 }
30
31 sub _parse {
32     my $self = shift;
33     my $url = shift;
34     my $pattern = shift;
35     my $content;
36     my $metadata = {};
37     my $p = XML::Simple->new();
38     my $t;
39     my @accum;
40
41     $url =~ m|$pattern|;
42     $url = $1;
43
44     $metadata->{'URL'} = $url;
45     $metadata->{'ID'} = $2;
46     $metadata->{'TYPE'} = 'video';
47     $metadata->{'SOURCE'} = $self->{'NAME'};
48     $metadata->{'TITLE'} = undef;
49     $metadata->{'DLURL'} = undef;
50
51     # Get the XML file containing the video metadata
52     unless(defined($content = LWP::Simple::get(sprintf('http://cosmos.bcst.yahoo.com/up/yep/process/getPlaylistFOP.php?node_id=%s', $2)))) {
53         $self->error('Could not download XML metadata');
54         return undef;
55     }
56
57     # There is no XML header in the data, which makes XML::Simple unhappy
58     $content = '<?xml version="1.0" encoding="UTF-8"?>' . $content;
59
60     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
61         $self->error('Could not parse XML metadata');
62         return undef;
63     }
64
65     $metadata->{'DLURL'} = $t->{'DATA'}->{'SEQUENCE-ITEM'}->{'STREAM'}->{'APP'} . $t->{'DATA'}->{'SEQUENCE-ITEM'}->{'STREAM'}->{'FULLPATH'};
66
67     # The XML does not contain the title of the video, for
68     # reasons possibly known to some jerk at yahoo.
69     # So we'll have to parse the actual HTML, too.
70     unless(defined($content = LWP::Simple::get($url))) {
71         $self->error('Could not download HTML');
72         return undef;
73     }
74     $p = HTML::Parser->new(api_version => 3);
75
76     $p->handler(start => \@accum, "tagname, attr");
77     $p->report_tags(qw(meta));
78     $p->utf8_mode(1);
79     $p->parse($content);
80
81     # Look for the title in the meta tags
82     foreach $t (@accum) {
83         if ('meta' eq $t->[0]) {
84             if (exists($t->[1]->{'name'}) and ('title' eq $t->[1]->{'name'})) {
85                 $metadata->{'TITLE'} = $t->[1]->{'content'};
86                 last;
87             }
88         }
89     }
90
91     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
92         $self->error('Could not extract download URL and title');
93         return undef;
94     }
95
96     return $metadata;
97 }
98
99 1;