Aion-Surf
view release on metacpan or search on metacpan
lib/Aion/Surf.pm view on Meta::CPAN
package Aion::Surf;
use 5.22.0;
no strict; no warnings; no diagnostics;
use common::sense;
our $VERSION = "0.0.3";
use List::Util qw/pairmap/;
use LWP::UserAgent qw//;
use HTTP::Cookies qw//;
use Aion::Format::Json qw//;
use Aion::Format::Url qw//;
use Exporter qw/import/;
our @EXPORT = our @EXPORT_OK = grep {
ref \$Aion::Surf::{$_} eq "GLOB"
&& *{$Aion::Surf::{$_}}{CODE}
&& !/^(_|(NaN|import)\z)/n
} keys %Aion::Surf::;
#@category surf
use config TIMEOUT => 10;
use config FROM_IP => undef;
use config AGENT => q{Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15};
our $ua = LWP::UserAgent->new;
$ua->agent(AGENT);
#$ua->env_proxy;
$ua->timeout(TIMEOUT);
$ua->local_address(FROM_IP) if FROM_IP;
$ua->cookie_jar(HTTP::Cookies->new);
# ÐÐµÐ¶Ð´Ñ Ð²Ñзовами делаем ÑлÑÑайнÑй инÑеÑвал (Ð´Ð»Ñ Ð³Ñаббинга - ÑÑÐ¾Ð±Ñ Ð½Ðµ бÑÑÑ Ð·Ð°Ð±Ð»Ð¾ÐºÐ¸ÑованнÑм за авÑомаÑиÑеÑкие обÑаÑениÑ)
our $SLEEP = 0;
our $LAST_REQUEST = Time::HiRes::time();
sub _sleep(;$) {
Time::HiRes::sleep(rand + .5) if Time::HiRes::time() - $LAST_REQUEST < 2;
$LAST_REQUEST = Time::HiRes::time();
}
sub surf(@) {
my $method = $_[0] =~ /^(\w+)\z/ ? shift: "GET";
my $url = shift;
my $headers;
my $data = ref $_[0]? shift: undef;
$headers = $data, undef $data if $method =~ /^(GET|HEAD)\z/n;
my %set = @_;
if(exists $set{sleep}) {
my $sleep = delete $set{sleep};
} else {
_sleep if $SLEEP;
}
my $query = delete $set{query};
if(defined $query) {
$url = join "", $url, $url =~ /\?/ ? "&": "?",
Aion::Format::Url::to_url_params $query;
}
my $request = HTTP::Request->new($method => $url);
my $validate_data = sub {
die "surf: data has already been provided!" if defined $data;
die "surf: sended data in $method!" if $method =~ /^(HEAD|GET)\z/;
};
# УÑÑанавливаем даннÑе
my $json = delete $set{json};
$json = $data, undef $data if not defined $json and ref $data eq "HASH";
if(defined $json) {
$validate_data->();
$request->header('Content-Type' => 'application/json; charset=utf-8');
$data = Aion::Format::Json::to_json $json;
utf8::encode($data) if utf8::is_utf8($data);
$request->content($data);
}
my $form = delete $set{form};
$form = $data, undef $data if not defined $form and ref $data eq "ARRAY";
if(defined $form) {
$validate_data->();
$data = 1;
$request->header('Content-Type' => 'application/x-www-form-urlencoded');
$request->content(Aion::Format::Url::to_url_params $form);
}
if($headers = delete($set{headers}) // $headers) {
lib/Aion/Surf.pm view on Meta::CPAN
$response->content("nf");
}
}
$response
};
get "http://example/ex" # => get
surf "http://example/ex" # => get
head "http://example/ex" # -> 1
head "http://example/not-found" # -> ""
surf HEAD => "http://example/ex" # -> 1
surf HEAD => "http://example/not-found" # -> ""
[map { surf $_ => "http://example/ex" } qw/GET HEAD POST PUT PATCH DELETE/] # --> [qw/get 1 post put patch delete/]
patch "http://example/json" # --> {a => 10}
[map patch, qw! http://example/ex http://example/json !] # --> ["patch", {a => 10}]
get ["http://example/ex", headers => {Accept => "*/*"}] # => get
surf "http://example/ex", headers => [Accept => "*/*"] # => get
=head1 DESCRIPTION
Aion::Surf contains a minimal set of functions for surfing the Internet. The purpose of the module is to make surfing as easy as possible, without specifying many additional settings.
=head1 SUBROUTINES
=head2 surf ([$method], $url, [$data], %params)
Send request by LWP::UserAgent and adapt response.
C<@params> maybe:
=over
=item * C<query> - add query params to C<$url>.
=item * C<json> - body request set in json format. Add header C<Content-Type: application/json; charset=utf-8>.
=item * C<form> - body request set in url params format. Add header C<Content-Type: application/x-www-form-urlencoded>.
=item * C<headers> - add headers. If C<header> is array ref, then add in the order specified. If C<header> is hash ref, then add in the alphabet order.
=item * C<cookies> - add cookies. Same as: C<< cookies =E<gt> {go =E<gt> "xyz", session =E<gt> ["abcd", path =E<gt> "/page"]} >>.
=item * C<response> - returns response (as HTTP::Response) by this reference.
=back
my $req = "MAYBE_ANY_METHOD https://ya.ru/page?z=30&x=10&y=%1F9E8
Accept: */*,image/*
Content-Type: application/x-www-form-urlencoded
x&y=2
";
my $req_cookies = 'Set-Cookie3: go=""; path="/"; domain=ya.ru; version=0
Set-Cookie3: session=%1F9E8; path="/page"; domain=ya.ru; version=0
';
# mock
*LWP::UserAgent::request = sub {
my ($ua, $request) = @_;
$request->as_string # -> $req
$ua->cookie_jar->as_string # -> $req_cookies
my $response = HTTP::Response->new(200, "OK");
$response->content(3.14);
$response
};
my $res = surf MAYBE_ANY_METHOD => "https://ya.ru/page?z=30", [x => 1, y => 2, z => undef],
headers => [
'Accept' => '*/*,image/*',
],
query => [x => 10, y => "ð§¨"],
response => \my $response,
cookies => {
go => "",
session => ["ð§¨", path => "/page"],
},
;
$res # -> 3.14
ref $response # => HTTP::Response
=head2 head (;$url)
Check resurce in internet. Returns C<1> if exists resurce in internet, otherwice returns C<"">.
=head2 get (;$url)
Get content from resurce in internet.
=head2 post (;$url, [$headers_href], %params)
Add content resurce in internet.
=head2 put (;$url, [$headers_href], %params)
Create or update resurce in internet.
=head2 patch (;$url, [$headers_href], %params)
Set attributes on resurce in internet.
=head2 del (;$url)
Delete resurce in internet.
=head2 chat_message ($chat_id, $message)
Sends a message to a telegram chat.
# mock
use Aion::Format::Json;
*LWP::UserAgent::request = sub {
my ($ua, $request) = @_;
( run in 0.724 second using v1.01-cache-2.11-cpan-524268b4103 )