| 1 | # ABSTRACT: Download kernels on kernel.ubuntu.com/~kernel-ppa/mainline/. | ||
| 2 | package Uptake::Command::get; | ||
| 3 | use Uptake -command; | ||
| 4 | |||
| 5 | use autodie; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use feature qw(say); | ||
| 9 | |||
| 10 | use Carp; | ||
| 11 | use File::chdir; | ||
| 12 | use List::Util qw(none); | ||
| 13 | use Mojo::UserAgent; | ||
| 14 | |||
| 15 | $ENV{MOJO_MAX_MESSAGE_SIZE} = 0; | ||
| 16 | |||
| 17 | my $path = "$ENV{HOME}/.cache/kernels"; | ||
| 18 | my $ua = Mojo::UserAgent->new; | ||
| 19 | my $url = 'http://kernel.ubuntu.com/~kernel-ppa/mainline/'; | ||
| 20 | |||
| 21 | sub execute { | ||
| 22 | my ($self, $opt, $args) = @_; | ||
| 23 | |||
| 24 | $path = delete $opt->{dir} if $opt->dir; | ||
| 25 | mkdir $path unless -d $path; | ||
| 26 | |||
| 27 | @ARGV = @$args == 0 ? () : @$args; | ||
| 28 | |||
| 29 | local $CWD = $path; | ||
| 30 | while (<>) { | ||
| 31 | chomp; | ||
| 32 | mkdir $_ unless -d $_; | ||
| 33 | local $CWD = $_; | ||
| 34 | |||
| 35 | $self->_download({ | ||
| 36 | url => $url . $_ . '/', | ||
| 37 | no => $opt->{no}, | ||
| 38 | suffix => '[.]deb$', | ||
| 39 | }) | ||
| 40 | } | ||
| 41 | }; | ||
| 42 | |||
| 43 | sub opt_spec { | ||
| 44 | return ( | ||
| 45 | [ 'dir=s', 'download directory.' ], | ||
| 46 | [ 'no=s@', 'file name will exclude the specified word.' ], | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _download { | ||
| 51 | my ($self, $args) = @_; | ||
| 52 | exists $args->{url} && | ||
| 53 | exists $args->{suffix} or confess; | ||
| 54 | |||
| 55 | my @debs; | ||
| 56 | $ua->get( | ||
| 57 | $args->{url} => {DNT => 1})->res->dom('tr > td > a')->each(sub { | ||
| 58 | push @debs, $_->text if $_->text =~ /$args->{suffix}/; | ||
| 59 | }); | ||
| 60 | |||
| 61 | @debs = grep { | ||
| 62 | my $deb = $_; | ||
| 63 | none { $deb =~ /$_/ } @{$args->{no}}; | ||
| 64 | } @debs if exists $args->{no}; | ||
| 65 | |||
| 66 | for (@debs) { | ||
| 67 | say 'Downloading ' . $_; | ||
| 68 | $ua->get($args->{url} . $_) | ||
| 69 | ->res->content->asset->move_to($CWD . '/' . $_); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; |