App-Photobear

 view release on metacpan or  search on metacpan

lib/App/Photobear.pm  view on Meta::CPAN

        if ($@) {
            my $cmd = qq(curl --silent -L -I $url);
            my @output = `$cmd`;
            for my $line (@output) {
                chomp $line;
                if ($line =~ /^HTTP/ and $line =~ /200/) {
                    return 1;
                }
            }
        }

    } else {
        return 0;
    }
}

sub url_type {
    my $url = shift;
    my $cmd = qq(curl --silent -L -I $url);
    if ($? == -1) {
        Carp::croak("[url_type] ", "Failed to execute: $!\n");
    } elsif ($? & 127) {
        Carp::croak("[url_type] ", sprintf("Child died with signal %d, %s coredump\n"),
            ($? & 127),  ($? & 128) ? 'with' : 'without');
    } elsif ($? >> 8) {
        Carp::croak("[url_type] ", sprintf("Child exited with value %d\n", $? >> 8));
    }
    my @output = `$cmd`;
    for my $line (@output) {
        chomp $line;
        if ($line =~ /^content-type/i) {
            # Strip color codes
            $line =~ s/\e\[[\d;]*[a-zA-Z]//g;
            my ($type) = $line =~ /Content-Type: (.*)/i;
            return $type;
        }
    }
    return undef;
}

sub curl {
    my ($url) = @_;
    

    eval {
        require LWP::UserAgent;
        
        # Create a UserAgent object
        my $ua = LWP::UserAgent->new;
        $ua->ssl_opts(verify_hostname => 0);  # Disable SSL verification (optional)
        
        # Send the initial GET request
        my $response = $ua->get($url);
        
        # Follow redirects if any
        while ($response->is_redirect) {
            my $redirect_url = $response->header('Location');
            $response = $ua->get($redirect_url);
        }
        
        return $response->decoded_content;
    };
    
    if ($@) {
        # Fallback to system curl command
        eval {
            my $output = `curl --silent -L $url`;
            return $output;
        };
        if ($@) {
            die "Can't get content of $url: $@";
        }
    }
}

sub photobear {
    my ($api_key, $mode, $url) = @_;

    # If $mode is not in $MODES, then die
    if (! grep { $_ eq $mode } @MODES) {
        Carp::croak("Invalid mode: $mode (must be one of @MODES)");
    }

    # If no API key, then die
    if (! $api_key or length($api_key) == 0) {
        Carp::croak "No API key provided";
    }
    my $cmd = qq(curl --location --silent --request POST '$PHOTOBEAR_URL' \
        --header 'x-api-key: $api_key' \
        --header 'Content-Type: application/json' \
        --data-raw '{
            "photo_url":"$url", 
            "mode":"$mode"
        }');
    $cmd =~ s/\n//g;
    
    if ($ENV{'DEBUG'}) {
        say STDERR "[DEBUG] $cmd";
    }

    my $output = $ENV{'DEBUG'} ? $TEST_ANSWER : `$cmd`;
    if ($? == -1) {
        Carp::croak("[photobear]", "Failed to execute: $!\n");
    } elsif ($? & 127) {
        Carp::croak("[photobear]", sprintf("Child died with signal %d, %s coredump\n"),
            ($? & 127),  ($? & 128) ? 'with' : 'without');
    } elsif ($? >> 8) {
        Carp::croak("[photobear]", sprintf("Child exited with value %d\n", $? >> 8));
    }
    
    my $decoded_content = decode_json($output);
    return $decoded_content;

}

sub download {
    my ($url, $dest) = @_;
    # Use curl
    my $cmd = qq(curl  -L -o "$dest" "$url");#
    
    if ($TESTMODE) {
        return 1;
    }
    my $output = `$cmd`;
    if ($? == -1) {
        Carp::croak("[download] ", "Failed to execute: $!\n");
    } elsif ($? & 127) {
        Carp::croak("[download] ", sprintf("Child died with signal %d, %s coredump\n"),
            ($? & 127),  ($? & 128) ? 'with' : 'without');
    } elsif ($? >> 8) {
        Carp::croak("[download] ", sprintf("Child exited with value %d\n", $? >> 8));
    } elsif ($? == 0) {
        return 1;
    } else {
        return 0;
    }

}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Photobear - Photobear API client

=head1 VERSION

version 0.1.2

=head1 SYNOPSIS

  use App::Photobear;
  
  # Load configuration from file
  my $config = App::Photobear::loadconfig($config_file);
  
  # Save configuration to file
  App::Photobear::saveconfig($config_file, $config);
  
  # Check if a URL exists
  my $url_exists = App::Photobear::url_exists($url);

  # Get the content of a URL
  my $content = App::Photobear::curl($url);

  # Perform Photobear API request



( run in 1.000 second using v1.01-cache-2.11-cpan-5b529ec07f3 )