| 1 | #ABSTRACT: Photobear API client | ||||||||||||||||||||||||||||
| 2 | package App::Photobear; | ||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use v5.18; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Define version | ||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.1.2'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Define constants | ||||||||||||||||||||||||||||
| 14 | 2 | our $PHOTOBEAR_URL = 'https://photobear.io/api/public/submit-photo'; | |||||||||||||||||||||||||||
| 15 | 2 | our @MODES = split / /, "background_removal vectorization super_resolution compress"; | |||||||||||||||||||||||||||
| 16 | 2 | ▶ | our $TESTMODE = $ENV{"PHOTOBEAR_TEST"} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{'PHOTOBEAR_TEST'} || 0
MC/DC100%1/1
$ENV{'PHOTOBEAR_TEST'} || 0
$ENV{'PHOTOBEAR_TEST'}
Truth table100%2/2
A
A= $ENV{'PHOTOBEAR_TEST'}
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Export MODES | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | our @EXPORT_OK = qw(loadconfig saveconfig url_exists curl photobear url_type @MODES); | |||||||||||||||||||||||||||
| 21 | 2 | 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"}}); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub loadconfig { | ||||||||||||||||||||||||||
|
Subroutine
loadconfig: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 2 | my $filename = shift; | |||||||||||||||||||||||||||
| 25 | 2 | ▶ | if (! -e "$filename") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 1 | return {}; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | open my $fh, '<', $filename or Carp::croak "Can't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | my $config = {}; | |||||||||||||||||||||||||||
| 30 | 1 | ▶ | while (my $line = readline($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 2 | chomp $line; | |||||||||||||||||||||||||||
| 32 | 2 | ▶ | next if $line =~ /^[#[]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 1 | my ($key, $value) = split /=/, $line; | |||||||||||||||||||||||||||
| 34 | 1 | $config->{"$key"} = $value; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 1 | return $config; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub writeconfig { | ||||||||||||||||||||||||||
|
Subroutine
writeconfig: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my ($filename, $config) = @_; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | open my $fh, '>', $filename or Carp::croak "Can't open $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | say $fh '[photobear]'; | |||||||||||||||||||||||||||
| 43 | 0 | foreach my $key (keys %$config) { | |||||||||||||||||||||||||||
| 44 | 0 | print $fh "$key=$config->{$key}\n"; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | sub url_exists { | ||||||||||||||||||||||||||
|
Subroutine
url_exists: called CC=8 SCAR=35.1
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 2 | my ($url) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Create an HTTP::Tiny object | ||||||||||||||||||||||||||||
| 52 | 2 | my $http = HTTP::Tiny->new; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Send a HEAD request to check the URL | ||||||||||||||||||||||||||||
| 55 | 2 | my $response = $http->head($url); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # If the response status is success (2xx), the URL exists | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | if ($response->{success}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 1 | return 1; | |||||||||||||||||||||||||||
| 60 | } elsif ($response->{status} == 599) { | ||||||||||||||||||||||||||||
| 61 | # Try anothe method: SSLeay 1.49 or higher required | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | eval { | |||||||||||||||||||||||||||
| 64 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 65 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 66 | 0 | $ua->ssl_opts(verify_hostname => 0); # Disable SSL verification (optional) | |||||||||||||||||||||||||||
| 67 | 0 | my $response = $ua->get($url); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | return 1; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | return 0; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | my $cmd = qq(curl --silent -L -I $url); | |||||||||||||||||||||||||||
| 77 | 0 | my @output = `$cmd`; | |||||||||||||||||||||||||||
| 78 | 0 | for my $line (@output) { | |||||||||||||||||||||||||||
| 79 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($line =~ /^HTTP/ and $line =~ /200/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$line =~ /^HTTP/u and $line =~ /200/u
MC/DC0%0/2
$line =~ /^HTTP/u and $line =~ /200/u
$line =~ /^HTTP/u
$line =~ /200/u
Truth table0%0/3
A and B
A= $line =~ /^HTTP/uB= $line =~ /200/u
| |||||||||||||||||||||||||||||
| 81 | 0 | return 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 1 | return 0; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | sub url_type { | ||||||||||||||||||||||||||
|
Subroutine
url_type: called CC=7 SCAR=22.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | my $url = shift; | |||||||||||||||||||||||||||
| 93 | 1 | my $cmd = qq(curl --silent -L -I $url); | |||||||||||||||||||||||||||
| 94 | 1 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 95 | 0 | Carp::croak("[url_type] ", "Failed to execute: $!\n"); | |||||||||||||||||||||||||||
| 96 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | Carp::croak("[url_type] ", sprintf("Child died with signal %d, %s coredump\n"), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | ($? & 127), ($? & 128) ? 'with' : 'without'); | ||||||||||||||||||||||||||||
| 99 | } elsif ($? >> 8) { | ||||||||||||||||||||||||||||
| 100 | 0 | Carp::croak("[url_type] ", sprintf("Child exited with value %d\n", $? >> 8)); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 1 | my @output = `$cmd`; | |||||||||||||||||||||||||||
| 103 | 1 | for my $line (@output) { | |||||||||||||||||||||||||||
| 104 | 9 | chomp $line; | |||||||||||||||||||||||||||
| 105 | 9 | ▶ | if ($line =~ /^content-type/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # Strip color codes | ||||||||||||||||||||||||||||
| 107 | 1 | $line =~ s/\e\[[\d;]*[a-zA-Z]//g; | |||||||||||||||||||||||||||
| 108 | 1 | my ($type) = $line =~ /Content-Type: (.*)/i; | |||||||||||||||||||||||||||
| 109 | 1 | return $type; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 0 | return undef; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub curl { | ||||||||||||||||||||||||||
|
Subroutine
curl: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | my ($url) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | eval { | |||||||||||||||||||||||||||
| 120 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Create a UserAgent object | ||||||||||||||||||||||||||||
| 123 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 124 | 0 | $ua->ssl_opts(verify_hostname => 0); # Disable SSL verification (optional) | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Send the initial GET request | ||||||||||||||||||||||||||||
| 127 | 0 | my $response = $ua->get($url); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Follow redirects if any | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | while ($response->is_redirect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $redirect_url = $response->header('Location'); | |||||||||||||||||||||||||||
| 132 | 0 | $response = $ua->get($redirect_url); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | return $response->decoded_content; | |||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | # Fallback to system curl command | ||||||||||||||||||||||||||||
| 140 | 0 | eval { | |||||||||||||||||||||||||||
| 141 | 0 | my $output = `curl --silent -L $url`; | |||||||||||||||||||||||||||
| 142 | 0 | return $output; | |||||||||||||||||||||||||||
| 143 | }; | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | die "Can't get content of $url: $@"; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | ▶ | sub photobear { | ||||||||||||||||||||||||||
|
Subroutine
photobear: called CC=10 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 1 | my ($api_key, $mode, $url) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # If $mode is not in $MODES, then die | ||||||||||||||||||||||||||||
| 154 | 1 | ▶ | if (! grep { $_ eq $mode } @MODES) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | Carp::croak("Invalid mode: $mode (must be one of @MODES)"); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # If no API key, then die | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | if (! $api_key or length($api_key) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $api_key or length $api_key == 0
MC/DC0%0/2
not $api_key or length $api_key == 0
not $api_key
length $api_key == 0
Truth table33%1/3
not(A) or B
A= not $api_keyB= length $api_key == 0
| |||||||||||||||||||||||||||||
| 160 | 0 | Carp::croak "No API key provided"; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 1 | my $cmd = qq(curl --location --silent --request POST '$PHOTOBEAR_URL' \ | |||||||||||||||||||||||||||
| 163 | --header 'x-api-key: $api_key' \ | ||||||||||||||||||||||||||||
| 164 | --header 'Content-Type: application/json' \ | ||||||||||||||||||||||||||||
| 165 | --data-raw '{ | ||||||||||||||||||||||||||||
| 166 | "photo_url":"$url", | ||||||||||||||||||||||||||||
| 167 | "mode":"$mode" | ||||||||||||||||||||||||||||
| 168 | }'); | ||||||||||||||||||||||||||||
| 169 | 1 | $cmd =~ s/\n//g; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | ▶ | if ($ENV{'DEBUG'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | say STDERR "[DEBUG] $cmd"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 1 | ▶ | my $output = $ENV{'DEBUG'} ? $TEST_ANSWER : `$cmd`; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 177 | 0 | Carp::croak("[photobear]", "Failed to execute: $!\n"); | |||||||||||||||||||||||||||
| 178 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | Carp::croak("[photobear]", sprintf("Child died with signal %d, %s coredump\n"), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | ($? & 127), ($? & 128) ? 'with' : 'without'); | ||||||||||||||||||||||||||||
| 181 | } elsif ($? >> 8) { | ||||||||||||||||||||||||||||
| 182 | 0 | Carp::croak("[photobear]", sprintf("Child exited with value %d\n", $? >> 8)); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | my $decoded_content = decode_json($output); | |||||||||||||||||||||||||||
| 186 | 0 | return $decoded_content; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub download { | ||||||||||||||||||||||||||
|
Subroutine
download: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 0 | my ($url, $dest) = @_; | |||||||||||||||||||||||||||
| 192 | # Use curl | ||||||||||||||||||||||||||||
| 193 | 0 | my $cmd = qq(curl -L -o "$dest" "$url");# | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if ($TESTMODE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | return 1; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | 0 | my $output = `$cmd`; | |||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 200 | 0 | Carp::croak("[download] ", "Failed to execute: $!\n"); | |||||||||||||||||||||||||||
| 201 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | Carp::croak("[download] ", sprintf("Child died with signal %d, %s coredump\n"), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | ($? & 127), ($? & 128) ? 'with' : 'without'); | ||||||||||||||||||||||||||||
| 204 | } elsif ($? >> 8) { | ||||||||||||||||||||||||||||
| 205 | 0 | Carp::croak("[download] ", sprintf("Child exited with value %d\n", $? >> 8)); | |||||||||||||||||||||||||||
| 206 | } elsif ($? == 0) { | ||||||||||||||||||||||||||||
| 207 | 0 | return 1; | |||||||||||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||||||||||
| 209 | 0 | return 0; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 2 | 1; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | __END__ | ||||||||||||||||||||||||||||