App-get_flash_videos
view release on metacpan or search on metacpan
get_flash_videos view on Meta::CPAN
info "Already downloaded $file ($part_size bytes)";
next;
}
}
my $size = $downloader->$action($data, $file, $browser) || return 0;
info "\n" . ($remaining == 1 ? "Done. " : "")
. "Saved $size bytes to $downloader->{printable_filename}";
}
return 1;
}
sub read_conf {
for my $file("/etc/get_flash_videosrc", "$ENV{HOME}/.get_flash_videosrc") {
open my $fh, "<", $file or next;
while(<$fh>) {
s/\r?\n//;
next if /^\s*(#|$)/;
my($n, $v) = split /\s*=\s*/;
$v = 1 unless defined $v;
$opt{$n} = $v;
}
}
}
sub add_plugin {
my($plugin_url) = @_;
my $uri = URI->new($plugin_url);
unless(-d get_plugin_dir()) {
require File::Path;
File::Path::mkpath(get_plugin_dir())
or die "Unable to create plugin dir: $!";
}
my $filename = get_plugin_dir() . "/" . basename($uri->path);
if($filename !~ /\.pm$/) {
die "Plugins must have a file extension of '.pm'\n";
}
if(!$uri->scheme) {
# Local path given
require File::Copy;
File::Copy::copy($plugin_url => $filename)
|| die "Unable to copy plugin to '$filename': $!\n";
info "Plugin installed.";
return 0;
} else {
my $browser = FlashVideo::Mechanize->new;
return !install_plugin($browser, $plugin_url, $filename);
}
}
sub update {
my %update_types = (
'cpan-cpan' => [1, "cpan " . __PACKAGE__],
'cpan-cpanp' => [1, "cpanp i " . __PACKAGE__],
'cpan-cpanm' => [1, "cpanm " . __PACKAGE__],
'cpan-manual' => [0, "Manual install"],
);
# SCRIPT_NAME is some magic set by combine-perl or via MakeMaker
if($::SCRIPT_NAME) {
my $browser = FlashVideo::Mechanize->new;
$browser->get("http://get-flash-videos.googlecode.com/svn/wiki/Version.wiki");
if(!$browser->response->is_success) {
die "Unable to retrieve version data: " . $browser->response->status_line . "\n";
}
my $version = ($browser->content =~ /version: (\S+)/)[0];
my $base = ($browser->content =~ /from: (\S+)/)[0];
my $info = ($browser->content =~ /info: (\S+)/)[0];
my $url = $base . $::SCRIPT_NAME . "-" . $version;
die "Unable to parse version data" unless $version and $base;
# Split version on . and compare... (can't yet use version, that is only
# core since 5.10).
my @v = split /\./, $version;
my @V = split /\./, $VERSION;
my $newer = 0;
my $i = 0;
for(@v) {
$newer = 1 if !defined $V[$i] || $_ > $V[$i];
last if $V[$i] > $v[$i];
$i++;
}
if($newer) {
info "Newer version ($version) available";
debug "(Install type: $::INSTALL_TYPE)";
if($::INSTALL_TYPE =~ /^cpan-/) {
my $update_method = $update_types{$::INSTALL_TYPE};
if($update_method->[0]) {
info "This was installed via CPAN, you may upgrade by running:";
info $update_method->[1];
my $run_cpan = $opt{yes} || do {
info "Shall I run that for you? (Y/n)";
<STDIN> =~ /(?:^\s*$|y)/i;
};
if($run_cpan) {
system $update_method->[1];
}
} else {
info "Please visit http://code.google.com/p/get-flash-videos to upgrade";
}
} else {
update_script($browser, $url, $info);
}
} else {
print STDERR "You already have the latest version.\n";
}
} else {
info "Development version, not updated";
}
update_plugins();
return 0; # exit code
}
sub update_script {
my($browser, $url, $info) = @_;
info "Downloading new version...";
die "Cannot update -- unable to write to $0\n" unless -w $0;
my $new_file = $0 . ".new";
$browser->mirror($url, $new_file);
if($browser->response->is_success && -f $new_file) {
rename $0, "$0.old" or die "Unable to rename $0 to $0.old: $!";
rename $new_file, $0 or die "Unable to rename $new_file to $0: $!";
chmod 0755, $0;
info "New version installed as $0";
info "(previous version backed up to $0.old).";
info $info;
} else {
die "Download failed: " . $browser->response->status_line;
}
}
sub update_plugins {
my $browser = FlashVideo::Mechanize->new;
foreach my $plugin(get_installed_plugins()) {
debug "Seeing if there is an update for $plugin..";
my $file = get_plugin_dir() . "/$plugin";
require $file;
my $package = "FlashVideo::Site::" . ($plugin =~ /(.*)\.pm$/)[0];
if($package->can("update")) {
# Allow plugin to override generic updater
$package->update();
} else {
no strict 'refs';
my $downloaded = 0;
my $newer_found = 0;
foreach my $update_url (@{ "$package\::update_urls" }) {
$browser->head($update_url);
if (!$browser->response->is_success) {
# This shouldn't be fatal
debug "Couldn't retrieve $update_url for $plugin: " . $browser->response->status_line;
next;
}
# Compare the last modified time of the plugin to the time of the file on disk
my $file_mtime = stat($file)->mtime;
my $remote_plugin_mtime = $browser->response->last_modified;
if ($remote_plugin_mtime > $file_mtime) {
info "Newer version of plugin $plugin found at $update_url, trying to download and install";
$newer_found = 1;
if ($downloaded = install_plugin($browser, $update_url, $file)) {
last;
}
}
else {
debug "Plugin $plugin is already the lastest version.";
debug "(Remote: " . $browser->response->header("Last-Modified")
. "; Local: " . gmtime($file_mtime) . " GMT)";
}
}
if ($newer_found and !$downloaded) {
die "Couldn't install $plugin plugin";
}
}
}
}
# Upgrade a plugin or install a new one.
sub install_plugin {
my ($browser, $url, $file) = @_;
# So we can track newly installed plugins as well as updated ones
( run in 0.435 second using v1.01-cache-2.11-cpan-5511b514fd6 )