Accept fmt34.
[clive.git] / lib / clive / Config.pm
bloba1ea1b1b62944834423380d8a9f5a63627572fb3
1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
5 # Copyright 2009 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::Config;
24 use warnings;
25 use strict;
27 use base 'Class::Singleton';
29 use Getopt::ArgvFile(
30 home => 1,
31 startupFilename => [qw(.cliverc .clive/config .config/clive/config)],
34 use Getopt::Long qw(:config bundling);
35 use File::Spec::Functions;
36 use File::Path qw(mkpath);
37 use Cwd qw(getcwd);
39 use clive::HostFactory;
40 use clive::Compat;
41 use clive::Error qw(CLIVE_OK CLIVE_OPTARG);
43 use constant VERSION => "2.2.6";
45 sub init {
46 my $self = shift;
48 my %config;
50 GetOptions(
51 \%config,
52 'debug', 'exec=s', 'cclass=s', 'stream=i',
53 'continue|c', 'recall|l', 'format|f=s', 'agent=s',
54 'quiet|q', 'proxy=s', 'stderr',
55 'hosts' => \&clive::HostFactory::dumpHosts,
56 'version|v' => \&_printVersion,
57 'help|h', => \&_printHelp,
59 'cache_read|cache-read|cacheread|r',
60 'cache_dump|cache-dump|cachedump|d',
61 'cache_grep|cache-grep|cachegrep|g=s',
62 'cache_remove_record|cache-remove-record|cacheremoverecord|D',
63 'cache_ignore_case|cache_ignore-case|cacheignorecase|i',
64 'cache_dump_format|cache-dump-format|cachedumpformat=s',
65 'cache_clear|cache-clear|cacheclear',
66 'no_cache|no-cache|nocache',
67 'no_extract|no-extract|noextract|n',
68 'no_proxy|no-proxy|noproxy',
69 'filename_format|filename-format|filenameformat=s',
70 'emit_csv|emit-csv|emitcsv',
71 'stream_exec|stream-exec|streamexec=s',
72 'stream_pass|stream-pass|streampass|s',
73 'output_file|output-file|outputfile|O=s',
74 'limit_rate|limit-rate|limitrate=i',
75 'connect_timeout|connect-timeout|connecttimeout=i',
76 'connect_timeout_socks|connect-timeout-socks|connecttimeoutsocks=i',
77 'save_dir|save-dir|savedir=s',
78 'recall_file|recall-file|recallfile=s',
79 'cache_file|cache-file|cachefile=s',
80 'no_cclass|no-cclass|nocclass|C',
81 'stop_after|stop-after|stopafter=s',
82 'exec_run|exec-run|execrun|e',
83 'upgrade_config|upgrade-config|upgradeconfig' =>
84 \&clive::Compat::upgradeConfig,
85 'cookie_jar|cookie-jar|cookiejar=s',
86 'print_fname|print-fname|printfname',
87 ) or exit(CLIVE_OPTARG);
89 my $homedir = $ENV{HOME} || getcwd();
91 my $cachedir = $ENV{CLIVE_CACHE}
92 || catfile( $homedir, ".cache", "clive" );
94 eval { mkpath($cachedir) };
95 die "$cachedir: $@"
96 if ($@);
98 $config{recall_file} ||= catfile( $cachedir, "last" );
99 $config{cache_file} ||= catfile( $cachedir, "cache" );
101 $config{format} ||= 'flv';
103 # Check format.
104 my @youtube = qw(fmt18 fmt34 fmt35 fmt22 fmt17 hq 3gp);
105 my @google = qw(mp4);
106 my @dmotion = qw(spark-mini vp6-hq vp6-hd vp6 h264);
107 my @vimeo = qw(hd);
108 my @spiegel # vp6_388=flv (regular)
109 = qw(vp6_64 vp6_576 vp6_928 h264_1400 small iphone podcast);
110 my @golem = qw(high ipod); # medium=flv (regular)
112 my @formats = (
113 qw(flv best), @youtube, @google, @dmotion, @vimeo, @spiegel, @golem
116 #unless (@formats ~~ $config{format}) { # Perl 5.10.0+
117 unless ( grep( /^$config{format}$/, @formats ) ) {
118 clive::Log->instance->err( CLIVE_OPTARG,
119 "unsupported format `$config{format}'" );
120 exit(CLIVE_OPTARG);
123 my $log = clive::Log->instance;
124 my $str = "%s depends on --stream-exec which is undefined";
126 # Check streaming options.
128 if ( $config{stream} && !$config{stream_exec} ) {
129 $log->err( CLIVE_OPTARG, sprintf( $str, "--stream" ) );
130 exit(CLIVE_OPTARG);
133 if ( $config{stream_pass} && !$config{stream_exec} ) {
134 $log->err( CLIVE_OPTARG, sprintf( $str, "--stream-pass" ) );
135 exit(CLIVE_OPTARG);
138 # Check --stop-after.
139 if ( $config{stop_after} ) {
140 if ( $config{stop_after} !~ /M$/
141 && $config{stop_after} !~ /%$/ )
143 clive::Log->instance->err( CLIVE_OPTARG,
144 "--stop-after must be terminated by either '%' or 'M'" );
145 exit(CLIVE_OPTARG);
149 $self->{config} = \%config;
152 sub config {
153 my $self = shift;
154 return $self->{config};
157 sub _printVersion {
158 my $locale = $ENV{LANG} || "undefined";
159 my $perl_v = sprintf( "%vd", $^V );
160 my $str
161 = sprintf( "clive version %s with WWW::Curl version "
162 . "$WWW::Curl::VERSION\n"
163 . "Copyright (C) 2007,2008,2009 Toni Gundogdu\n\n"
164 . "OS: %s Perl: %s Locale: %s\n\n"
165 . "License GPLv3+: GNU GPL version 3 or later\n"
166 . " <http://www.gnu.org/licenses/>\n\n"
167 . "This is free software: you are free to change and "
168 . "redistribute it.\n"
169 . "There is NO WARRANTY, to the extent permitted by law.\n\n"
170 . "Report bugs: <http://code.google.com/p/clive/issues/>\n",
171 VERSION, $^O, $perl_v, $locale );
172 print($str);
173 exit(CLIVE_OK);
176 sub _printHelp {
178 # Edit bin/clive for --help contents.
179 require Pod::Usage;
180 Pod::Usage::pod2usage( -exitstatus => CLIVE_OK, -verbose => 1 );
185 # There's too much confusion.