Skip to content

Instantly share code, notes, and snippets.

@zivester
Last active October 22, 2015 06:32
Show Gist options
  • Select an option

  • Save zivester/a61eb14fb25bcf067c69 to your computer and use it in GitHub Desktop.

Select an option

Save zivester/a61eb14fb25bcf067c69 to your computer and use it in GitHub Desktop.
download-ubuntu-torrents.pl
#!/usr/bin/env perl
#
# Download all the torrents for a particular ubuntu release.
#
# Requires: wget
#
# Usage:
# perl download-ubuntu-torrents.pl /tmp/
#
use strict;
my $SLEEP = 300; # seconds to wait until the next retry
my $dest = $ARGV[0] || '.'; # destination directory to send torrents
# it defaults to the current working directory
my @versions = ( "15.10" ); # The version you want to download
my @archs = ( "amd64", "i386" ); # The architecture
my @types = ( "desktop", "server" ); # The release type
for my $version (@versions){
for my $arch (@archs){
for my $type (@types){
my @cmd = (
"wget",
"-P",
$dest,
"http://releases.ubuntu.com/$version/ubuntu-$version-$type-$arch.iso.torrent"
);
print "@cmd\n";
my $error;
do {
$error = system(@cmd)
and print "ERROR, going to retry in $SLEEP seconds\n";
} while ( $error && sleep $SLEEP );
}
}
}
@zivester
Copy link
Author

Too tired to finish, but a better rewrite in node.js use the official torrent.ubuntu.com:6699 mirror
TODO: Add polling/retries

#!/usr/bin/env node

var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
var cheerio = require('cheerio');
var fs = require('fs');
var path = require('path');

var SITE = 'http://torrent.ubuntu.com:6969';

var destination = process.argv[2] || '/tmp';
var versions = [ "15.04" ];          // The version you want to download
var archs = [ "amd64", "i386" ];     // The architecture
var names  = [ "ubuntu", "xubuntu" ] // The release
var types = [ "desktop", "server" ];  // The release type


request.getAsync(SITE).then(function(data){
    // console.log(data);
    var $page = cheerio.load(data.toString());
    var $table = $page('table');
    var $children = $table.children();
    // console.log($children.length);

    var releases = [];

    $children.filter(function(){
        var $this = cheerio(this);
        return $this.find('td > code').length;
    }).map(function(){
        var $this = cheerio(this);
        // console.log($this.html());
        var $link = $this.find('a');
        var href = $link.attr('href');
        var name = $link.text();
        releases.push({
            href : SITE + href,
            name : name         
        });
    });

    return releases;
})
.then(filter)
.then(download)
.catch(function(err){
    console.error(err.stack);
});

function filter(releases) {
    return releases.filter(function(obj){
        var is_match = false;
        versions.forEach(function(v){
            archs.forEach(function(a){
                names.forEach(function(n){
                    types.forEach(function(t){
                        var check = [ n, v, t, a ].join('-') + '.iso';
                        if (check === obj.name) {
                            console.log('MATCH', obj.name);
                            is_match = true;
                        }
                    });     
                });
            });
        });
        return is_match;
    });
}

function download(releases) {

    function _download(release) {
        var dest = path.resolve(destination + '/' + release.name + '.torrent');
        var file = fs.createWriteStream(dest);
        console.log('WRITING %s', dest);
        return request.get(release.href + '123').pipe(file);
    }

    return releases.map(function(release){
        return _download(release);
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment