package GCPlugins::GCbooks::GCLeLivre;
###################################################
#
# Copyright 2005-2006 Tian
# Copyright 2016 Varkolak
#
# This file is part of GCstar.
#
# GCstar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GCstar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCstar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###################################################
use strict;
use utf8;
use GCPlugins::GCbooks::GCbooksCommon;
{
package GCPlugins::GCbooks::GCPluginLeLivre;
use base qw(GCPlugins::GCbooks::GCbooksPluginsBase);
use URI::Escape;
sub start
{
my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
$self->{inside}->{$tagname}++;
if ($self->{parsingList})
{
if ($tagname eq 'book')
{
$self->{itemIdx}++;
$self->{isTitle} = 1;
}
elsif ($self->{isTitle})
{
$self->{itemsList}[$self->{itemIdx}]->{url} = $attr->{href};
$self->{itemsList}[$self->{itemIdx}]->{title} = $attr->{title};
$self->{isTitle} = 0;
}
elsif ($tagname eq 'auth')
{
$self->{isAuthor} = 1;
}
elsif ($tagname eq 'edito')
{
$self->{isPublisher} = 1;
}
}
else
{
if ($tagname eq 'image')
{
$self->{isImage} = 1;
}
elsif (($tagname eq 'a') && ($self->{isImage}))
{
$self->{curInfo}->{cover} = 'http://www.le-livre.fr/' . $attr->{href};
$self->{isImage} = 0;
}
elsif ($tagname eq 'titre')
{
$self->{isTitle} = 1;
}
elsif ($tagname eq 'isbn')
{
$self->{isISBN} = 1;
}
elsif ($tagname eq 'commentaires')
{
$self->{isComm} = 1;
}
elsif ($tagname eq 'auteur')
{
$self->{isAuthor} = 1;
}
elsif ($tagname eq 'divers')
{
$self->{isFormat} = 1;
}
}
}
sub end
{
my ($self, $tagname) = @_;
$self->{inside}->{$tagname}--;
}
sub text
{
my ($self, $origtext) = @_;
$origtext =~ s/^\s+//;
$origtext =~ s/\s+$//g;
if ($self->{parsingList})
{
if (($self->{isAuthor}) && ($origtext ne ''))
{
$self->{itemsList}[$self->{itemIdx}]->{authors} = $origtext;
$self->{isAuthor} = 0 ;
}
elsif (($self->{isPublisher}) && ($origtext ne ''))
{
my @array = split(/\./,$origtext);
$self->{itemsList}[$self->{itemIdx}]->{edition} = $array[0];
$self->{itemsList}[$self->{itemIdx}]->{publication} = $array[1];
$self->{itemsList}[$self->{itemIdx}]->{format} = $array[2];
$self->{isPublisher} = 0 ;
}
}
else
{
if ($self->{isTitle})
{
$self->{curInfo}->{title} = $origtext;
$self->{isTitle} = 0;
}
elsif ($self->{isISBN})
{
$self->{curInfo}->{isbn} = $origtext;
$self->{isISBN} = 0;
}
elsif (($self->{isComm}) && ($origtext ne ''))
{
$self->{curInfo}->{description} = $origtext;
my @array = split(/[\.*]/,$origtext);
for my $i (0 .. $#array)
{
if ($array[$i] =~ s/.*[0-9]-//i)
{
#$array[$i] =~ s/.*-//i;
$array[$i] =~ s/^\s+//;
$self->{curInfo}->{genre} = $array[$i];
}
}
$self->{isComm} = 0 ;
}
elsif ($self->{isAuthor})
{
my @array = split(/-/,$origtext);
$array[0] =~ s/([\w']+)/\u\L$1/g;
$self->{curInfo}->{authors} = $array[0];
for my $i (1 .. $#array)
{
$array[$i] =~ s/([\w']+)/\u\L$1/g;
$self->{curInfo}->{authors} .= ", " . $array[$i];
}
$self->{isAuthor} = 0 ;
}
elsif (($self->{isFormat}) && ($origtext ne ''))
{
my @array = split(/[\.]/,$origtext);
$array[0] =~ s/([\w']+)/\u\L$1/g;
$self->{curInfo}->{publisher} = $array[0];
$array[1] =~ s/^\s+//;
$self->{curInfo}->{publication} = $array[1];
$array[2] =~ s/^\s+//;
$self->{curInfo}->{format} = $array[2];
for my $i (3 .. $#array)
{
if ($array[$i] =~ /pages/i)
{
$array[$i] =~ s/ pages.*//i;
$array[$i] =~ s/^\s+//;
$self->{curInfo}->{pages} = $array[$i];
}
}
$self->{isFormat} = 0 ;
}
}
}
sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new();
bless ($self, $class);
$self->{hasField} = {
title => 1,
authors => 1,
publication => 1,
format => 1,
edition => 1,
serie => 0,
};
$self->{isImage} = 0;
$self->{isTitle} = 0;
$self->{isAuthor} = 0;
$self->{isPublisher} = 0;
$self->{isISBN} = 0;
$self->{isFormat} = 0;
$self->{isTranslator} = 0;
$self->{isComm} = 0;
return $self;
}
sub preProcess
{
my ($self, $html) = @_;
if ($self->{parsingList})
{
$html =~ s|<td class="illustration" alt="">|<book>|gi;
$html =~ s|<td class="auteur">|<auth>|gi;
$html =~ s|"ref=|"|gi;
$html =~ s|<td class="caracteristique">|<edito>|gi;
$html =~ s|<b>||gi;
$html =~ s|</b>||gi;
}
else
{
$html =~ s|<div class="watermark">|<image>|i;
$html =~ s|<h1 class="FicheTitre" itemprop="name">|<titre>|i;
$html =~ s|<span class="FicheDetailISBN" >|<isbn>|i;
$html =~ s|<span class="FicheDetailAuteur" >|<auteur>|i;
$html =~ s|<h2 class="TitreFicheDesciption"> Description </h2>|<infos>|i;
$html =~ s|<h2 class="TitreFicheDesciption"> Informations Supplémentaires </h2>|<commentaires>|i;
$html =~ s|<br /> <br />|<divers>|i;
$html =~ s|\x{92}|'|g;
$html =~ s|’|'|gi;
$html =~ s|•|*|gi;
$html =~ s|…|...|gi;
$html =~ s|\x{85}|...|gi;
$html =~ s|\x{8C}|OE|gi;
$html =~ s|\x{9C}|oe|gi;
}
return $html;
}
sub getSearchUrl
{
my ($self, $word) = @_;
return "http://www.le-livre.fr/default.asp?Rech=1&Submit_Rech_Rapide=1&rechercherap=". $word;
}
sub getItemUrl
{
my ($self, $url) = @_;
return $url;
}
sub getName
{
return "Le-Livre";
}
sub getCharset
{
my $self = shift;
return "ISO-8859-15";
}
sub getAuthor
{
return 'Varkolak';
}
sub getLang
{
return 'FR';
}
sub getSearchFieldsArray
{
return ['ISBN', 'title', 'author', 'publication'];
}
}
1;