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