App-Photobear

 view release on metacpan or  search on metacpan

bin/curly  view on Meta::CPAN

        $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);
        }
        say STDERR "[DEBUG] LWP::UserAgent Response code: ", $response->code if $verbose;
        return $response->decoded_content;
    };
    
    if ($@) {
        # Fallback to system curl command
        eval {
            $output = `curl --silent -L $url`;
            say STDERR "[DEBUG] used curl: ", $output if $verbose;
            return $output;
        };
        if ($@) {
            say STDERR "[DEBUG] Curl failed" if $verbose;
            die "Can't get content of $url: $@";
        }
    }
    return $output;
}


my @URLS = ();
push(@URLS, @ARGV);
push(@URLS, $url) if $url;

bin/photobear  view on Meta::CPAN

my @MODES = @App::Photobear::MODES;
GetOptions(
    'o|outdir=s' => \$opt_outdir,
    'api-key=s' => \$opt_api_key,
    'config=s'  => \$opt_config_file,
    'verbose'   => \$opt_verbose,
    'h|help'      => sub { usage(); exit 0 },
);

if ($opt_config_file) {
    say STDERR YELLOW, "# Using config file $opt_config_file", RESET if $opt_verbose;
    $CONFIG = App::Photobear::loadconfig($opt_config_file);
    $API_KEY = $CONFIG->{"api_key"} if $CONFIG->{"api_key"};
} 
if ($opt_api_key) {
    if (not defined $API_KEY) {#
        say STDERR YELLOW, "# Saving API KEY to $CONFIG_FILE", RESET if $opt_verbose;
        $CONFIG->{"api_key"} = $opt_api_key;
        App::Photobear::writeconfig($CONFIG_FILE, $CONFIG);
    }
    
}

if (not defined $API_KEY) {
    usage();
    say STDERR RED, "\n# No API KEY found [$CONFIG_FILE]", RESET if $opt_verbose;
    say STDERR "ERROR: API-KEY is required, add it to $CONFIG_FILE or use --api-key option, or the \$PHOTOBEAR_API_KEY environment variable";
    exit 1;
}

my $mode = shift @ARGV;
my $url  = shift @ARGV;


if (not defined $mode) {
    usage();
    say STDERR RED, "\n ERROR: No command specified", RESET;
    exit 1;
} elsif (not grep { $_ eq $mode } @MODES) {
    
    usage();
    say STDERR RED, "\nERROR: Unknown mode $mode", RESET;
    exit 1;
}

# Check URL
if (not defined $url) {
    usage();
    say STDERR "\nERROR: No URL specified";
    exit 1;
} else {
    my $check = App::Photobear::url_type($url);
    if (not defined $check) {
        say STDERR "\nERROR: Invalid URL $url";
        exit 1;
    } elsif ($check !~/image/i) {
        say STDERR "\nWARNING: URL $url might not point to an image";
    }
}

if (! -d $opt_outdir) {
    eval { mkdir $opt_outdir };
    if ($@) {
        say STDERR "\nERROR: Cannot create output directory $opt_outdir";
        exit 1;
    }
}
my $out = App::Photobear::photobear($API_KEY, $mode, $url);

if ($out->{"status"} ne "success") {
    say Dumper $out;
    say STDERR "\nWRONG REQUEST\n";
    exit 1;
} else {
    say STDERR "URL: ", GREEN $out->{"data"}->{"result_url"}, RESET;
    my $dest = catfile($opt_outdir, basename($out->{"data"}->{"result_url"}));
    say STDERR "Saving to: ", GREEN, $dest, RESET;
    App::Photobear::download($out->{"data"}->{"result_url"}, $dest);
    exit 0;
}

sub usage {

    my $modes = join(' ', @MODES);
    print <<EOF;
 Usage: $BASE [options] <command> <url>

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

        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;

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

    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));



( run in 1.032 second using v1.01-cache-2.11-cpan-a1f116cd669 )