File Coverage

blib/lib/WWW/Wikipedia/Links.pm
Criterion Covered Total %
statement 26 43 60.4
branch 3 16 18.7
condition n/a
subroutine 7 8 87.5
pod 1 1 100.0
total 37 68 54.4


line stmt bran cond sub pod time code
1             package WWW::Wikipedia::Links;
2              
3 3     3   96229 use 5.008002;
  3         11  
  3         204  
4 3     3   19 use strict;
  3         7  
  3         114  
5 3     3   16 use warnings;
  3         11  
  3         106  
6 3     3   2877 use Mojo::UserAgent;
  3         1133052  
  3         51  
7 3     3   131 use Exporter qw/import/;
  3         7  
  3         1602  
8             our @EXPORT_OK = qw/wiki_links/;
9              
10             =head1 NAME
11              
12             WWW::Wikipedia::Links - Extract links from Wikipedia pages
13              
14             =head1 VERSION
15              
16             Version 0.01
17              
18             =cut
19              
20             our $VERSION = '0.02';
21              
22              
23             =head1 SYNOPSIS
24              
25             Quick summary of what the module does.
26              
27             Perhaps a little code snippet.
28              
29             use WWW::Wikipedia::Links qw/wiki_links/;
30             my $links = wiki_links 'http://de.wikipedia.org/wiki/Ralf_Isau';
31             if ($links) {
32             for (@{ $links->{translations} }) {
33             print $_->{lang}, ' ', $_->{url}, "\n";
34             }
35             }
36              
37             =head1 EXPORT
38              
39             Function C is be exported if requested.
40              
41             =head1 SUBROUTINES
42              
43             =head2 wiki_links
44              
45             my $res = wiki_links $wiki_url;
46              
47             Returns a hash ref of the following structure. Any fields may be missinf
48             if the information could not be extracted.
49            
50             my $res = {
51             license => $link_to_license_page,
52             official_website => $official_website,
53             translations => [
54             {
55             lang => $wiki_language_code,
56             url => $translation_url,
57             title => $title_of_translation_page,
58             },
59             ...
60             ],
61             };
62              
63             The behavior in the case of an error is not yet defined.
64              
65             =cut
66              
67             sub wiki_links {
68 0     0 1 0 _extract_from_dom(Mojo::UserAgent->new->get(shift)->res->dom);
69             }
70              
71             sub _extract_from_dom {
72 2     2   98769 my $dom = shift;
73              
74 2         7 my %res;
75              
76             # languages
77             {
78 2         4 my $langs = $dom->at('#p-lang');
  2         19  
79 2 100       33696 if ($langs) {
80             $langs->find('li')->each( sub {
81 1     1   1364 my $lang = (split '-', $_->attrs->{class})[1];
82 0         0 my $url = $_->at('a')->attrs->{href};
83 0         0 my $title = $_->at('a')->attrs->{title};
84 0         0 my %r;
85 0 0       0 $r{lang} = $lang if defined $lang;
86 0 0       0 $r{url} = $url if defined $url;
87 0 0       0 $r{title} = $title if defined $title;
88 0 0       0 push @{ $res{translations} }, \%r if %r;
  0         0  
89 1         17 } );
90             }
91             }
92              
93             # license
94             {
95 1         3 my $c = $dom->at('head link[rel="copyright"]');
  1         3  
96 1 50       880 if ($c) {
97 1         16 $res{license} = $c->attrs->{href};
98             }
99             }
100              
101             # official homepage
102             {
103 0           my $vcard = $dom->at('table.vcard');
  0            
104 0 0         if ($vcard) {
105 0           for ($vcard->find('tr')->each) {
106             # TODO: make more robust against different languages
107 0 0         if ($_->all_text =~ /Official website/) {
108 0           $res{official_website} = $_->at('a')->attrs->{href};
109 0           last;
110             }
111             }
112             }
113             }
114              
115 0           return \%res;
116              
117             }
118             =head1 AUTHOR
119              
120             Moritz Lenz, C<< >>
121              
122             =head1 BUGS AND DEVELOPMENT
123              
124             Please report any bugs or feature requests at
125             L
126              
127             Development happens at github, you can seen (and modify)
128             the latest source code at L.
129              
130             =head1 SUPPORT
131              
132             You can find documentation for this module with the perldoc command.
133              
134             perldoc WWW::Wikipedia::Links
135              
136              
137             You can also look for information at:
138              
139             =over 4
140              
141             =item * Bug tracker (report bugs here)
142              
143             L
144              
145             =item * AnnoCPAN: Annotated CPAN documentation
146              
147             L
148              
149             =item * CPAN Ratings
150              
151             L
152              
153             =item * Search CPAN
154              
155             L
156              
157             =back
158              
159              
160             =head1 ACKNOWLEDGEMENTS
161              
162              
163             =head1 LICENSE AND COPYRIGHT
164              
165             Copyright 2011 Moritz Lenz.
166              
167             This program is free software; you can redistribute it and/or modify it
168             under the terms of either: the GNU General Public License as published
169             by the Free Software Foundation; or the Artistic License.
170              
171             See http://dev.perl.org/licenses/ for more information.
172              
173              
174             =cut
175              
176             1; # End of WWW::Wikipedia::Links