Fix Youtube: Unknown error (http/404) [#1]
[clive.git] / lib / clive / Host / Youtube.pm
blob290d230eeef372cb3a7ab2c6b403063567c119c8
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009,2010 Toni Gundogdu.
7 # This file is part of clive.
9 # clive is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free
11 # Software Foundation, either version 3 of the License, or (at your option)
12 # any later version.
14 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <http://www.gnu.org/licenses/>.
21 ###########################################################################
22 package clive::Host::Youtube;
24 use warnings;
25 use strict;
27 sub new { return bless ({}, shift); }
29 sub parsePage {
30 my ($self, $content, $props) = @_;
32 $$props->video_host ("youtube");
34 my %re = (
35 id => qr|&video_id=(.*?)&|,
36 fmt_url_map => qr|fmt_url_map=(.*?)&|,
39 my $tmp;
40 if (clive::Util::matchRegExps (\%re, \$tmp, $content) == 0) {
42 my $best;
43 my %h;
45 require URI::Escape;
47 foreach (split /,/, URI::Escape::uri_unescape ($tmp->{fmt_url_map})) {
48 my ($id, $url) = split /\|/, $_;
49 $best = $url unless $best;
50 $h{$id} = $url;
53 my $url;
55 my $config = clive::Config->instance->config;
57 if ($config->{format} eq 'best') {
58 $url = $best;
60 else {
61 $url = toURL ($self, $config->{format}, \%h);
62 $url = toURL ($self, 'default', \%h) unless $url;
65 $$props->video_id ($tmp->{id});
66 $$props->video_link ($url);
68 return 0;
71 return 1;
74 sub toURL {
75 my ($self, $fmt, $h) = @_;
77 $fmt = 'flv_240p' if $fmt eq 'default';
78 $fmt = toFmt ($self, $fmt);
80 foreach (keys %{$h})
81 { return $$h{$_} if $_ eq $fmt; }
83 return undef;
86 sub toFmt {
87 my ($self, $id) = @_;
89 # http://en.wikipedia.org/wiki/YouTube#Quality_and_codecs
90 # $container_$maxwidth = '$fmt_id'
92 my %h = (
93 # flv
94 flv_240p => '5',
95 flv_360p => '34',
96 flv_480p => '35',
97 # mp4
98 mp4_360p => '18',
99 mp4_720p => '22',
100 mp4_1080p => '37',
101 mp4_3072p => '38',
102 # webm
103 webm_480p => '43',
104 webm_720p => '45',
105 # 3gp
106 '3gp_144p'=> '17',
108 # For backward-compatibility only.
109 mobile => '17',
110 sd_270p => "18",
111 sd_360p => "34",
112 hq_480p => "35",
113 hd_720p => "22",
114 hd_1080p => "37",
115 webm_480p => "43",
116 webm_720p => "45",
117 '3gp' => "17",
118 mp4 => "18",
119 hq => "35",
120 hd => "22",
123 foreach (keys %h)
124 { return $h{$_} if $id eq $_; }
126 return $id;
131 # And this is not our fate.