| 1 | package WWW::Wikipedia::Links; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use 5.008002; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use Mojo::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use Exporter qw/import/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | 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 | 4 | 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<wiki_links> 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 | 0 | ▶ | sub wiki_links { | ||||||||
|
Subroutine
wiki_links: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | _extract_from_dom(Mojo::UserAgent->new->get(shift)->res->dom); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 2 | ▶ | sub _extract_from_dom { | ||||||||
|
Subroutine
_extract_from_dom: called CC=6 SCAR=28.4
| |||||||||||
| 72 | 2 | my $dom = shift; | |||||||||
| 73 | |||||||||||
| 74 | 2 | my %res; | |||||||||
| 75 | |||||||||||
| 76 | # languages | ||||||||||
| 77 | { | ||||||||||
| 78 | 2 | my $langs = $dom->at('#p-lang'); | |||||||||
| 79 | 2 | ▶ | if ($langs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 1 | ▶ | $langs->find('li')->each( sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=32.5
| |||||||||||
| 81 | 1 | my $lang = (split '-', $_->attrs->{class})[1]; | |||||||||
| 82 | 0 | my $url = $_->at('a')->attrs->{href}; | |||||||||
| 83 | 0 | my $title = $_->at('a')->attrs->{title}; | |||||||||
| 84 | 0 | my %r; | |||||||||
| 85 | 0 | ▶ | $r{lang} = $lang if defined $lang; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | ▶ | $r{url} = $url if defined $url; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | ▶ | $r{title} = $title if defined $title; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | push @{ $res{translations} }, \%r if %r; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | } ); | |||||||||
| 90 | } | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # license | ||||||||||
| 94 | { | ||||||||||
| 95 | 2 | my $c = $dom->at('head link[rel="copyright"]'); | |||||||||
| 96 | 1 | ▶ | if ($c) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 1 | $res{license} = $c->attrs->{href}; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | # official homepage | ||||||||||
| 102 | { | ||||||||||
| 103 | 0 | my $vcard = $dom->at('table.vcard'); | |||||||||
| 104 | 0 | ▶ | if ($vcard) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | for ($vcard->find('tr')->each) { | |||||||||
| 106 | # TODO: make more robust against different languages | ||||||||||
| 107 | 0 | ▶ | if ($_->all_text =~ /Official website/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<< <moritz at faui2k3.org> >> | ||||||||||
| 121 | |||||||||||
| 122 | =head1 BUGS AND DEVELOPMENT | ||||||||||
| 123 | |||||||||||
| 124 | Please report any bugs or feature requests at | ||||||||||
| 125 | L<https://github.com/moritz/WWW-Wikipedia-Links/issues> | ||||||||||
| 126 | |||||||||||
| 127 | Development happens at github, you can seen (and modify) | ||||||||||
| 128 | the latest source code at L<https://github.com/moritz/WWW-Wikipedia-Links>. | ||||||||||
| 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<https://github.com/moritz/WWW-Wikipedia-Links/issues> | ||||||||||
| 144 | |||||||||||
| 145 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 146 | |||||||||||
| 147 | L<http://annocpan.org/dist/WWW-Wikipedia-Links> | ||||||||||
| 148 | |||||||||||
| 149 | =item * CPAN Ratings | ||||||||||
| 150 | |||||||||||
| 151 | L<http://cpanratings.perl.org/d/WWW-Wikipedia-Links> | ||||||||||
| 152 | |||||||||||
| 153 | =item * Search CPAN | ||||||||||
| 154 | |||||||||||
| 155 | L<http://search.cpan.org/dist/WWW-Wikipedia-Links/> | ||||||||||
| 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 | 4 | 1; # End of WWW::Wikipedia::Links | |||||||||