Youtube: Simplify match pattern
[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
21     $self->{'NAME'} = 'mncast';
22     $self->{'PATTERNS'} = ['(http://www\.mncast\.com/\?(\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;
37     my $t;
38     my @accum;
39     my $vid;
40     my $ua = $self->ua();
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 = $self->simple_get(sprintf('http://www.mncast.com/player/index.asp?mnum=%s', $2), $ua))) {
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 = $self->simple_get(sprintf('http://www.mncast.com/_MovieInfo_/_MovieInfoXML_Tag_v2.asp?movieID=%s&loginPNum=-1&player=0', $vid), $ua))) {
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;