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