Comics

 view release on metacpan or  search on metacpan

lib/Comics/Fetcher/Base.pm  view on Meta::CPAN

directory.

=cut

sub spoolfile {
    my ( $self, $file ) = @_;
    ::spoolfile($file);
}

use Digest::MD5 qw(md5_base64);

use Image::Info qw(image_info);

=head2 urlabs($url, $path)

Returns the full URL for the given i<path>, possibly relative to I<url>.

=cut

sub urlabs {
    my ( $self, $url, $path ) = @_;
    if ( $path =~ m;^/; ) {
	if ( $path =~ m;^//; ) {
	    $path = "http:" . $path;
	}
	elsif ( $path =~ m;^/static\.nrc\.nl/; ) { # TODO
	    $path = "https:/" . $path;
	}
	else {
	    $url =~ s;(^\w+://.*?)/.*;$1;;
	    $path = $url . $path;
	}
    }
    elsif ( $path !~ m;^\w+://; ) {
	$path = $url . "/" . $path;
    }

    return $path;
}

=head2 save_image($image, $dataref)

Saves the contents of I<dataref> to the spooldir, using I<image> as
the name for the file.

See also: B<spoolfile>.

=cut

sub save_image {
    my ( $self, $image, $data ) = @_;
    my $f = $self->spoolfile($image);
    open( my $fd, ">:raw", $f );
    print $fd $$data;
    close($fd) or warn("$f: $!\n");
    ::debug("Wrote: $f");
}

=head2 save_html($html)

Generates and saves the HTML fragment for this comic to the spooldir,
using I<html> as the name for the file.

See also: B<spoolfile>.

=cut

sub save_html {
    my ( $self, $html, $data ) = @_;
    my $f = $self->spoolfile($html);
    open( my $fd, ">:utf8", $f );
    print $fd ( $data // $self->html );
    close($fd) or warn("$f: $!\n");
    ::debug("Wrote: $f");
}

=head2 load_html($html)

Loads the HTML fragment for this comic from the spooldir,

See also: B<spoolfile>.

=cut

sub load_html {
    my ( $self, $html ) = @_;
    my $f = $self->spoolfile($html);
    my $opts = { split => 0, fail => "soft" };
    my $data = loadlines( $f, $opts );
    if ( $opts->{error} || !$data ) {
	::debug("Cannot reuse $html: ", $opts->{error}, "\n");
	return;
    }
    ::debug("Read: $f");
    return $data;
}

1;



( run in 0.601 second using v1.01-cache-2.11-cpan-364913b4093 )