browser: update wikimedia mobile browser recognition
[sheet.git] / tools / mkusage-wikimedia
1 #!/usr/bin/perl -n
2 use 5.010; use strict; use warnings;
3 our %count;
4 our $mobile;
5
6 our $VERSION = '1.01';
7
8 if (m{<td class=hl>} .. m{</?td>}) {
9         $count{-source} = 'http://stats.wikimedia.org/archive/squid_reports/';
10         $count{-title } = 'Wikimedia';
11         $count{-date} = $1 if m{ period: (?:\d+ )?(\w+ \d+) };
12         next;
13 }
14
15 # select relevant columns
16 />Browser versions(.*)/ ... m{</table>} && last or next;
17 my ($tr, $id, $count2, $count) = split /(?:<[^>]*>)+/;
18 $mobile = $count2 !~ /non mobile/ if $id ~~ '&nbsp;';
19 next if $id ~~ ['Total', '&nbsp;'];
20
21 # convert to usable syntax
22 my ($browser, $version) = split /\h+/, $id, 2;
23 $count =~ s/,//g;
24 $count =~ s/%$//;
25 $version //= 0;
26 given ($browser) {
27         when (['Firefox', 'Iceweasel']) {
28                 $browser = $mobile ? 'and_ff' : 'firefox';
29                 continue;
30         }
31         when ('MSIE') {
32                 $browser = $mobile ? 'ie_mob' : 'ie';
33                 continue;
34         }
35         when ('Opera') {
36                 $browser = $mobile ? 'op_mob' : 'opera';
37                 for ($version) {
38                         if (m{\(Mini(.*)\)$}) {
39                                 $browser = 'op_mini';
40                                 ($_) = $1 =~ m{^/(\d+)};
41                                 continue;
42                         }
43                         s/^\d*\.\d\K.*//;  # one significant digit
44                 }
45                 continue;
46         }
47         when ('Safari') {
48                 $browser = $mobile ? 'ios_saf' : 'safari';
49                 my $numversion = join('.', map { sprintf '%03d', $_ } split /\./, $version);
50                 $numversion =~ s/6(?=\d{3})//;  # incomparable 6532.22 → 523
51                 for (
52                         # http://en.wikipedia.org/wiki/Safari_version_history
53                         $mobile ? (
54                                 [ '413'     => '1'   ],
55                                 [ '419'     => '1.1' ],
56                                 [ '525'     => '2'   ],
57                                 [ '528'     => '3'   ],
58                                 [ '531'     => '3.2' ],
59                                 [ '531.022' => '4'   ],
60                                 [ '533'     => '4.2' ],
61                                 [ '534'     => '4x' ],
62                         ) : (
63                                 [ '413'     => '2'   ],
64                                 [ '522'     => '3'   ],
65                                 [ '525.013' => '3.1' ],
66                                 [ '525.026' => '3.2' ],
67                                 [ '526'     => '4'   ],
68                                 [ '533'     => '5'   ],
69                                 [ '534'     => '5x'  ],
70                         )
71                 ) {
72                         last if $numversion lt $_->[0];
73                         $version = $_->[1];
74                 }
75         }
76         when ('Chrome') {
77                 $browser = $mobile ? 'and_chr' : 'chrome';
78                 s/\.\d+$// for $version;
79         }
80         when ('Android') {
81                 $browser = 'android';
82         }
83         when ('BlackBerry') {
84                 $browser = 'bb';
85         }
86         when ('UCWEB') {
87                 $browser = 'and_uc';
88         }
89         s/\.0$// for $version;
90 }
91
92 $count{$browser}{$version} += $count;
93
94 END {
95         use Data::Dump 'pp';
96         print pp(\%count);
97 }
98
99 __END__
100
101 =head1 USAGE
102
103         curl http://stats.wikimedia.org/archive/squid_reports/2010-10/SquidReportClients.htm |
104         ./mkusage-wikimedia > browser-usage.inc.pl
105