#!perl

# Note: This script is a CLI for Riap function /App/CekBpom/cek_bpom_products
# and generated automatically using Perinci::CmdLine::Gen version 0.498

our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2021-02-19'; # DATE
our $DIST = 'App-CekBpom'; # DIST
our $VERSION = '0.013'; # VERSION

use 5.010001;
use strict;
use warnings;
use Log::ger;

use Perinci::CmdLine::Any;

my $cmdline = Perinci::CmdLine::Any->new(
    url => "/App/CekBpom/cek_bpom_products",
    program_name => "cek-bpom-products",
    log => 1,
    read_config => 0,
    read_env => 0,
);

$cmdline->run;

# ABSTRACT: Search BPOM products via https://cekbpom.pom.go.id/
# PODNAME: cek-bpom-products

__END__

=pod

=encoding UTF-8

=head1 NAME

cek-bpom-products - Search BPOM products via https://cekbpom.pom.go.id/

=head1 VERSION

This document describes version 0.013 of cek-bpom-products (from Perl distribution App-CekBpom), released on 2021-02-19.

=head1 SYNOPSIS

Usage:

 % cek-bpom-products [-P] [--bentuk-sediaan] [--debug] [--format=name]
     [--get-product-detail] [--json] [--jumlah-dan-kemasan] [--komposisi]
     [--log-level=level] [-m] [--merk] [--(no)naked-res] [--nama-pendaftar]
     [--nama-produk] [--no-get-product-detail] [--noget-product-detail]
     [--nomor-registrasi] [--note=s] [--npwp-pendaftar] [-p]
     [--page-result[=program]] [--queries-json=s] [--query-log-file=s]
     [--query=s+] [--quiet] [--result-dump-dir=s] [--search-type=s+]
     [--search-types-json=s] [-t=s+] [--trace] [--verbose]
     [--view-result[=program]] <query> ...

Examples:

By default search against name (nama_produk) and brand (merk):

 % cek-bpom-products hichew hi-chew 'hi chew'

Adding --trace will show query details, --format html+datatables is convenient to see/search/sort results in browser:

 % cek-bpom-products hichew hi-chew 'hi chew' --trace --format html+datatables

=head1 DESCRIPTION

Uses L<LWP::UserAgent::Plugin> so you can add retry, caching, or additional
HTTP client behavior by setting C<LWP_USERAGENT_PLUGINS> environment variable.

=head1 OPTIONS

C<*> marks required options.

=head2 Main options

=over

=item B<--bentuk-sediaan>

Shortcut for --search-type=bentuk_sediaan.

See C<--search-type>.

=item B<--get-product-detail>

For each product (search result), fetch the detail. This currently fetches the
manufacturer ("produsen"), which is not displayed by the search result page.
Note that this requires a separate HTTP request for each product so can
potentially take a long time and might get you banned. Suggestions include: (1)
searching without this option first to find out the number of results, then
search again with this option if you need it; (2) use
<pm:LWP::UserAgent::Plugin::Delay> to throttle your HTTP requests.


=item B<--jumlah-dan-kemasan>

Shortcut for --search-type=jumlah_dan_kemasan.

See C<--search-type>.

=item B<--komposisi>

Shortcut for --search-type=komposisi.

See C<--search-type>.

=item B<--merk>

Shortcut for --search-type=merk.

See C<--search-type>.

=item B<--nama-pendaftar>

Shortcut for --search-type=nama_pendaftar.

See C<--search-type>.

=item B<--nama-produk>

Shortcut for --search-type=nama_produk.

See C<--search-type>.

=item B<--nomor-registrasi>

Shortcut for --search-type=nomor_registrasi.

See C<--search-type>.

=item B<--npwp-pendaftar>

Shortcut for --search-type=npwp_pendaftar.

See C<--search-type>.

=item B<--queries-json>=I<s>

See C<--query>.

=item B<--query>=I<s@>*

Can be specified multiple times.

=item B<--search-type>=I<s@>, B<-t>

Select what field(s) to search against.

By default, if not specified, will search against product name ("nama_produk")
and brand ("merk"). If you specify multiple times, it will search against all
those types, e.g.:

    --search-type nama_produk --search-type nama_pendaftar

or:

    --nama-produk --nama-pendaftar

Note: the mobile app version allows you to search for products by original
manufacturer ("produsen") as well, which is not available in the website
version. The website allows you to search for producers ("sarana") by
name/address/city/province/country, though, and lets you view what products are
registered for that producer.

This utility will allow you to fetch the detail of each product, including
manufacturer (see `--get-product-detail` option).


Can be specified multiple times.

=item B<--search-types-json>=I<s>

Select what field(s) to search against (JSON-encoded).

See C<--search-type>.

=item B<-m>

Shortcut for --search-type=merk.

See C<--search-type>.

=item B<-p>

Shortcut for --search-type=nama_produk.

See C<--search-type>.

=item B<-P>

Shortcut for --search-type=nama_pendaftar.

See C<--search-type>.

=back

=head2 Logging options

=over

=item B<--debug>

Shortcut for --log-level=debug.

=item B<--log-level>=I<s>

Set log level.

=item B<--note>=I<s>

Add note.

This will not be sent as queries, but will be added to the log file if log file
is specified, as well as added to the result dump file name, in encoded form.


=item B<--query-log-file>=I<s>

Log queries to log file.

If specified, each invocation of this utility will be logged into a line in the
specified file path, in TSV format. Tab character in the query will be converted
into 4 spaces, to avoid clash with the use of Tab as field separator.

For example, this invocation:

    % cek-bpom-products "minuman susu fermentasi" yakult --query-log-file /some/path.txt

Sample log line:

    time:2020-10-22T01:02:03.000Z    queries:minuman susu fermentasi,yakult    search_types:merk,nama_produk    num_results:51    duration:3.402


=item B<--quiet>

Shortcut for --log-level=error.

=item B<--result-dump-dir>=I<s>

Dump result to directory.

If specified, will dump full enveloped result to a file in specified directory
path, in JSON format. The JSON formatting makes it easy to grep each row. The
file will be named
`cek-bpom-products-result.<encoded-timestamp>.<search-types-encoded>.<queries-encoded>(.<note-encoded>)?.json`.
The encoded timestamp is ISO 8601 format with colon replaced by underscore. The
encoded query will replace all every group of "unsafe" characters in query with
a single dash. The same goes with encoded note, which comes from the `note`
argument. For example, this invocation:

    % cek-bpom-products "minuman susu fermentasi" yakult --note "some note"

will result in a result dump file name like:
`cek-bpom-products-result.2020-10-22T01_02_03.000Z.merk-nama_produk.minuman-susu-fermentasi-yakult.some-note.json`.


=item B<--trace>

Shortcut for --log-level=trace.

=item B<--verbose>

Shortcut for --log-level=info.

=back

=head2 Output options

=over

=item B<--format>=I<s>

Choose output format, e.g. json, text.

Default value:

 undef

=item B<--json>

Set output format to json.

=item B<--naked-res>

When outputing as JSON, strip result envelope.

Default value:

 0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
`--naked-res` so you just get:

    [1,2,3]


=item B<--page-result>

Filter output through a pager.

=item B<--view-result>

View output using a viewer.

=back

=head2 Other options

=over

=item B<--help>, B<-h>, B<-?>

Display help message and exit.

=item B<--version>, B<-v>

Display program's version and exit.

=back

=head1 COMPLETION

This script has shell tab completion capability with support for several
shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C cek-bpom-products cek-bpom-products

in your bash startup (e.g. F<~/.bashrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is recommended, however, that you install modules using L<cpanm-shcompgen>
which can activate shell completion for scripts immediately.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete cek-bpom-products 'p/*/`cek-bpom-products`/'

in your tcsh startup (e.g. F<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is also recommended to install L<shcompgen> (see above).

=head2 other shells

For fish and zsh, install L<shcompgen> as described above.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CekBpom>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CekBpom>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://github.com/perlancar/perl-App-CekBpom/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
