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