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