-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank_rosetta.p6
executable file
·50 lines (36 loc) · 1.42 KB
/
rank_rosetta.p6
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
#!/usr/bin/env perl6
#-------------------------------------------.
# Rosetta Code/Rank languages by popularity |
#-------------------------------------------'
# For the task: https://rosettacode.org/wiki/Rosetta_Code/Rank_languages_by_popularity
# Language: Perl 6
# Author: Sam S <[email protected]>
use HTTP::UserAgent;
use JSON::Tiny;
.say for
mediawiki-query('http://rosettacode.org/mw', 'pages',
generator => 'categorymembers',
gcmtitle => "Category:Programming Languages",
prop => 'categoryinfo')\
.map({ .<categoryinfo><pages> || 0,
.<title>.subst(/^'Category:'/, '') })\
.sort(-*[0])\
.map({ sprintf "%3d. %3d - %s", ++$, @$_ });
sub mediawiki-query ($site, $type, *%query) {
my $url = "$site/api.php?" ~ uri-query-string(
:action<query>, :format<json>, :gcmlimit<350>, :rawcontinue(), |%query);
my $continue = '';
my $client = HTTP::UserAgent.new;
gather loop {
my $response = $client.get("$url&$continue");
my $data = from-json($response.content);
take $_ for $data.<query>.{$type}.values;
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
}
}
sub uri-query-string (*%fields) {
%fields.map({ "{.key}={uri-encode .value}" }).join("&")
}
sub uri-encode ($str) {
$str.subst(/<[\x00..\xff]-[a..zA..Z0..9_.~-]>/, *.ord.fmt('%%%02X'), :g)
}