Line 0
Link Here
|
|
|
1 |
package C4::SocialData; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 2 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along with |
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
|
18 |
use Modern::Perl; |
19 |
use C4::Context; |
20 |
use Business::ISBN; |
21 |
use C4::Koha; |
22 |
|
23 |
=head2 get_data |
24 |
|
25 |
Get social data from a biblio |
26 |
|
27 |
params: |
28 |
$isbn = isbn of the biblio (it must be the same in your database, isbn given to babelio) |
29 |
|
30 |
returns: |
31 |
this function returns an hashref with keys |
32 |
|
33 |
isbn = isbn |
34 |
num_critics = number of critics |
35 |
num_critics_pro = number of profesionnal critics |
36 |
num_quotations = number of quotations |
37 |
num_videos = number of videos |
38 |
score_avg = average score |
39 |
num_scores = number of score |
40 |
=cut |
41 |
sub get_data { |
42 |
my ( $isbn ) = @_; |
43 |
my $dbh = C4::Context->dbh; |
44 |
my $sth = $dbh->prepare( qq{SELECT * FROM social_data WHERE isbn = ? LIMIT 1} ); |
45 |
$sth->execute( $isbn ); |
46 |
my $results = $sth->fetchrow_hashref; |
47 |
|
48 |
return $results; |
49 |
} |
50 |
|
51 |
=head 2 |
52 |
|
53 |
Update Social data |
54 |
|
55 |
params: |
56 |
$url = url containing csv file with data |
57 |
|
58 |
data separator : ; (semicolon) |
59 |
data order : isbn ; active ; critics number , critics pro number ; quotations number ; videos number ; average score ; scores number |
60 |
|
61 |
=cut |
62 |
sub update_data { |
63 |
my ( $output_filepath ) = @_; |
64 |
|
65 |
my $dbh = C4::Context->dbh; |
66 |
my $sth = $dbh->prepare( qq{INSERT INTO social_data ( |
67 |
`isbn`, `num_critics`, `num_critics_pro`, `num_quotations`, `num_videos`, `score_avg`, `num_scores` |
68 |
) VALUES ( ?, ?, ?, ?, ?, ?, ? ) |
69 |
ON DUPLICATE KEY UPDATE `num_critics`=?, `num_critics_pro`=?, `num_quotations`=?, `num_videos`=?, `score_avg`=?, `num_scores`=? |
70 |
} ); |
71 |
|
72 |
open( FILE, $output_filepath ) or die "File $output_filepath can not be read"; |
73 |
my $sep = qq{;}; |
74 |
my $i = 0; |
75 |
my $unknown = 0; |
76 |
while ( my $line = <FILE> ) { |
77 |
my ( $isbn, $active, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores ) = split $sep, $line; |
78 |
next if not $active; |
79 |
eval { |
80 |
$sth->execute( $isbn, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores, |
81 |
$num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores |
82 |
); |
83 |
}; |
84 |
if ( $@ ) { |
85 |
warn "Can't insert $isbn ($@)"; |
86 |
} else { |
87 |
$i++; |
88 |
} |
89 |
} |
90 |
say "$i data insered or updated"; |
91 |
} |
92 |
|
93 |
=head 2 |
94 |
|
95 |
Get social data report |
96 |
|
97 |
=cut |
98 |
sub get_report { |
99 |
my $dbh = C4::Context->dbh; |
100 |
|
101 |
my $sth = $dbh->prepare( qq{ |
102 |
SELECT biblionumber, isbn FROM biblioitems |
103 |
} ); |
104 |
$sth->execute; |
105 |
my %results; |
106 |
while ( my ( $biblionumber, $isbn ) = $sth->fetchrow() ) { |
107 |
push @{ $results{no_isbn} }, { biblionumber => $biblionumber } and next if not $isbn; |
108 |
my $original_isbn = $isbn; |
109 |
$isbn =~ s/^\s*(\S*)\s*$/$1/; |
110 |
$isbn = GetNormalizedISBN( $isbn, undef, undef ); |
111 |
$isbn = Business::ISBN->new( $isbn ); |
112 |
next if not $isbn; |
113 |
eval{ |
114 |
$isbn = $isbn->as_isbn13->as_string; |
115 |
}; |
116 |
next if $@; |
117 |
$isbn =~ s/-//g; |
118 |
my $social_datas = C4::SocialData::get_data( $isbn ); |
119 |
if ( $social_datas ) { |
120 |
push @{ $results{with} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; |
121 |
} else { |
122 |
push @{ $results{without} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; |
123 |
} |
124 |
} |
125 |
return \%results; |
126 |
} |
127 |
|
128 |
1; |
129 |
|