1 |
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::Rules; |
20 |
use Data::Dump qw/dump/; |
21 |
use File::Slurp; |
22 |
use JSON::XS; |
23 |
|
24 |
sub vocabulary { |
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 $rules = XML::Rules->new( rules => [ |
57 |
_default => sub { |
58 |
my ($tag_name, $tag_hash, $context, $parent_data) = @_; |
59 |
warn "_default $tag_name ",dump( $tag_hash ); |
60 |
return; |
61 |
}, |
62 |
vocabulary => 'no content', |
63 |
vocabularyentry => sub { |
64 |
die "no key attribute?" unless defined( $_[1]->{key} ); |
65 |
'@' . $_[0] => $_[1]->{key}; |
66 |
}, |
67 |
]); |
68 |
$data = $rules->parsefile( $xml_path ) |
69 |
|| die "can't parse $xml_path: $!"; |
70 |
|
71 |
$data = $data->{vocabulary} || die "no vocabulary in data: ", dump( $data ); |
72 |
|
73 |
write_file( $data_path, encode_json( $data )) || die "can't save into $data_path: $!"; |
74 |
Jifty->log->info("saved dump into $data_path"); |
75 |
} else { |
76 |
$data = decode_json( read_file( $data_path ) ) || die "can't read $data_path: $!"; |
77 |
Jifty->log->info("loaded dump from $data_path"); |
78 |
} |
79 |
|
80 |
warn '## ',ref($data),' = ',dump($data); |
81 |
|
82 |
die "data not for for $name" unless $data->{name} eq $name; |
83 |
warn "no vocabularyentry for $name from $data_path" unless defined( $data->{vocabularyentry} ); |
84 |
|
85 |
return $data->{vocabularyentry}; |
86 |
} |
87 |
|
88 |
warn "# AAUEduHr loaded\n"; |
89 |
|
90 |
1; |