#!/usr/bin/perl
# This program believes in the creative persons...
use strict;
use warnings;
use LWP::UserAgent;
use Term::ANSIColor;
use HTML::Entities;
use Getopt::Long;
my $script_version = '0.9.2';
my $main_url = '';
my $FILEHANDLE_REPORT;
my $FILEHANDLE_VIDEO;
my $temporary_filename = time();
my $parameter_show_help;
my $parameter_allow_proxy;
my $lwp_useragent = '';
my $lwp_response = '';
my $lwp_content = '';
my $video_code = '';
my $video_title = '';
my $video_swfconf = '';
my $video_final_url = '';
my $ffmpeg_conversion_choice = '';
my $ffmpeg_file_to_convert = '';
GetOptions(
'help' => \$parameter_show_help,
'proxy=s' => \$parameter_allow_proxy,
);
if ( defined($parameter_show_help) ) {
print color('green'), q {
Kludown }, color('reset'), $script_version, q {
Usage: perl }, $0, q { [--options]
--proxy: allow use of connections via proxy;
--help: show this help.
SEE THE PERLDOC (perldoc }, $0, q {) FOR MORE INFORMATIONS
};
exit;
}
print "\n", 'Insert the youtube link: ';
chomp( $main_url =
# Create the report file in txt for mark down all steps of the script.
# The name of the report file will be chosen according to the function time().
open $FILEHANDLE_REPORT, '>', 'report_' . $temporary_filename . '.txt'
or die "can't open the file handle: $!\n";
print {$FILEHANDLE_REPORT} "--KLUDOWN $script_version REPORT $temporary_filename--\n\n";
sub ffmpeg_conversion {
$ffmpeg_file_to_convert = shift;
system("ffmpeg -i $ffmpeg_file_to_convert $ffmpeg_file_to_convert.mp3");
print '90% - converting video in mp3', "\n";
-e $ffmpeg_file_to_convert . '.mp3'
? print "\n", color('green'), '--VIDEO SUCCESSFULLY CONVERTED--', color('reset'), "\n\n"
: print "\n", color('red'), '--FAILED TO CONVERT THE FILE HANDLE--', color('reset'), "\n\n";
close $FILEHANDLE_REPORT;
}
# Sub fuction, which downloads the video from the original youtube path
# and saves the content on a temporary file name, with a name chosen by
# the function time().
sub getvideo {
$video_final_url = shift;
$temporary_filename = shift;
$lwp_response = $lwp_useragent->get($video_final_url);
$lwp_content = $lwp_response->content;
open $FILEHANDLE_VIDEO, '>', $temporary_filename
or die "can't open the file handle, for save video: $!\n";
print {$FILEHANDLE_VIDEO} $lwp_content;
close $FILEHANDLE_VIDEO;
}
# Check if inserted a valid link of youtube, else the program prints a warning.
( $main_url =~ m{http://www.youtube.com/watch\?v=(.*)}i )
? $video_code = $1
: die "\n", 'usage: perl ', $0, ' {url}, example: http://www.youtube.com/watch?v=VIDEOCODE', "\n\n";
print {$FILEHANDLE_REPORT} "video url: \t$main_url\n";
print '10% - saving video url', "\n";
print {$FILEHANDLE_REPORT} "video code: \t$video_code\n";
print '20% - parsing the video ID', "\n";
$lwp_useragent = LWP::UserAgent->new;
$lwp_useragent->agent('Mozilla/5.0');
# Allow the usage of proxy if inserted a proxy option from the argv
# parameters and saves the proxy address in the report document.
if ( defined($parameter_allow_proxy) ) {
die "\n", color('red'), 'insert a correct proxy address', color('reset'), "\n\n"
unless $parameter_allow_proxy =~ m{\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b}i;
chomp($parameter_allow_proxy);
$lwp_useragent->proxy('http', 'http://' . $parameter_allow_proxy . ':8080');
print {$FILEHANDLE_REPORT} "proxy used: \t$parameter_allow_proxy\n";
}
$lwp_response = $lwp_useragent->get($main_url);
die "\nError connection: can't connect to remote host\n\n"
unless $lwp_response->is_success;
$lwp_content = $lwp_response->content;
$video_title = $1
if $lwp_content =~ m{}i;
# Check if there are any html entities in the URL and decodes if exists.
if ( $video_title =~ m{&}i ) {
for ( my $i = 0 ; $i <= length($video_title) ; $i++ ) {
decode_entities($video_title);
}
}
print '30% - decode html entities', "\n";
print {$FILEHANDLE_REPORT} "video title: \t", $video_title, "\n";
print '40% - parsing the video title', "\n";
$video_swfconf = $1
if $lwp_content =~ m{var swfConfig = {(.*?)}}i;
$video_final_url = $1
if $video_swfconf =~ m{\|(.*?)\,}i;
print {$FILEHANDLE_REPORT} "\n", 'video encoded final url:', "\n\n", $video_final_url, "\n";
print '50% - cathing the encoded final url', "\n";
$video_final_url =~ s/\\//g;
print {$FILEHANDLE_REPORT} "\n", 'video decoded url:', "\n\n", $video_final_url, "\n\n";
print '60% - adjusts the url', "\n";
print '--SAVING VIDEO ON LOCAL SYSTEM--', "\n";
print '(wait several minutes...)', "\n\n";
# Call the sub function to fetch the video by youtube path and save
# it in a temporary file name.
getvideo( $video_final_url, $temporary_filename );
print '70% - video downloaded', "\n";
if ( -e $temporary_filename ) {
print color('green'), '--SUCCESSFULLY SAVED--', color('reset'), "\n";
print "\n", 'whould you want to convert the video to mp3 with ffmpeg?', "\n";
print '(you need to have ffmpeg installed)', "\n", '[Y/n]? ';
chomp( $ffmpeg_conversion_choice =
print '80% - ask the conversion in mp3', "\n";
lc($ffmpeg_conversion_choice) eq 'y'
? ffmpeg_conversion($temporary_filename)
: print "\n", '--OPERATIONS FINISHED--', "\n\n";
}
else {
print color('red'), '--FAILED TO SAVE THE FILE HANDLE--', color('reset'), "\n\n";
}
__END__
=head1 NAME
Kludown
=head1 SYNOPSIS
perl kludown.pl --help
perl kludown.pl --proxy {proxy address}
=head1 EXAMPLES
perl kludown.pl
or
perl kludown.pl --proxy 127.0.0.1
# script output
Insert the youtube link: http://www.youtube.com/watch?v=RVHp1YFVp3w
=head1 DESCRIPTION
This is just a script which downloads from the most popular video
sharing web site, whatever video you want to. If youtube sets any limits
for your nation, you can simply divert this limits by inserting a proxy
address as an argv parameter. If you have installed the software ffmpeg
you can also convert your youtube video in mp3 format and listen the song
on every mp3 player you want.
=head1 SEE ALSO
Getopt::Long ~ http://search.cpan.org/~enrys/POD2-IT-Getopt-Long/lib/POD2/IT/Getopt/Long.pm
LWP::UserAgent ~ http://search.cpan.org/~gaas/libwww-perl-5.837/lib/LWP/UserAgent.pm
HTML::Entities ~ http://search.cpan.org/~gaas/HTML-Parser-3.68/lib/HTML/Entities.pm
See the FAQ about Term modules at http://perldoc.perl.org/perlfaq8.html
=head1 COPYRIGHT
Copyright (C) 2010 by sysxash
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301,
USA.
On Debian systems, the complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3.
=head1 AUTHOR
sysxash with his mind -
=head1 THANKS
Thanks to Plucky aka neov for many bug reports and suggestions, and
a special thanks to Stoke for giving me the main idea.
=cut
by sysxash
0 comments:
Post a Comment