App-Photobear
view release on metacpan or search on metacpan
lib/App/Photobear.pm view on Meta::CPAN
#ABSTRACT: Photobear API client
package App::Photobear;
use v5.18;
use warnings;
use Carp;
use HTTP::Tiny;
use Data::Dumper;
use JSON::PP;
# Define version
our $VERSION = '0.1.2';
# Define constants
our $PHOTOBEAR_URL = 'https://photobear.io/api/public/submit-photo';
our @MODES = split / /, "background_removal vectorization super_resolution compress";
our $TESTMODE = $ENV{"PHOTOBEAR_TEST"} || 0;
# Export MODES
use Exporter qw(import);
our @EXPORT_OK = qw(loadconfig saveconfig url_exists curl photobear url_type @MODES);
our $TEST_ANSWER = q({"status":"success","data":{"result_url":"https://res.cloudinary.com/dy4s1umzd/image/upload/e_vectorize:colors:20:detail:0.7:corners:20/v1688570702/svg_inp/aia14r/core-people.svg"}});
sub loadconfig {
my $filename = shift;
if (! -e "$filename") {
return {};
}
open my $fh, '<', $filename or Carp::croak "Can't open $filename: $!";
my $config = {};
while (my $line = readline($fh)) {
chomp $line;
next if $line =~ /^[#[]/;
my ($key, $value) = split /=/, $line;
$config->{"$key"} = $value;
}
return $config;
}
sub writeconfig {
my ($filename, $config) = @_;
open my $fh, '>', $filename or Carp::croak "Can't open $filename: $!";
say $fh '[photobear]';
foreach my $key (keys %$config) {
print $fh "$key=$config->{$key}\n";
}
}
sub url_exists {
my ($url) = @_;
# Create an HTTP::Tiny object
my $http = HTTP::Tiny->new;
# Send a HEAD request to check the URL
my $response = $http->head($url);
# If the response status is success (2xx), the URL exists
if ($response->{success}) {
return 1;
} elsif ($response->{status} == 599) {
# Try anothe method: SSLeay 1.49 or higher required
eval {
require LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->ssl_opts(verify_hostname => 0); # Disable SSL verification (optional)
my $response = $ua->get($url);
if ($response->is_success) {
return 1;
} else {
return 0;
}
};
if ($@) {
( run in 2.954 seconds using v1.01-cache-2.11-cpan-d8267643d1d )