EBook-Gutenberg

 view release on metacpan or  search on metacpan

lib/EBook/Gutenberg.pm  view on Meta::CPAN

}

sub _dialog_read {

    my $self = shift;
    my $book = shift;

    $self->{ Dialog }->infobox(
        'Fetching text...', 0, 0
    );

    my $file = $self->{ TxtCache }{ $book->{ 'Text#' } } // do {

        my $p = eval {
            my $tmp = do {
                my ($fh, $fn) = tempfile;
                close $fh;
                $fn;
            };
            # TODO: Sleep less?
            sleep 5 unless $self->_get_ok;
            $self->_touch_get;
            gutenberg_get(
                $book->{ 'Text#' },
                { fmt => 'text', to => $tmp },
            );
        };

        if ($@ ne '') {
            $self->{ Dialog }->msgbox(
                "Failed to fetch ebook: $@", 0, 0
            );
            return 1;
        }

        $self->{ TxtCache }{ $book->{ 'Text#' } } = $p;

    };

    # dialog does have a textbox widget, but it doesn't display large text files
    # like Gutenberg's text ebooks correctly, so we'll just use a pager instead.
    $self->{ Dialog }->pager($file);

    return 1;

}

sub help {

    my $self = shift;
    my $exit = shift;

    print $HELP;

    exit $exit if defined $exit;

    return 1;

}

sub update {

    my $self = shift;

    my $catalog = EBook::Gutenberg::Catalog->new($self->{ Catalog });

    unless ($self->_get_ok) {
        die "Please wait at least 5 seconds before performing another " .
            "network operation with Project Gutenberg\n";
    }

    unless ($self->{ Quiet }) {
        say "Fetching Project Gutenberg catalog, please be patient";
    }

    $catalog->fetch;

    $self->_touch_get;

    unless ($self->{ Quiet }) {
        say "Updated $self->{ Catalog }";
    }

    return 1;

}

sub search {

    my $self = shift;

    unless (-f $self->{ Catalog }) {
        die "Could not find an existing Project Gutenberg catalog, please " .
            "run 'update' to fetch a catalog before running 'search'\n";
    }

    my %search = $self->_gen_search_params;

    unless (%search) {
        $self->help(1);
    }

    my @books = $self->_search(%search);

    if (@books == 0) {
        die "Could not find any ebooks matching the given parameters\n";
    } else {
        _print_list(@books);
    }

    return 1;

}

sub get {

    my $self = shift;

    unless (-f $self->{ Catalog }) {
        die "Could not find an existing Project Gutenberg catalog, please " .
            "run 'update' to fetch a catalog before running 'get'\n";



( run in 1.208 second using v1.01-cache-2.11-cpan-d7f47b0818f )