<commit_before>package Shodan::WebAPI;

use CGI::Enurl;
use HTTP::Request::Common qw(GET);
use JSON::XS;
use LWP::UserAgent;

use warnings;
use strict;

our $VERSION = '0.4';

=head1 NAME

Shodan::WebAPI - A Perl wrapper around the Shodan webservice

=head1 AUTHOR

"achillean", C<< <"jmath at surtri.com"> >>

=head1 SUBROUTINES/METHODS

=head2 new

Initializes the API object.

=cut

sub new {
	my $class = shift;
	my $self  = {
		_api_key  => shift,
		_base_url => "http://www.shodanhq.com/api/",
	};

	bless $self, $class;
	return $self;
}

=head2 _request

General-purpose function to create web requests to SHODAN.

=cut

sub _request {
	my ( $self, $func, $params ) = @_;

	# Add the API key to the parameters automatically
	$params->{'key'} = $self->{_api_key};

	# Create the request URL
	my $url = $self->{_base_url} . $func . "?" . enurl($params);

	# Send the request
	my $ua       = LWP::UserAgent->new;
	my $response = $ua->request( GET $url);

	# Parse the JSON
	my $json_parser = JSON::XS->new();

	# Return the data as a hash
	return $json_parser->decode( $response->content );
}

=head2 search

Search the SHODAN database.

Arguments:
query    -- search query; identical syntax to the website

Returns:
A hash with 3 main items: matches, countries and total.
Visit the website for more detailed information.

=cut

sub search {
	my ( $self, $query ) = @_;

	return $self->_request( "search", { q => $query } );
}

=head2 count

Search the SHODAN database and only return the total number of results,
not any of the actual matches.

Arguments:
query    -- search query; identical syntax to the website

Returns:
A hash with 1 main item: total.

=cut

sub count {
	my ( $self, $query ) = @_;

	return $self->_request( "count", { q => $query } );
}

=head2 host<commit_msg>Added support for paging through search results<commit_after>package Shodan::WebAPI;

use CGI::Enurl;
use HTTP::Request::Common qw(GET);
use JSON::XS;
use LWP::UserAgent;

use warnings;
use strict;

our $VERSION = '0.4';

=head1 NAME

Shodan::WebAPI - A Perl wrapper around the Shodan webservice

=head1 AUTHOR

"achillean", C<< <"jmath at surtri.com"> >>

=head1 SUBROUTINES/METHODS

=head2 new

Initializes the API object.

=cut

sub new {
	my $class = shift;
	my $self  = {
		_api_key  => shift,
		_base_url => "http://www.shodanhq.com/api/",
	};

	bless $self, $class;
	return $self;
}

=head2 _request

General-purpose function to create web requests to SHODAN.

=cut

sub _request {
	my ( $self, $func, $params ) = @_;

	# Add the API key to the parameters automatically
	$params->{'key'} = $self->{_api_key};

	# Create the request URL
	my $url = $self->{_base_url} . $func . "?" . enurl($params);

	# Send the request
	my $ua       = LWP::UserAgent->new;
	my $response = $ua->request( GET $url);

	# Parse the JSON
	my $json_parser = JSON::XS->new();

	# Return the data as a hash
	return $json_parser->decode( $response->content );
}

=head2 search

Search the SHODAN database.

Arguments:
query    -- search query; identical syntax to the website

Optional Arguments:
page     -- page number; use it to iterate over all the search results

Returns:
A hash with 3 main items: matches, countries and total.
Visit the website for more detailed information.

=cut

sub search {
	my ( $self, $query, $page ) = @_;
	my $args = {};

	if (defined $page) {
		$args->{'p'} = $page;
	}

	$args->{'q'} = $query;

	return $self->_request( "search", $args );
}

=head2 count

Search the SHODAN database and only return the total number of results,
not any of the actual matches.

Arguments:
query    -- search query; identical syntax to the website

Returns:
A hash with 1 main item: total.

=cut

sub count {
	my ( $self, $query ) = @_;

	return $self->_request( "count", { q => $query } );
}

=head2 host
{'fraction_non_alphanumeric': 0.13614775725593667, 'fraction_numerical': 0.0063324538258575196, 'mean_word_length': 3.6744759556103577, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3668869', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1331, 'n_words': 460}