1 |
dpavlin |
88 |
package A3C::AAIEduHr; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use warnings; |
5 |
|
|
|
6 |
|
|
=head1 NAME |
7 |
|
|
|
8 |
|
|
AAAIEduHr |
9 |
|
|
|
10 |
|
|
=head1 DESCRIPTION |
11 |
|
|
|
12 |
|
|
Pull varios stuff from XML at L<http://shema.aaiedu.hr/> and cache it as |
13 |
|
|
JSON on disk |
14 |
|
|
|
15 |
|
|
=cut |
16 |
|
|
|
17 |
|
|
#use base 'Jifty::Object'; |
18 |
|
|
|
19 |
|
|
use XML::Simple; |
20 |
|
|
use Data::Dump qw/dump/; |
21 |
|
|
use File::Slurp; |
22 |
|
|
use JSON::XS; |
23 |
|
|
|
24 |
|
|
sub available { |
25 |
|
|
my $self = shift; |
26 |
|
|
|
27 |
|
|
my $name = shift; |
28 |
|
|
die "invalid format of name" unless $name =~ /^hrEdu\d\d\d$/; |
29 |
|
|
|
30 |
|
|
my $url = "http://shema.aaiedu.hr/aosi/$name.xml"; |
31 |
|
|
my $xml_path = Jifty::Util->absolute_path( "var/$name.xml" ); |
32 |
|
|
my $data_path = Jifty::Util->absolute_path( "var/$name.js" ); |
33 |
|
|
|
34 |
|
|
# sanity cleanup |
35 |
|
|
unlink $xml_path if -s $xml_path == 0; |
36 |
|
|
unlink $data_path if -s $data_path == 0; |
37 |
|
|
|
38 |
|
|
# FIXME expire on some other way than rm? |
39 |
|
|
if ( ! -e $xml_path ) { |
40 |
|
|
|
41 |
|
|
my $ua = LWP::UserAgent->new; |
42 |
|
|
my $res = $ua->get( $url ); |
43 |
|
|
|
44 |
|
|
die "can't get $url status: ", $res->status_line |
45 |
|
|
unless $res->is_success; |
46 |
|
|
|
47 |
|
|
write_file( $xml_path, $res->content ); |
48 |
|
|
Jifty->log->info("saved $url into $xml_path"); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
die "can't find xml $xml_path: $!" unless -s $xml_path; |
52 |
|
|
|
53 |
|
|
my $data; |
54 |
|
|
|
55 |
|
|
if ( ! -e $data_path ) { |
56 |
|
|
my $xs = XML::Simple->new; |
57 |
|
|
$data = $xs->XMLin($xml_path, |
58 |
|
|
# KeyAttr => { vocabularyentry => 'key' }, |
59 |
|
|
ContentKey => '-content', |
60 |
|
|
) || die "can't parse $xml_path: $!"; |
61 |
|
|
|
62 |
|
|
write_file( $data_path, encode_json( $data )) || die "can't save into $data_path: $!"; |
63 |
|
|
Jifty->log->info("saved dump into $data_path"); |
64 |
|
|
} else { |
65 |
|
|
$data = decode_json( read_file( $data_path ) ) || die "can't read $data_path: $!"; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
warn '## ',ref($data),' = ',dump($data); |
69 |
|
|
|
70 |
|
|
die "data not for for $name" unless $data->{name} eq $name; |
71 |
|
|
|
72 |
|
|
return $data; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
warn "# AAUEduHr loaded\n"; |
76 |
|
|
|
77 |
|
|
1; |