File Coverage

blib/lib/Woothee.pm
Criterion Covered Total %
statement 129 133 96.9
branch 72 80 90.0
condition 8 18 44.4
subroutine 21 21 100.0
pod 2 11 18.1
total 232 263 88.2


line stmt bran cond sub pod time code
1             package Woothee;
2              
3 6     6   256962 use strict;
  6         16  
  6         204  
4 6     6   38 use warnings;
  6         12  
  6         175  
5 6     6   34 use Carp;
  6         14  
  6         376  
6              
7 6     6   2086 use Woothee::DataSet;
  6         33  
  6         365  
8 6     6   1880 use Woothee::Browser;
  6         12  
  6         179  
9 6     6   1787 use Woothee::OS;
  6         17  
  6         181  
10 6     6   1733 use Woothee::MobilePhone;
  6         18  
  6         193  
11 6     6   1611 use Woothee::Crawler;
  6         17  
  6         182  
12 6     6   1664 use Woothee::Appliance;
  6         16  
  6         171  
13 6     6   1655 use Woothee::Misc;
  6         17  
  6         4716  
14              
15             our $VERSION = "1.7.0";
16              
17             sub parse {
18 717     717 1 1658650 my ($this,$useragent) = @_;
19 717   66     2636 $useragent ||= $this; # called as Woothee::parse()
20 717         1291 return fill_result(exec_parse($useragent));
21             }
22              
23             sub is_crawler {
24 717     717 1 1213322 my ($this,$useragent) = @_;
25 717   66     2463 $useragent ||= $this; # called as Woothee::is_crawler()
26              
27 717 0 33     1572 return 0 if not defined($useragent) || length($useragent) < 1 || $useragent eq '-';
      33        
28 717 100       1339 if (try_crawler($useragent, {})) {
29 168         422 return 1;
30             }
31 549         1564 return 0;
32             }
33              
34             sub exec_parse {
35 717     717 0 1073 my $useragent = shift;
36 717         1008 my $result = {};
37              
38 717 0 33     1740 return $result if not defined($useragent) || length($useragent) < 1 || $useragent eq '-';
      33        
39              
40 717 100       1373 if (try_crawler($useragent, $result)) {
41 168         353 return $result;
42             }
43              
44 549 100       1049 if (try_browser($useragent, $result)) {
45 216 50       459 if (try_os($useragent, $result)) {
46 216         568 return $result;
47             }else{
48 0         0 return $result;
49             }
50             }
51              
52 333 100       660 if (try_mobilephone($useragent, $result)) {
53 78         183 return $result;
54             }
55              
56 255 100       479 if (try_appliance($useragent, $result)) {
57 33         78 return $result;
58             }
59              
60 222 100       407 if (try_misc($useragent, $result)) {
61 9         25 return $result;
62             }
63              
64             # browser unknown. check os only
65 213 100       386 if (try_os($useragent, $result)) {
66 45         125 return $result;
67             }
68              
69 168 50       295 if (try_rare_cases($useragent, $result)) {
70 168         425 return $result;
71             }
72              
73 0         0 return $result;
74             }
75              
76             sub try_crawler {
77 1434     1434 0 2194 my ($useragent, $result) = @_;
78              
79 1434 100       3262 if (Woothee::Crawler::challenge_google($useragent, $result)) {
80 54         121 return 1;
81             }
82 1380 100       2955 if (Woothee::Crawler::challenge_crawlers($useragent, $result)) {
83 282         587 return 1;
84             }
85 1098         2187 return 0;
86             }
87              
88             sub try_browser {
89 549     549 0 895 my ($useragent, $result) = @_;
90              
91 549 100       1439 if (Woothee::Browser::challenge_msie($useragent, $result)) {
92 66         203 return 1;
93             }
94 483 100       1155 if (Woothee::Browser::challenge_vivaldi($useragent, $result)) {
95 9         30 return 1;
96             }
97 474 100       1068 if (Woothee::Browser::challenge_safari_chrome($useragent, $result)) {
98 72         186 return 1;
99             }
100 402 100       860 if (Woothee::Browser::challenge_firefox($useragent, $result)) {
101 27         65 return 1;
102             }
103 375 100       842 if (Woothee::Browser::challenge_opera($useragent, $result)) {
104 30         78 return 1;
105             }
106 345 100       759 if (Woothee::Browser::challenge_webview($useragent, $result)) {
107 12         33 return 1;
108             }
109              
110 333         669 return 0;
111             }
112              
113             sub try_os {
114 429     429 0 703 my ($useragent, $result) = @_;
115              
116             # Windows PC, and Windows Phone OS
117 429 100       1046 if (Woothee::OS::challenge_windows($useragent, $result)) {
118 93         238 return 1;
119             }
120              
121             # OSX PC and iOS devices(strict check)
122 336 100       706 if (Woothee::OS::challenge_osx($useragent, $result)) {
123 54         133 return 1;
124             }
125              
126             # Linux PC, and Android
127 282 100       572 if (Woothee::OS::challenge_linux($useragent, $result)) {
128 36         83 return 1;
129             }
130              
131             # all useragents matches /(iPhone|iPad|iPod|Android|BlackBerry)/
132 246 100       513 if (Woothee::OS::challenge_smartphone($useragent, $result)) {
133 36         83 return 1;
134             }
135              
136             # mobile phones like KDDI-.*
137 210 100       438 if (Woothee::OS::challenge_mobilephone($useragent, $result)) {
138 18         43 return 1;
139             }
140              
141             # Nintendo DSi/Wii with Opera
142 192 100       374 if (Woothee::OS::challenge_appliance($useragent, $result)) {
143 6         15 return 1;
144             }
145              
146             # Win98, BSD
147 186 100       324 if (Woothee::OS::challenge_misc($useragent, $result)) {
148 18         39 return 1;
149             }
150              
151 168         290 return 0;
152             }
153              
154             sub try_mobilephone {
155 333     333 0 525 my ($useragent, $result) = @_;
156              
157 333 100       804 if (Woothee::MobilePhone::challenge_docomo($useragent, $result)) {
158 18         59 return 1;
159             }
160 315 100       695 if (Woothee::MobilePhone::challenge_au($useragent, $result)) {
161 6         16 return 1;
162             }
163 309 100       647 if (Woothee::MobilePhone::challenge_softbank($useragent, $result)) {
164 21         48 return 1;
165             }
166 288 100       600 if (Woothee::MobilePhone::challenge_willcom($useragent, $result)) {
167 9         26 return 1;
168             }
169 279 100       566 if (Woothee::MobilePhone::challenge_misc($useragent, $result)) {
170 24         49 return 1;
171             }
172 255         432 return 0;
173             }
174              
175             sub try_appliance {
176 255     255 0 391 my ($useragent, $result) = @_;
177              
178 255 100       614 if (Woothee::Appliance::challenge_playstation($useragent, $result)) {
179 15         40 return 1;
180             }
181 240 100       517 if (Woothee::Appliance::challenge_nintendo($useragent, $result)) {
182 6         18 return 1;
183             }
184 234 100       474 if (Woothee::Appliance::challenge_digitaltv($useragent, $result)) {
185 12         25 return 1;
186             }
187 222         398 return 0;
188             }
189              
190             sub try_misc {
191 222     222 0 337 my ($useragent, $result) = @_;
192              
193 222 100       491 if (Woothee::Misc::challenge_desktoptools($useragent, $result)) {
194 9         22 return 1;
195             }
196 213         401 return 0;
197             }
198              
199             sub try_rare_cases {
200 168     168 0 256 my ($useragent, $result) = @_;
201              
202 168 50       323 if (Woothee::Misc::challenge_smartphone_patterns($useragent, $result)) {
203 0         0 return 1;
204             }
205 168 100       356 if (Woothee::Browser::challenge_sleipnir($useragent, $result)) {
206 3         9 return 1;
207             }
208 165 100       327 if (Woothee::Misc::challenge_http_library($useragent, $result)) {
209 78         175 return 1;
210             }
211 87 100       167 if (Woothee::Misc::challenge_maybe_rss_reader($useragent, $result)) {
212 18         41 return 1;
213             }
214 69 50       155 if (Woothee::Crawler::challenge_maybe_crawler($useragent, $result)) {
215 69         148 return 1;
216             }
217 0         0 return 0;
218             }
219              
220             my @filled = (
221             Woothee::DataSet->const('ATTRIBUTE_NAME') => Woothee::DataSet->const('VALUE_UNKNOWN'),
222             Woothee::DataSet->const('ATTRIBUTE_CATEGORY') => Woothee::DataSet->const('VALUE_UNKNOWN'),
223             Woothee::DataSet->const('ATTRIBUTE_OS') => Woothee::DataSet->const('VALUE_UNKNOWN'),
224             Woothee::DataSet->const('ATTRIBUTE_OS_VERSION') => Woothee::DataSet->const('VALUE_UNKNOWN'),
225             Woothee::DataSet->const('ATTRIBUTE_VERSION') => Woothee::DataSet->const('VALUE_UNKNOWN'),
226             Woothee::DataSet->const('ATTRIBUTE_VENDOR') => Woothee::DataSet->const('VALUE_UNKNOWN'),
227             );
228             sub fill_result {
229 717     717 0 1074 my $result = shift;
230 717         4937 return +{@filled, %$result};
231             }
232              
233             1;
234              
235             __END__