Replace use of LWP::Simple in all grabbers by $self->simple_get()
[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
21     $self->{'NAME'} = 'yahoo';
22     $self->{'PATTERNS'} = ['(http://video\.yahoo\.com/watch/\d+/(\d+))'];
23
24     bless($self, $class);
25     $self->_prepare_parameters();
26
27     return $self;
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 = XML::Simple->new();
37     my $t;
38     my @accum;
39     my $ua = $self->ua();
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 = $self->simple_get(sprintf('http://cosmos.bcst.yahoo.com/up/yep/process/getPlaylistFOP.php?node_id=%s', $2), $ua))) {
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     $metadata->{'DLURL'} =~ s/\&amp;/\&/g;
67
68     # The XML does not contain the title of the video, for
69     # reasons possibly known to some jerk at yahoo.
70     # So we'll have to parse the actual HTML, too.
71     unless(defined($content = $self->simple_get($url, $ua))) {
72         $self->error('Could not download HTML');
73         return undef;
74     }
75     $p = HTML::Parser->new(api_version => 3);
76
77     $p->handler(start => \@accum, "tagname, attr");
78     $p->report_tags(qw(meta));
79     $p->utf8_mode(1);
80     $p->parse($content);
81
82     # Look for the title in the meta tags
83     foreach $t (@accum) {
84         if ('meta' eq $t->[0]) {
85             if (exists($t->[1]->{'name'}) and ('title' eq $t->[1]->{'name'})) {
86                 $metadata->{'TITLE'} = $t->[1]->{'content'};
87                 last;
88             }
89         }
90     }
91
92     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
93         $self->error('Could not extract download URL and title');
94         return undef;
95     }
96
97     return $metadata;
98 }
99
100 1;