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