Use API for output
[videosite.git] / videosite / MNCastGrabber.pm
1 # (c) 2008 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
3 #
4 # Grabber for mncast.com
5
6 package videosite::MNCastGrabber;
7
8 use videosite::GrabberBase;
9 @ISA = qw(videosite::GrabberBase);
10
11 use XML::Simple;
12 use HTML::TokeParser;
13 use Data::Dumper;
14
15 use strict;
16
17 sub new {
18     my $class = shift;
19     my $self = $class->SUPER::new(
20         NAME => 'mncast',
21         PATTERNS => ['(http://www\.mncast\.com/\?(\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;
34     my $t;
35     my @accum;
36     my $vid;
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     # First, get a webpage containing the video ID
50     unless(defined($content = $self->simple_get(sprintf('http://www.mncast.com/player/index.asp?mnum=%s', $2), $ua))) {
51         $self->error('Could not download player page');
52         return undef;
53     }
54
55     $p = HTML::TokeParser->new(\$content);
56
57     while ($t = $p->get_tag('script')) {
58         if ($p->get_text() =~ m|strMID = \x22([^\x22]+)\x22|s) {
59             $vid = $1;
60             last;
61         }
62     }
63
64     # Get the XML file containing the video metadata
65     unless(defined($content = $self->simple_get(sprintf('http://www.mncast.com/_MovieInfo_/_MovieInfoXML_Tag_v2.asp?movieID=%s&loginPNum=-1&player=0', $vid), $ua))) {
66         $self->error('Could not download XML metadata');
67         return undef;
68     }
69
70     # There is no XML header in the data, which makes XML::Simple unhappy
71     $content = '<?xml version="1.0" encoding="UTF-8"?>' . $content;
72     $p = XML::Simple->new();
73
74     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
75         $self->error('Could not parse XML metadata');
76         return undef;
77     }
78
79     $metadata->{'DLURL'} = sprintf('http://%s.flv', $t->{'moviedata'}->{'url'});
80     $metadata->{'TITLE'} = $t->{'moviedata'}->{'title'};
81
82     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
83         $self->error('Could not extract download URL and title');
84         return undef;
85     }
86
87     return $metadata;
88 }
89
90 1;