Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created November 19, 2012 21:46
Show Gist options
  • Save waffle2k/4114234 to your computer and use it in GitHub Desktop.
Save waffle2k/4114234 to your computer and use it in GitHub Desktop.
Get the URL of the latest package available, using a PCRE against a URL as input.
#!/usr/bin/perl
use strict;
#my $regex = 'http://slowpoke.internal.tucows.com/~pblair/perl/Tucows-OPS-Cloud-(\d+(?:\.\d+)).tar.gz';
my $WGET = `which wget` || die 'Must install either wget for this to work';
chomp $WGET;
die 'wget must be installed and in the PATH' if $WGET =~ /^\s*$/;
$WGET .= ' -O - 2>/dev/null';
@ARGV = map { /^http/ ? "$WGET $_ |" : $_ } @ARGV;
while ( <> ){
chomp;
s/#.*//g;
next if /^\s*$/;
my $regex = $_;
my $versions = {};
my @a = reverse( split( /\//, $regex ) );
my $r = shift( @a );
my $url = join( "/", reverse( @a ) );
open( FD, "$WGET -O - $url 2>/dev/null |" );
while( <FD> ){
chomp;
if( /a href="(.*?)"/ ){
my $link = $1;
if( $1 =~ /$r/ ){
$versions->{$1} = $link;
}
}
}
my @latest = sort keys %$versions;
print $url . "/" . $versions->{$latest[-1]} . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment