line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Wikipedia::LangTitles; |
2
|
1
|
|
|
1
|
|
12639
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
3
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
2
|
use Carp; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
103
|
|
5
|
|
|
|
|
|
|
require Exporter; |
6
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
7
|
|
|
|
|
|
|
our @EXPORT_OK = qw/get_wiki_titles make_wiki_url/; |
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
9
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
10
|
|
|
|
|
|
|
); |
11
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
534
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
29909
|
|
|
1
|
|
|
|
|
24
|
|
14
|
1
|
|
|
1
|
|
5
|
use URI::Escape 'uri_escape_utf8'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
83
|
|
15
|
1
|
|
|
1
|
|
425
|
use JSON::Parse 'parse_json'; |
|
1
|
|
|
|
|
673
|
|
|
1
|
|
|
|
|
404
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub make_wiki_url |
18
|
|
|
|
|
|
|
{ |
19
|
2
|
|
|
2
|
1
|
254
|
my ($name, $lang) = @_; |
20
|
2
|
100
|
|
|
|
4
|
if (! $lang) { |
21
|
|
|
|
|
|
|
# Defaults to English. |
22
|
1
|
|
|
|
|
2
|
$lang = 'en'; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
# Have to say "enwiki" or "jawiki" in the URL, since it can be |
25
|
|
|
|
|
|
|
# "enquote" or something. |
26
|
2
|
50
|
|
|
|
6
|
if ($lang !~ /wiki$/) { |
27
|
2
|
|
|
|
|
2
|
$lang .= 'wiki'; |
28
|
|
|
|
|
|
|
} |
29
|
2
|
|
|
|
|
2
|
my $safe_name = $name; |
30
|
2
|
|
|
|
|
4
|
$safe_name = uri_escape_utf8 ($safe_name); |
31
|
|
|
|
|
|
|
# The URL to get the information from. |
32
|
2
|
|
|
|
|
51
|
my $url = "https://www.wikidata.org/w/api.php?action=wbgetentities&sites=$lang&titles=$safe_name&props=sitelinks/urls|datatype&format=json"; |
33
|
2
|
|
|
|
|
5
|
return $url; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get_wiki_titles |
37
|
|
|
|
|
|
|
{ |
38
|
0
|
|
|
0
|
1
|
|
my ($name, %options) = @_; |
39
|
0
|
|
|
|
|
|
my $lang = $options{lang}; |
40
|
0
|
|
|
|
|
|
my $verbose = $options{verbose}; |
41
|
0
|
|
|
|
|
|
my $url = make_wiki_url ($name, $lang); |
42
|
0
|
0
|
|
|
|
|
if ($verbose) { |
43
|
0
|
|
|
|
|
|
print "Getting $name from '$url'.\n"; |
44
|
|
|
|
|
|
|
} |
45
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new (); |
46
|
|
|
|
|
|
|
# Tell the server from what software this request originates, in |
47
|
|
|
|
|
|
|
# case this module turns out to be problematic for them somehow. |
48
|
0
|
|
|
|
|
|
my $agent = __PACKAGE__; |
49
|
0
|
|
|
|
|
|
$ua = LWP::UserAgent->new (agent => $agent); |
50
|
0
|
|
|
|
|
|
$ua->default_header ( |
51
|
|
|
|
|
|
|
'Accept-Encoding' => scalar HTTP::Message::decodable() |
52
|
|
|
|
|
|
|
); |
53
|
0
|
|
|
|
|
|
my $response = $ua->get ($url); |
54
|
0
|
0
|
|
|
|
|
if (! $response->is_success ()) { |
55
|
0
|
|
|
|
|
|
carp "Get $url failed: " . $response->status_line (); |
56
|
0
|
|
|
|
|
|
return; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
0
|
|
|
|
|
if ($verbose) { |
59
|
0
|
|
|
|
|
|
print "$name data was retrieved successfully.\n"; |
60
|
|
|
|
|
|
|
} |
61
|
0
|
|
|
|
|
|
my $json = $response->decoded_content (); |
62
|
0
|
|
|
|
|
|
my $data = parse_json ($json); |
63
|
0
|
|
|
|
|
|
my $array = $data->{entities}; |
64
|
0
|
|
|
|
|
|
my %lang2title; |
65
|
0
|
|
|
|
|
|
for my $k (keys %$array) { |
66
|
0
|
|
|
|
|
|
my $sitelinks = $array->{$k}->{sitelinks}; |
67
|
0
|
|
|
|
|
|
for my $k (keys %$sitelinks) { |
68
|
0
|
|
|
|
|
|
my $lang = $k; |
69
|
|
|
|
|
|
|
# Reject these? This is a legacy of the script that this |
70
|
|
|
|
|
|
|
# used to be, it might be more useful for the CPAN module |
71
|
|
|
|
|
|
|
# not to reject these. |
72
|
0
|
0
|
|
|
|
|
if ($lang =~ /wikiversity|simple|commons|wikiquote|wikibooks/) { |
73
|
0
|
|
|
|
|
|
next; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
|
|
|
|
|
$lang =~ s/wiki$//; |
76
|
0
|
|
|
|
|
|
$lang2title{$lang} = $sitelinks->{$k}->{title}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
0
|
0
|
|
|
|
|
if ($verbose) { |
80
|
0
|
|
|
|
|
|
print "$name operations complete.\n"; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
|
|
|
|
|
return \%lang2title; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |