| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package EPFL::Sciper::List; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 144449 | use 5.006; | 
|  | 3 |  |  |  |  | 27 |  | 
| 4 | 3 |  |  | 3 |  | 16 | use strict; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 74 |  | 
| 5 | 3 |  |  | 3 |  | 14 | use warnings; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 97 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 3 |  |  | 3 |  | 1929 | use JSON; | 
|  | 3 |  |  |  |  | 31991 |  | 
|  | 3 |  |  |  |  | 14 |  | 
| 8 | 3 |  |  | 3 |  | 2012 | use Readonly; | 
|  | 3 |  |  |  |  | 11337 |  | 
|  | 3 |  |  |  |  | 181 |  | 
| 9 | 3 |  |  | 3 |  | 2113 | use LWP::UserAgent; | 
|  | 3 |  |  |  |  | 139933 |  | 
|  | 3 |  |  |  |  | 174 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | EPFL::Sciper::List - Retrieve a list of all public active sciper from EPFL. | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 VERSION | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | Version 1.02 | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =cut | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | our $VERSION = '1.02'; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | Retrieve sciper from EPFL | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | use EPFL::Sciper::List qw/retrieveSciper toJson toTsv/; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my @listPersons = retrieveSciper(); | 
| 30 |  |  |  |  |  |  | print toJson(@listPersons); | 
| 31 |  |  |  |  |  |  | print toTsv(@listPersons); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | Via the command line epfl-sciper-list | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | A simple module to retrieve a list of all public active sciper from EPFL. | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =cut | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 3 |  |  | 3 |  | 33 | use base 'Exporter'; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1853 |  | 
| 42 |  |  |  |  |  |  | our @EXPORT_OK = | 
| 43 |  |  |  |  |  |  | qw/p_createUserAgent p_getUrl p_buildUrl retrieveSciper toJson toTsv/; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Readonly::Scalar my $TIMEOUT => 1200; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | Readonly::Scalar my $MAXREDIRECT => 10; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | Readonly::Scalar my $AUTOCOMPLETE_URL => | 
| 50 |  |  |  |  |  |  | 'https://search.epfl.ch/json/autocompletename.action?maxRows=99999999&term='; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head1 SUBROUTINES/METHODS | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | =head2 retrieveSciper( ) | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | Return a list of persons from EPFL with information like: | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | (sciper => 999999, firstname => 'Taylor', name => 'Swift'); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =cut | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub retrieveSciper { | 
| 63 | 2 |  |  | 2 | 1 | 16659 | my @listPersons = (); | 
| 64 | 2 |  |  |  |  | 17 | my @alphabet    = ( 'a' .. 'z' ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 |  |  |  |  | 7 | my $ua = p_createUserAgent(); | 
| 67 | 2 |  |  |  |  | 6 | foreach my $letter (@alphabet) { | 
| 68 | 52 |  |  |  |  | 388 | my $response = p_getUrl( $ua, p_buildUrl($letter) ); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 52 | 100 |  |  |  | 96002 | if ( $response->is_success ) { | 
| 71 | 26 |  |  |  |  | 233 | my $struct = from_json( $response->decoded_content ); | 
| 72 | 26 |  |  |  |  | 3917 | push @listPersons, @{ $struct->{result} }; | 
|  | 26 |  |  |  |  | 236 |  | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 2 |  |  |  |  | 16 | my %hash = (); | 
| 77 | 2 |  |  |  |  | 6 | foreach my $per (@listPersons) { | 
| 78 | 65 |  |  |  |  | 110 | $hash{ $per->{sciper} } = $per; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 2 |  |  |  |  | 8 | @listPersons = (); | 
| 82 | 2 |  |  |  |  | 13 | foreach my $sciper ( sort { $a <=> $b } keys %hash ) { | 
|  | 300 |  |  |  |  | 369 |  | 
| 83 | 62 |  |  |  |  | 94 | push @listPersons, $hash{$sciper}; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 2 |  |  |  |  | 93 | return @listPersons; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head2 toJson | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Return sciper list in JSON | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =cut | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub toJson { | 
| 96 | 1 |  |  | 1 | 1 | 3512 | my @list = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 1 |  |  |  |  | 17 | my $json = JSON->new->allow_nonref; | 
| 99 | 1 |  |  |  |  | 85 | return $json->pretty->encode( \@list ); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head2 toTsv | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | Return sciper list in TSV | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =cut | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub toTsv { | 
| 109 | 1 |  |  | 1 | 1 | 47387 | my @list = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 1 |  |  |  |  | 3 | my $output = q{}; | 
| 112 | 1 |  |  |  |  | 3 | foreach my $per (@list) { | 
| 113 |  |  |  |  |  |  | $output .= | 
| 114 | 62 |  |  |  |  | 126 | $per->{sciper} . "\t" . $per->{firstname} . "\t" . $per->{name} . "\n"; | 
| 115 |  |  |  |  |  |  | } | 
| 116 | 1 |  |  |  |  | 26 | return $output; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 PRIVATE SUBROUTINES/METHODS | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head2 p_createUserAgent | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Return a LWP::UserAgent. | 
| 124 |  |  |  |  |  |  | LWP::UserAgent objects can be used to dispatch web requests. | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =cut | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | sub p_createUserAgent { | 
| 129 | 3 |  |  | 3 | 1 | 13568 | my $ua = LWP::UserAgent->new; | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 3 |  |  |  |  | 6193 | $ua->timeout($TIMEOUT); | 
| 132 | 3 |  |  |  |  | 57 | $ua->agent('DevRunBot'); | 
| 133 | 3 |  |  |  |  | 200 | $ua->env_proxy; | 
| 134 | 3 |  |  |  |  | 28991 | $ua->max_redirect($MAXREDIRECT); | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 3 |  |  |  |  | 38 | return $ua; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =head2 p_getUrl | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | Dispatch a GET request on the given $url | 
| 142 |  |  |  |  |  |  | The return value is a response object. See HTTP::Response for a description | 
| 143 |  |  |  |  |  |  | of the interface it provides. | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =cut | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | sub p_getUrl { | 
| 148 | 54 |  |  | 54 | 1 | 33856 | my ( $ua, $url ) = @_; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 54 |  |  |  |  | 167 | return $ua->get($url); | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head2 p_buildUrl | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Return the autocomplete url to retrieve sciper. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =cut | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | sub p_buildUrl { | 
| 160 | 1 |  |  | 1 | 1 | 85 | my $letter = shift; | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 1 |  |  |  |  | 9 | return $AUTOCOMPLETE_URL . $letter; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head1 AUTHOR | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | William Belle, C<<  >> | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =head1 BUGS AND LIMITATIONS | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Please report any bugs or feature requests here L. | 
| 172 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on | 
| 173 |  |  |  |  |  |  | your bug as I make changes. | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =head1 SUPPORT | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | perldoc EPFL::Sciper::List | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | You can also look for information at: | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =over 4 | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | L | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | L | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =item * Search CPAN | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | L | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | =back | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | Original work Copyright ECOLE POLYTECHNIQUE FEDERALE DE LAUSANNE, | 
| 202 |  |  |  |  |  |  | Switzerland, VPSI, 2017-2018. | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | Modified work Copyright William Belle, 2018. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | Licensed under the Apache License, Version 2.0 (the "License"); | 
| 207 |  |  |  |  |  |  | you may not use this file except in compliance with the License. | 
| 208 |  |  |  |  |  |  | You may obtain a copy of the License at | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | L | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | Unless required by applicable law or agreed to in writing, software | 
| 213 |  |  |  |  |  |  | distributed under the License is distributed on an "AS IS" BASIS, | 
| 214 |  |  |  |  |  |  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 215 |  |  |  |  |  |  | See the License for the specific language governing permissions and | 
| 216 |  |  |  |  |  |  | limitations under the License. | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | =cut | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | 1;    # End of EPFL::Sciper::List |