-
Notifications
You must be signed in to change notification settings - Fork 1
/
Duadua.pm
405 lines (278 loc) · 9.23 KB
/
Duadua.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package Duadua;
use strict;
use warnings;
use Duadua::Parser;
our $VERSION = '0.34';
my @PARSER_PROC_LIST = qw/
Duadua::Parser::Browser::MicrosoftEdge
Duadua::Parser::Browser::GoogleChrome
Duadua::Parser::Browser::MicrosoftInternetExplorer
Duadua::Parser::Bot::Googlebot
Duadua::Parser::Bot::GooglebotMobile
Duadua::Parser::Bot::Bingbot
Duadua::Parser::Browser::MozillaFirefox
Duadua::Parser::Bot::Twitterbot
Duadua::Parser::Bot::FacebookCrawler
Duadua::Parser::Bot::Slackbot
Duadua::Parser::Bot::BingPreview
Duadua::Parser::Browser::Opera
Duadua::Parser::Bot::GooglebotAd
Duadua::Parser::Bot::GoogleRead
Duadua::Parser::Bot::AdIdxBot
Duadua::Parser::Browser::YahooJapanAppBrowser
Duadua::Parser::Bot::ChatGPTUser
Duadua::Parser::Browser::AppleSafari
Duadua::Parser::Browser::Vivaldi
Duadua::Parser::Bot::Huawei
Duadua::Parser::Bot::Sakura
Duadua::Parser::HTTPClient::HTTPClient
Duadua::Parser::Bot::YahooSlurp
Duadua::Parser::Bot::Baiduspider
Duadua::Parser::Bot::Bytespider
Duadua::Parser::Bot::YandexBot
Duadua::Parser::Bot::GooglebotMisc
Duadua::Parser::Bot::DuckDuckBot
Duadua::Parser::Bot::Applebot
Duadua::Parser::Bot::Msnbot
Duadua::Parser::Bot::BotMisc
Duadua::Parser::Bot::Log4Shell
Duadua::Parser::Browser::GoogleApp
Duadua::Parser::Browser::SamsungInternetBrowser
Duadua::Parser::Browser::Yandex
Duadua::Parser::Browser::DuckDuckGo
Duadua::Parser::Bot::OracleGrapeshotCrawler
Duadua::Parser::Bot::MailRUBot
Duadua::Parser::Browser::PaleMoon
Duadua::Parser::Browser::UCBrowser
Duadua::Parser::Browser::BrowserMisc
Duadua::Parser::Bot::YahooJapanBot
Duadua::Parser::Bot::HatenaBot
Duadua::Parser::Bot::Feedly
Duadua::Parser::Bot::Reeder
Duadua::Parser::Bot::QuiteRSS
Duadua::Parser::Bot::Feedbin
Duadua::Parser::Bot::Inoreader
Duadua::Parser::Bot::Fastladder
Duadua::Parser::Bot::Feedeen
Duadua::Parser::Bot::Feedspot
Duadua::Parser::Bot::OldReader
Duadua::Parser::Bot::Flipboard
Duadua::Parser::Bot::Skype
Duadua::Parser::Browser::Xiaomi
Duadua::Parser::FeaturePhone::FeaturePhone
Duadua::Parser::Bot::Netcraft
Duadua::Parser::Bot::Barkrowler
Duadua::Parser::Bot::SMTBot
Duadua::Parser::Bot::AdstxtCom
Duadua::Parser::Bot::CentroAdsCrawler
Duadua::Parser::Bot::LetsEncrypt
Duadua::Parser::Bot::ClaudeBot
/;
for my $parser (@PARSER_PROC_LIST) {
eval "require $parser;"; ## no critic
die "Could not load $parser, $@" if $@;
}
sub new {
my $opt = $_[2] || {};
my $ua = $_[1];
if (!defined $ua) {
$ua = exists $ENV{HTTP_USER_AGENT} && defined $ENV{HTTP_USER_AGENT} ? $ENV{HTTP_USER_AGENT} : '';
} elsif (ref($ua) =~ m!^HTTP::Headers!) {
$ua = $ua->header('User-Agent');
}
bless {
_ua => $ua,
_parsed => 0,
_result => {},
_parsers => $_[0]->_build_parsers($opt),
_opt_version => $opt->{version},
}, $_[0];
}
sub _build_parsers {
my ($class, $opt) = @_;
if (exists $opt->{skip}) {
my @parsers;
for my $p (@PARSER_PROC_LIST) {
next if grep { $p eq $_ } @{$opt->{skip}};
push @parsers, $p;
}
return \@parsers;
}
return \@PARSER_PROC_LIST;
}
sub opt_version { shift->{_opt_version} }
sub parsers { shift->{_parsers} }
sub ua { shift->{_ua} }
sub reparse {
my $self = shift;
my $ua = $_[0];
if (!defined $ua) {
$ua = exists $ENV{HTTP_USER_AGENT} && defined $ENV{HTTP_USER_AGENT} ? $ENV{HTTP_USER_AGENT} : '';
} elsif (ref($ua) =~ m!^HTTP::Headers!) {
$ua = $ua->header('User-Agent');
}
$self->{_ua} = $ua;
$self->{_result} = {};
return $self->_parse;
}
sub _result {
if (@_ == 1) {
$_[0]->parse unless $_[0]->{_parsed};
return $_[0]->{_result};
}
$_[0]->{_result} = $_[1];
return $_[0];
}
sub parse {
my $self = shift;
if (ref $self eq __PACKAGE__) {
$self->_parse;
}
elsif ($self eq __PACKAGE__) {
# my $d_obj = Duadua->parse('User-Agent String', $opt);
my $d = __PACKAGE__->new(@_);
return $d->_parse;
}
else {
# my $d_obj = Duadua::parse('User-Agent String', $opt);
my $d = __PACKAGE__->new($self, @_);
return $d->_parse;
}
}
sub _parse {
my $self = shift;
$self->_result(Duadua::Parser->parse($self));
$self->{_parsed} = 1;
return $self;
}
sub name {
shift->_result->{name};
}
sub is_bot {
shift->_result->{is_bot} ? 1 : 0;
}
sub is_ios {
shift->_result->{is_ios} ? 1 : 0;
}
sub is_android {
shift->_result->{is_android} ? 1 : 0;
}
sub is_linux {
shift->_result->{is_linux} ? 1 : 0;
}
sub is_windows {
shift->_result->{is_windows} ? 1 : 0;
}
sub is_chromeos {
shift->_result->{is_chromeos} ? 1 : 0;
}
sub version {
shift->_result->{version} || '';
}
sub _contain_mozilla {
return 1 if shift->{_contain_mozilla}
}
sub _contain_mozilla_top {
return 1 if shift->{_contain_mozilla_top}
}
sub _prefix {
return 1 if index(shift->{_ua}, shift) == 0;
}
sub _contain {
return 1 if index(shift->{_ua}, shift) != -1;
}
1;
__END__
=encoding UTF-8
=head1 NAME
Duadua - Detect User-Agent, do up again!
=head1 SYNOPSIS
use Duadua;
my $ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
my $d = Duadua->new($ua);
$d->is_bot
and say $d->name; # Googlebot
Or call as a function to parse immediately
my $d = Duadua->parse($ua);
$d->is_bot
and say $d->name; # Googlebot
And it's able to accept an object like L<HTTP::Headers> instead of user-agent string.
use HTTP::Headers;
use Duadua;
my $headers = HTTP::Headers->new(
'User_Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
);
my $d = Duadua->new($headers);
$d->is_bot
and say $d->name; # Googlebot
B<NOTE> that an object class should be HTTP::Headers[::*], and it should have a method `header` to get specific HTTP-Header.
If you would like to parse many times, then you can use C<reparse> method. It's fast.
my $d = Duadua->new;
for my $ua (@ua_list) {
my $result = $d->reparse($ua);
$result->is_bot
and say $result->name;
}
If you need to get version info, then you should set true value to version option like below.
use Duadua;
my $ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
my $d = Duadua->new($ua, { version => 1 });
say $d->version; # 2.1
=head1 DESCRIPTION
`Duadua` is a User-Agent detector.
* Detect over 160 User-Agents
* Browsers, Bots and CLI clients
* Detect name, OS and version
* Optimized performance for recent actual logs on a Web site
Send an issue or PR on Github to add a User-Agent you want to detect if it's not supported.
=head1 METHODS
=head2 new($user_agent_string, $options_hash)
constructor
=head3 Constructor options
=over
=item version => 1 or 0
If you set the true value to C<version>, then you can get version string. (By default, don't get version)
=item skip => ['ParserClass']
If you set the array to C<skip>, then you can skip detect logic by specific classes.
NOTE that ParserClass is case sensitive, and it might be going to change results.
=back
=head2 parse
Parses User-Agent string
=head2 reparse($ua)
Parses User-Agent string by same instance without new
=head2 GETTERS
=over
=item ua
Returns raw User-Agent string
=item name
Gets User-Agent name
=item is_bot
Returns true value if the User-Agent is bot.
=item is_ios
Returns true value if the User-Agent is iOS.
=item is_android
Returns true value if the User-Agent is Android.
=item is_linux
Returns true value if the User-Agent is Linux.
=item is_windows
Returns true value if the User-Agent is Windows.
=item is_chromeos
Returns true value if the User-Agent is ChromeOS.
=item opt_version
Returns version option value. If it's true value, then parse to get User Agent version also.
=item version
Returns version from user agent string
=item parsers
The list of User Agent Parser
=back
=head1 REPOSITORY
=begin html
<a href="https://github.com/bayashi/Duadua/blob/main/lib/Duadua.pm"><img src="https://img.shields.io/badge/Version-0.34-green?style=flat"></a> <a href="https://github.com/bayashi/Duadua/blob/main/LICENSE"><img src="https://img.shields.io/badge/LICENSE-Artistic%202.0-GREEN.png?style=flat"></a> <a href="https://github.com/bayashi/Duadua/actions"><img src="https://github.com/bayashi/Duadua/workflows/main/badge.svg?_t=1720941083"/></a> <a href="https://coveralls.io/r/bayashi/Duadua"><img src="https://coveralls.io/repos/bayashi/Duadua/badge.png?_t=1720941083&branch=main"/></a>
=end html
Duadua is hosted on github: L<http://github.com/bayashi/Duadua>
I appreciate any feedback :D
=head1 AUTHOR
Dai Okabayashi E<lt>bayashi@cpan.orgE<gt>
=head1 LICENSE
C<Duadua> is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0. (Note that, unlike the Artistic License 1.0, version 2.0 is GPL compatible by itself, hence there is no benefit to having an Artistic 2.0 / GPL disjunction.) See the file LICENSE for details.
=cut