Add man1/clive.1.pod, simplify bin/clive POD
[clive.git] / lib / clive / Config.pm
blobafd7aaf8d7a1d612d83afbf6d27b4fb6fd4e53a1
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 version 0.77 (); our $VERSION = version->declare ("2.2.19");
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 'license' => \&_printLicense,
58 'help|h', => \&_printHelp,
60 'cache_read|cache-read|cacheread|r',
61 'cache_dump|cache-dump|cachedump|d',
62 'cache_grep|cache-grep|cachegrep|g=s',
63 'cache_remove_record|cache-remove-record|cacheremoverecord|D',
64 'cache_ignore_case|cache_ignore-case|cacheignorecase|i',
65 'cache_dump_format|cache-dump-format|cachedumpformat=s',
66 'cache_clear|cache-clear|cacheclear',
67 'no_cache|no-cache|nocache',
68 'no_extract|no-extract|noextract|n',
69 'no_proxy|no-proxy|noproxy',
70 'filename_format|filename-format|filenameformat=s',
71 'emit_csv|emit-csv|emitcsv',
72 'stream_exec|stream-exec|streamexec=s',
73 'stream_pass|stream-pass|streampass|s',
74 'output_file|output-file|outputfile|O=s',
75 'limit_rate|limit-rate|limitrate=i',
76 'connect_timeout|connect-timeout|connecttimeout=i',
77 'connect_timeout_socks|connect-timeout-socks|connecttimeoutsocks=i',
78 'save_dir|save-dir|savedir=s',
79 'recall_file|recall-file|recallfile=s',
80 'cache_file|cache-file|cachefile=s',
81 'no_cclass|no-cclass|nocclass|C',
82 'stop_after|stop-after|stopafter=s',
83 'exec_run|exec-run|execrun|e',
84 'upgrade_config|upgrade-config|upgradeconfig' =>
85 \&clive::Compat::upgradeConfig,
86 'cookie_jar|cookie-jar|cookiejar=s',
87 'print_fname|print-fname|printfname',
88 ) or exit(CLIVE_OPTARG);
90 my $homedir = $ENV{HOME} || getcwd();
92 my $cachedir = $ENV{CLIVE_CACHE}
93 || catfile( $homedir, ".cache", "clive" );
95 eval { mkpath($cachedir) };
96 die "$cachedir: $@"
97 if ($@);
99 $config{recall_file} ||= catfile( $cachedir, "last" );
100 $config{cache_file} ||= catfile( $cachedir, "cache" );
102 $config{format} ||= 'default';
104 # Check format.
106 # Youtube: For backward compatibility only.
107 my @youtube_orig = qw (fmt17 fmt18 fmt22 fmt34 fmt35 fmt37 fmt43 fmt45);
109 my @youtube_old =
110 qw (mobile sd_270p sd_360p hq_480p hd_720p hd_1080p);
112 my @youtube_arcane = qw (mp4 hd hq 3gp);
114 # These are the most current ones, also listed with --hosts. Refer
115 # to clive/Host/Youtube.pm for details.
117 my @youtube_latest =
118 qw (flv_240p flv_360p flv_480p mp4_360p mp4_720p mp4_1080p
119 mp4_3072p webm_480p webm_720p 3gp_144p);
121 # Other:
123 my @spiegel = # vp6_388=default
124 qw(vp6_64 vp6_576 vp6_928 h264_1400 small iphone podcast);
126 my @golem = qw(high ipod); # medium=default
128 my @formats = (
129 qw(default best),
130 @youtube_orig, @youtube_old, @youtube_arcane, @youtube_latest,
131 @spiegel, @golem
134 #unless (@formats ~~ $config{format}) { # Perl 5.10.0+
135 unless ( grep( /^$config{format}$/, @formats ) ) {
136 clive::Log->instance->err( CLIVE_OPTARG,
137 "unsupported format `$config{format}'" );
138 exit(CLIVE_OPTARG);
141 my $log = clive::Log->instance;
142 my $str = "%s depends on --stream-exec which is undefined";
144 # Check streaming options.
146 if ( $config{stream} && !$config{stream_exec} ) {
147 $log->err( CLIVE_OPTARG, sprintf( $str, "--stream" ) );
148 exit(CLIVE_OPTARG);
151 if ( $config{stream_pass} && !$config{stream_exec} ) {
152 $log->err( CLIVE_OPTARG, sprintf( $str, "--stream-pass" ) );
153 exit(CLIVE_OPTARG);
156 # Check --stop-after.
157 if ( $config{stop_after} ) {
158 if ( $config{stop_after} !~ /M$/
159 && $config{stop_after} !~ /%$/ )
161 clive::Log->instance->err( CLIVE_OPTARG,
162 "--stop-after must be terminated by either '%' or 'M'" );
163 exit(CLIVE_OPTARG);
167 $self->{config} = \%config;
170 sub config {
171 my $self = shift;
172 return $self->{config};
175 sub _printLicense {
176 print
177 "Copyright (C) 2007,2008,2009,2010 Toni Gundogdu. License: GNU GPL version 3+
178 This is free software; see the source for copying conditions. There is NO
179 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
181 exit CLIVE_OK;
184 sub _printVersion {
185 printf "clive version %s with WWW::Curl version $WWW::Curl::VERSION\n"
186 . "os=%s, perl=%s, locale=%s\n",
187 $VERSION, $^O, ( sprintf "%vd", $^V ), $ENV{LANG} || "?";
188 exit CLIVE_OK;
191 sub _printHelp {
193 # Edit bin/clive for --help contents.
194 require Pod::Usage;
195 Pod::Usage::pod2usage( -exitstatus => CLIVE_OK, -verbose => 1 );
200 # There's too much confusion.