1 # -*- coding: ascii -*-
2 ###########################################################################
3 # clive, command line video extraction utility.
4 # Copyright 2007, 2008, 2009 Toni Gundogdu.
6 # This file is part of clive.
8 # clive is free software: you can redistribute it and/or modify it under
9 # the terms of the GNU General Public License as published by the Free
10 # Software Foundation, either version 3 of the License, or (at your option)
13 # clive is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 # You should have received a copy of the GNU General Public License along
19 # with this program. If not, see <http://www.gnu.org/licenses/>.
20 ###########################################################################
28 use File
::Basename
qw(basename);
29 use File
::Spec
::Functions
;
31 use Encode
qw(decode_utf8);
43 content_type
=> undef,
47 video_format
=> undef,
48 base_filename
=> undef,
50 initial_length
=> undef,
52 nothing_todo
=> undef,
55 _permitted
=> \
%fields,
58 return bless( $self, $class );
64 my ( $content, $title ) = @_;
66 require HTML
::TokeParser
;
67 my $p = HTML
::TokeParser
->new($content);
69 $self->{page_title
} = $p->get_trimmed_text;
73 $self->{page_title
} = $title;
76 return $self->{page_title
};
82 "file: %s %.1fM [%s]\n",
83 $self->{base_filename
},
84 clive
::Util
::toMB
( $self->{file_length
} ),
87 clive
::Log
->instance->out($str);
95 my @fields = qw(base_filename file_length video_link);
98 $str .= sprintf( qq/"%s",/, $self->$_ ) foreach (@fields);
101 clive
::Log
->instance->out("$str\n");
104 sub formatOutputFilename
{
107 my $config = clive
::Config
->instance->config;
110 if ( !$config->{output_file
} ) {
112 # Apply character-class.
113 my $title = $self->{page_title
};
114 my $cclass = $config->{cclass
} || qr
|\w
|;
116 $title = join( '', $self->{page_title
} =~ /$cclass/g )
117 if ( !$config->{no_cclass
} );
119 # Format output filename.
120 $fname = $config->{filename_format
} || "%t.%s";
122 my $id = $self->{video_id
};
126 if ( !$title && $fname !~ /%i/ );
128 $fname =~ s/%t/$title/;
129 $fname =~ s/%s/$self->{file_suffix}/;
131 $fname =~ s/%h/$self->{video_host}/;
133 my $config = clive
::Config
->instance->config;
134 $fname = catfile
( $config->{save_dir
} || getcwd
, $fname );
138 for ( my $i = 1; $i < 9999; ++$i ) {
139 $self->{initial_length
} = clive
::Util
::fileExists
($fname);
141 if ( $self->{initial_length
} == 0 ) {
144 elsif ( $self->{initial_length
} == $self->{file_length
} ) {
145 $self->{nothing_todo
} = 1;
149 if ( $config->{continue} ) {
157 $self->{initial_length
}
158 = clive
::Util
->fileExists( $config->{output_file
} );
159 if ( $self->{initial_length
} == $self->{file_length
} ) {
160 $self->{nothing_todo
} = 1;
163 $fname = $config->{output_file
};
167 if ( !$config->{continue} ) {
168 $self->{initial_length
} = 0;
171 $self->{base_filename
} = basename
($fname);
172 $self->{filename
} = $fname;
175 sub fromCacheRecord
{
176 my ( $self, $record ) = @_;
178 # No need to keep order in sync with clive::Video::toCacheRecord
179 # or clive::Cache::_mapRecord -- just make sure each item gets
181 $self->{page_title
} = decode_utf8
( $$record{page_title
} );
182 $self->{page_link
} = $$record{page_link
};
183 $self->{video_id
} = $$record{video_id
};
184 $self->{video_link
} = $$record{video_link
};
185 $self->{video_host
} = $$record{video_host
};
186 $self->{video_format
} = $$record{video_format
};
187 $self->{file_length
} = $$record{file_length
};
188 $self->{file_suffix
} = $$record{file_suffix
};
189 $self->{content_type
} = $$record{content_type
};
190 $self->{time_stamp
} = $$record{time_stamp
};
192 _cleanupTitle
($self);
198 # Should really remove all '#' from the strings
199 # before storing them. Living on the edge.
200 $self->{page_title
} =~ tr
{#}//d;
201 my $title = decode_utf8
( $self->{page_title
} );
203 # Keep the order in sync with clive::Cache::_mapRecord.
206 . $self->{page_link
} . "#"
207 . $self->{video_id
} . "#"
208 . $self->{video_link
} . "#"
209 . $self->{video_host
} . "#"
210 . $self->{video_format
} . "#"
211 . $self->{file_length
} . "#"
212 . $self->{file_suffix
} . "#"
213 . $self->{content_type
} . "#"
214 . POSIX
::strftime
( "%F %T", localtime ) # time_stamp
221 my $title = $self->{page_title
};
223 $title =~ s/(youtube|liveleak.com|sevenload|dailymotion)//gi;
224 $title =~ s/(cctv.com|redtube|ehrensenf|clipfish|funny hub)//gi;
225 $title =~ s/video[s]//gi;
226 $title =~ s/^[-\s]+//;
229 $self->{page_title
} = $title;
234 my $type = ref($self)
235 or croak
("$self is not an object");
236 my $name = $AUTOLOAD;
238 unless ( exists( $self->{_permitted
}->{$name} ) ) {
239 croak
("cannot access `$name' field in class $type");
242 return $self->{$name} = shift;
245 return $self->{$name};
251 # Barefoot servants too.