Merge branch 'master' of http://10.200.0.3/GIT/videosite
[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 LWP::Simple qw(!get);
12 use XML::Simple;
13 use HTML::TokeParser;
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'} = 'mncast';
23     $self->{'PATTERNS'} = ['(http://www\.mncast\.com/\?(\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;
38     my $t;
39     my @accum;
40     my $vid;
41
42     $url =~ m|$pattern|;
43     $url = $1;
44
45     $metadata->{'URL'} = $url;
46     $metadata->{'ID'} = $2;
47     $metadata->{'TYPE'} = 'video';
48     $metadata->{'SOURCE'} = $self->{'NAME'};
49     $metadata->{'TITLE'} = undef;
50     $metadata->{'DLURL'} = undef;
51
52     # First, get a webpage containing the video ID
53     unless(defined($content = LWP::Simple::get(sprintf('http://www.mncast.com/player/index.asp?mnum=%s', $2)))) {
54         $self->error('Could not download player page');
55         return undef;
56     }
57
58     $p = HTML::TokeParser->new(\$content);
59
60     while ($t = $p->get_tag('script')) {
61         if ($p->get_text() =~ m|strMID = \x22([^\x22]+)\x22|s) {
62             $vid = $1;
63             last;
64         }
65     }
66
67     # Get the XML file containing the video metadata
68     unless(defined($content = LWP::Simple::get(sprintf('http://www.mncast.com/_MovieInfo_/_MovieInfoXML_Tag_v2.asp?movieID=%s&loginPNum=-1&player=0', $vid)))) {
69         $self->error('Could not download XML metadata');
70         return undef;
71     }
72
73     # There is no XML header in the data, which makes XML::Simple unhappy
74     $content = '<?xml version="1.0" encoding="UTF-8"?>' . $content;
75     $p = XML::Simple->new();
76
77     unless(defined($t = $p->XMLin($content, KeepRoot => 1))) {
78         $self->error('Could not parse XML metadata');
79         return undef;
80     }
81
82     $metadata->{'DLURL'} = sprintf('http://%s.flv', $t->{'moviedata'}->{'url'});
83     $metadata->{'TITLE'} = $t->{'moviedata'}->{'title'};
84
85     unless(defined($metadata->{'DLURL'}) && defined($metadata->{'TITLE'})) {
86         $self->error('Could not extract download URL and title');
87         return undef;
88     }
89
90     return $metadata;
91 }
92
93 1;