package GCPlugins::GCbooks::GCFnac;
###################################################
#
# Copyright 2005-2006 Tian
# Copyright 2015-2016 Kerenoc01 on Google Mail
#
# 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::GCPluginFnac;
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 'div') && ($attr->{class} eq "Article-itemInfo"))
{
$self->{isTitle} = 1;
$self->{isPublisher} = 0;
}
elsif (($self->{isTitle} eq 1) && ($tagname eq 'a') && ($attr->{class} eq " js-minifa-title"))
{
$self->{itemIdx}++;
$self->{itemsList}[$self->{itemIdx}]->{url} = $attr->{href};
$self->{isTitle} = 2;
}
elsif (($tagname eq 'p') && ($attr->{class} eq "Article-descSub"))
{
$self->{isAuthor} = 1;
}
elsif (($self->{isAuthor} eq 1) && ($tagname eq 'a'))
{
$self->{isAuthor} = 2;
}
elsif ($self->{isAuthor} && $tagname eq 'div')
{
$self->{isAuthor} = 0;
}
elsif (($tagname eq 'div') && ($attr->{class} eq "editorialInfo"))
{
$self->{isAnalyse} = 1;
}
}
else
{
if (($tagname eq 'h1') && ($attr->{class} eq 'ProductSummary-title'))
{
$self->{isTitle} = 1 ;
}
elsif (($self->{isTitle} eq 1) && ($tagname eq 'span') && ($attr->{itemprop} eq 'name'))
{
$self->{isTitle} = 2 ;
}
elsif (($tagname eq 'div') && ($attr->{class} eq 'ProductSummary-subTitle'))
{
$self->{isAuthor} = 1 ;
}
elsif (($self->{isAuthor} eq 1) && ($tagname eq 'a'))
{
$self->{isAuthor} = 2 ;
}
elsif (($tagname eq 'a') && ($attr->{class} eq 'expandimg') && ($self->{bigPics}))
{
$self->{curInfo}->{cover} = $attr->{href} ;
}
elsif (($tagname eq 'img') && ($attr->{class} eq 'js-ProductVisuals-imagePreview') && ((!$self->{bigPics}) || ($self->{curInfo}->{cover} eq '')))
{
$self->{curInfo}->{cover} = $attr->{src} ;
}
elsif ($tagname eq 'section' && $attr->{id} eq 'ficheResume')
{
$self->{isDescription} = 1 ;
}
elsif ($tagname eq 'div' && $attr->{class} eq 'productStrateTop')
{
$self->{isDescription} = 1 ;
}
elsif ($self->{isDescription} eq 1 && $tagname eq 'div' && $attr->{class} eq 'whiteContent')
{
$self->{isDescription} = 2 ;
}
elsif (($tagname eq 'ul') && ($attr->{class} =~ m/Feature-list/))
{
$self->{isAnalyse} = 1 ;
$self->{isDescription} = 0;
}
elsif (($tagname eq 'span') && ($attr->{class} =~ m/Feature-label/))
{
$self->{isAnalyse} = 2 ;
}
elsif (($self->{isAnalyse} eq 1) && ($attr->{class} =~ m/Feature-desc/))
{
$self->{isPublisher} = 2 if ($self->{isPublisher});
$self->{isPublication} = 2 if ($self->{isPublication});
$self->{isSerie} = 2 if $self->{isSerie};
$self->{isISBN} = 2 if $self->{isISBN};
$self->{isPage} = 2 if $self->{isPage};
}
}
}
sub end
{
my ($self, $tagname) = @_;
$self->{inside}->{$tagname}--;
if ($self->{isAnalyse} && $tagname eq 'div')
{
$self->{isAnalyse} = 0;
}
elsif ($self->{isAuthor} && $tagname eq 'div')
{
$self->{isAuthor} = 0;
}
elsif ($self->{isAnalyse} eq 2 && $tagname eq 'strong')
{
$self->{isAnalyse} = 1;
}
elsif ($self->{isDescription} eq 2 && $tagname eq 'div')
{
# parfois des descriptions en double : resume + le mot de l'editeur
# meme contenu avec une orthographe et une mise en page différente!
$self->{isDescription} = 0;
}
}
sub text
{
my ($self, $origtext) = @_;
if ($self->{parsingList})
{
if ($self->{isTitle} eq 2)
{
# Enleve les blancs en debut de chaine
$origtext =~ s/^\s+//;
# Enleve les blancs en fin de chaine
$origtext =~ s/\s+$//g;
if (($self->{itemsList}[$self->{itemIdx}]->{title} eq '') && ($origtext ne ''))
{
$self->{itemsList}[$self->{itemIdx}]->{title} = $origtext;
}
elsif ($origtext ne '')
{
$self->{itemsList}[$self->{itemIdx}]->{title} .= ' - ';
$self->{itemsList}[$self->{itemIdx}]->{title} .= $origtext;
}
$self->{isTitle} = 0 ;
}
elsif ($self->{isAnalyse} > 0)
{
my @listInfo = split(/\n/, $origtext);
my $nbInfos = scalar @listInfo ;
if ($nbInfos eq 1)
{
return;
}
else
{
my $publication = $listInfo[$nbInfos-1];
$publication =~ s/^[-\s]+//;
$publication =~ s/\s+$//g;
$self->{itemsList}[$self->{itemIdx}]->{publication} = $publication;
my $edition = $listInfo[$nbInfos-2];
$edition =~ s/^[-\s]+//;
$edition =~ s/\s+$//g;
$self->{itemsList}[$self->{itemIdx}]->{edition} = $edition;
}
}
elsif ($self->{isAuthor} eq 2)
{
$origtext =~ s/^\s+//;
$origtext =~ s/\s+$//g;
if (($self->{itemsList}[$self->{itemIdx}]->{authors} eq '') && ($origtext ne ''))
{
$self->{itemsList}[$self->{itemIdx}]->{authors} = $origtext;
}
elsif ($origtext ne '')
{
$self->{itemsList}[$self->{itemIdx}]->{authors} .= ', ';
$self->{itemsList}[$self->{itemIdx}]->{authors} .= $origtext;
}
$self->{isAuthor} = 1;
}
}
else
{
# Enleve les blancs en debut de chaine
$origtext =~ s/^\s+//;
# Enleve les blancs en fin de chaine
$origtext =~ s/\s+$//g;
if ($self->{isTitle} eq '2')
{
$self->{curInfo}->{title} = $origtext;
$self->{isTitle} = 0 ;
}
elsif ($self->{isAnalyse} eq 2)
{
$self->{isISBN} = 1 if ($origtext =~ m/ISBN/i);
$self->{isPublisher} = 1 if ($origtext =~ m/Editeur/i);
$self->{isFormat} = 1 if ($origtext =~ m/Format/i);
$self->{isSerie} = 1 if ($origtext =~ m/Collection/i);
$self->{isPublication} = 1 if ($origtext =~ m/Date de parution/i);
$self->{isPage} = 1 if ($origtext =~ m/pages/i);
$self->{isTranslator} = 1 if ($origtext =~ m/Traduction/i);
$self->{isAnalyse} = 1 ;
}
elsif ($self->{isAuthor} eq 2)
{
# Enleve les virgules
$origtext =~ s/,//;
if ($origtext ne '')
{
$self->{author} = $origtext;
}
$self->{isAuthor} = 1;
}
elsif ($self->{isAuthor} eq 1)
{
if ($origtext =~ m/\(Traduct/)
{
$self->{curInfo}->{translator} = $origtext;
}
elsif ($origtext =~ m/^\(/)
{
$self->{curInfo}->{authors} .= $self->{author};
$self->{curInfo}->{authors} .= ", ";
}
}
elsif ($self->{isISBN} eq 2)
{
$self->{curInfo}->{isbn} = $origtext;
$self->{isISBN} = 0 ;
}
elsif ($self->{isPublisher} eq 2)
{
if ($origtext ne '')
{
$self->{curInfo}->{publisher} = $origtext;
$self->{isPublisher} = 0 ;
}
}
elsif ($self->{isFormat} eq 2)
{
if ($origtext ne '')
{
$self->{curInfo}->{format} = $origtext;
$self->{isFormat} = 0 ;
}
}
elsif ($self->{isSerie} eq 2)
{
if ($origtext ne '')
{
$self->{curInfo}->{serie} = $origtext;
$self->{isSerie} = 0 ;
}
}
elsif ($self->{isPublication} eq 2)
{
$self->{curInfo}->{publication} = $self->decodeDate($origtext)
if (!$self->{curInfo}->{publication});
$self->{isPublication} = 0 ;
}
elsif (($self->{isPage} eq 2))
{
if ($origtext ne '')
{
$self->{curInfo}->{pages} = $origtext;
$self->{isPage} = 0 ;
}
}
elsif ($self->{isTranslator})
{
if ($origtext ne '')
{
$self->{curInfo}->{translator} = $origtext;
$self->{isTranslator} = 0 ;
}
}
elsif ($self->{isDescription} eq 2)
{
$origtext .= "\n";
$self->{curInfo}->{description} .= $origtext;
}
}
}
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 => 0,
edition => 1,
serie => 0,
};
$self->{isTitle} = 0;
$self->{isAuthor} = 0;
$self->{isPublisher} = 0;
$self->{isISBN} = 0;
$self->{isPublication} = 0;
$self->{isFormat} = 0;
$self->{isSerie} = 0;
$self->{isPage} = 0;
$self->{isDescription} = 0;
$self->{isTranslator} = 0;
return $self;
}
sub preProcess
{
my ($self, $html) = @_;
if ($self->{parsingList})
{
$html =~ s|
||gmi;
}
else
{
# Le descriptif pouvant contenir des balises html je le repere maintenant
my $found = index($html,"Mot de l'");
if ( $found >= 0 )
{
my $html2 = substr($html, $found +length('Mot de l\''),length($html)- $found -length('Mot de l\''));
my $found2 = index($html2,"= 0 )
{
$html3 = substr($html2, $found2 +length('");
if ( $found2 >= 0 )
{
$html2 = substr($html2, $found2 +length('
'),length($html2)- $found2 -length(''));
}
$html2 =~ s||\n* |gi;
$html2 =~ s|
|\n|gi;
$html2 =~ s|
|\n|gi;
$html2 =~ s|||gi;
$html2 =~ s|||gi;
$html2 =~ s|||gi;
$html2 =~ s|||gi;
$html2 =~ s||\n|gi;
$html2 =~ s|
||gi;
$html2 =~ s|||gi;
$html2 =~ s|\x{92}|'|g;
$html2 =~ s||'|gi;
$html2 =~ s||*|gi;
$html2 =~ s|
|...|gi;
$html2 =~ s|\x{85}|...|gi;
$html2 =~ s|\x{8C}|OE|gi;
$html2 =~ s|\x{9C}|oe|gi;
}
}
return $html;
}
sub getSearchUrl
{
my ($self, $word) = @_;
return "http://www3.fnac.com/search/quick.do?filter=-3&text=". $word ."&category=book";
}
sub getItemUrl
{
my ($self, $url) = @_;
return $url if $url;
return 'http://www.fnac.com/';
}
sub getName
{
return "Fnac (FR)";
}
sub getCharset
{
my $self = shift;
return "ISO-8859-15";
}
sub getAuthor
{
return 'TPF - Kerenoc';
}
sub getLang
{
return 'FR';
}
sub getSearchFieldsArray
{
return ['isbn', 'title'];
}
sub decodeDate
{
my ($self, $date) = @_;
# date déjà dans le bon format
return $date if ($date =~ m|/|);
# date à convertir au format jour/mois/année
my @dateItems = split(/\s/, $date);
my @listeMois = ("janvier","février","mars","avril","mai","juin",
"juillet","août","septmbre","octobre","novembre","décembre");
my $mois = 0;
while ($mois < (scalar @listeMois) && $dateItems[(scalar @dateItems)-2] ne $listeMois[$mois])
{
$mois++;
}
return "01/".sprintf("%02d",$mois)."/".$dateItems[1];
}
}
1;