package GCPlugins::GCbooks::GCChapitre; ################################################### # # Copyright 2005-2006 Tian # Copyright 2015-2016 Kereno01 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::GCPluginChapitre; 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 'a') && ( $attr->{id} =~ m/_searchProductDisplay_hlProductTitle/)) { $self->{itemIdx}++; $self->{itemsList}[$self->{itemIdx}]->{url} = "http://www.chapitre.com" . $attr->{href}; $self->{isTitle} = 1 ; } elsif ($tagname eq 'em') { $self->{isAuthor} = 1 ; } elsif (($tagname eq 'span') && ( $attr->{class} eq 'editeur')) { $self->{isPublisher} = 1; } elsif (($tagname eq 'span') && ( $attr->{class} eq 'dateParution')) { $self->{isPublication} = 1; } } else { if (($tagname eq 'div') && ($attr->{class} eq 'clear')) { $self->{isDescription} = 0 ; } elsif (($tagname eq 'h1') && ( $attr->{class} eq 'ProductSummary-title')) { $self->{isTitle} = 1 ; } elsif (($tagname eq 'h1') && ( $attr->{class} eq 'product-title')) { $self->{isTitle} = 1 ; } elsif (($tagname eq 'div') && ( $attr->{id} eq 'ctl00_PHCenter_ProductFile1_ProductTitle1_pnlTranslator')) { $self->{isTranslator} = 1 ; } elsif (($tagname eq 'tpftraducteurtpf') && ( $self->{isTranslator} eq 1)) { $self->{isTranslator} = 2 ; } elsif (($tagname eq 'img') && ( $attr->{itemprop} eq 'image') && ( index($attr->{src},"http://images.chapitre.com/indispo") eq -1 )) { $self->{curInfo}->{cover} = $attr->{src} if ($self->{curInfo}->{cover} eq ""); } elsif (($tagname eq 'div') && ($attr->{itemprop} eq 'description')) { $self->{isDescription} = 1 ; } elsif (($tagname eq 'tpfdescriptiontpf') && ($self->{isDescription} eq 1)) { $self->{isDescription} = 2 ; } elsif (($tagname eq 'span') && ($self->{isDescription} eq 1)) { $self->{isDescription} = 2 ; } elsif (($tagname eq 'a') && ( $attr->{href} =~ m|/CHAPITRE/fr/search/Default.aspx\?collection=|i)) { $self->{isCollection} = 1 ; } elsif (($tagname eq 'a') && ( $attr->{href} =~ m|/CHAPITRE/fr/search/Default.aspx\?editeur=|i)) { $self->{isPublisher} = 1 ; } elsif (($tagname eq 'a') && ( $attr->{href} =~ m|/CHAPITRE/fr/t/|i)) { $self->{isGenre} = 1 ; } elsif (0 eq 1 && ($tagname eq 'a') && ( $attr->{href} =~ m|/CHAPITRE/fr/p/|i) && ( $attr->{id} =~ m|ctl00_PHCenter_productTop_|i)) { $self->{isAuthor} = 1 ; } elsif (($tagname eq 'span') && ( $attr->{id} =~ m|ctl00_PHCenter_productBottom_productDetail_rpDetails_|i)) { $self->{isAnalyse} = 1 ; } } } sub end { my ($self, $tagname) = @_; $self->{inside}->{$tagname}--; } sub text { my ($self, $origtext) = @_; if ($self->{parsingList}) { if ($self->{isTitle}) { $self->{itemsList}[$self->{itemIdx}]->{title} = $origtext; $self->{isTitle} = 0 ; } elsif ($self->{isAuthor}) { if ($origtext =~ m/Les outils de recherche/i) { # the search failed $self->{isAuthor} = 0; return; } # 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}]->{authors} eq '') { $self->{itemsList}[$self->{itemIdx}]->{authors} = $origtext; } else { $self->{itemsList}[$self->{itemIdx}]->{authors} .= ', '; $self->{itemsList}[$self->{itemIdx}]->{authors} .= $origtext; } $self->{isAuthor} = 0 ; } elsif ($self->{isAnalyse}) { $self->{isPublisher} = 1 if ($origtext =~ m/Editeur :/i); $self->{isSerie} = 1 if ($origtext =~ m/Collection :/i); $self->{isPublication} = 1 if ($origtext =~ m/Date :/i); $self->{isAnalyse} = 0 ; } elsif ($self->{isPublisher}) { $origtext =~ s/Editeur : //; $origtext =~ s/\.$//; my @array = split(/\n/,$origtext); $self->{itemsList}[$self->{itemIdx}]->{edition} = $array[0]; $self->{isPublisher} = 0 ; } elsif ($self->{isPublication}) { $origtext =~ s/Date de parution ://; $origtext =~ s/\.$//; my @array = split(/\n/,$origtext); $self->{itemsList}[$self->{itemIdx}]->{publication} = $array[0]; $self->{isPublication} = 0 ; } elsif ($self->{isSerie}) { my @array = split(/\n/,$origtext); $self->{itemsList}[$self->{itemIdx}]->{serie} = $array[0]; $self->{isSerie} = 0 ; } } 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}) { $self->{curInfo}->{title} = $origtext; $self->{isTitle} = 0 ; } elsif ($self->{isAuthor} eq 1) { if ( $origtext ne '') { my @array = split(/;/,$origtext); my $element; foreach $element (@array) { my @nom_prenom = split(/,/,$element); # Enleve les blancs en debut de chaine $nom_prenom[0] =~ s/^\s//; $nom_prenom[1] =~ s/^\s//; # Enleve les blancs en fin de chaine $nom_prenom[0] =~ s/\s+$//; $nom_prenom[1] =~ s/\s+$//; if ($self->{curInfo}->{authors} eq '') { if ($nom_prenom[1] ne '') { $self->{curInfo}->{authors} = $nom_prenom[1] ." " . $nom_prenom[0]; } else { $self->{curInfo}->{authors} = $nom_prenom[0]; } } else { if ($nom_prenom[1] ne '') { $self->{curInfo}->{authors} .= ", " . $nom_prenom[1] ." " . $nom_prenom[0]; } else { $self->{curInfo}->{authors} .= ", " . $nom_prenom[0]; } } } $self->{isAuthor} = 0 ; } } elsif ($self->{isTranslator} eq 2) { $self->{curInfo}->{translator} = $origtext; $self->{isTranslator} = 0 ; } elsif ($self->{isPublisher} eq 1) { $self->{curInfo}->{publisher} = $origtext; $self->{isPublisher} = 0 ; } elsif ($self->{isDescription} eq 2) { $self->{curInfo}->{description} = $origtext; $self->{isDescription} = 0 ; } elsif ($self->{isPublication} && $self->{isAnalyse}) { $self->{curInfo}->{publication} = $origtext; $self->{isPublication} = 0 ; } elsif ($self->{isISBN} && $self->{isAnalyse}) { $self->{curInfo}->{isbn} = $origtext; $self->{isISBN} = 0 ; } elsif ($self->{isPage} && $self->{isAnalyse}) { if ($origtext ne '') { $self->{curInfo}->{pages} = $origtext; $self->{isPage} = 0 ; } } elsif ($self->{isCollection}) { $self->{curInfo}->{serie} = $origtext; $self->{isCollection} = 0 ; } elsif ($self->{isGenre}) { $origtext =~ s|/|,|gi; $self->{curInfo}->{genre} = $origtext; $self->{isGenre} = 0 ; } elsif ($self->{isLanguage} && $self->{isAnalyse}) { $self->{curInfo}->{language} = $origtext; $self->{isLanguage} = 0 ; $self->{isAnalyse} = 0 ; } elsif ($self->{isAnalyse}) { $self->{isPublication} = 1 if ($origtext =~ m/parution/i); $self->{isISBN} = 1 if ($origtext =~ m/EAN13/i); $self->{isPublisher} = 1 if ($origtext =~ m/Editeur/i); $self->{isLanguage} = 1 if ($origtext =~ m/Langue/i); $self->{isPage} = 1 if ($origtext =~ m/Nombre de page/i); $self->{isAuthor} = 1 if ($origtext =~ m/Auteur/i); $self->{isAnalyse} = 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 => 0, edition => 1, serie => 1, }; $self->{isTitle} = 0; $self->{isAuthor} = 0; $self->{isPublisher} = 0; $self->{isSerie} = 0; $self->{isPublication} = 0; $self->{isAnalyse} = 0; $self->{isDescription} = 0; $self->{isISBN} = 0; $self->{isLanguage} = 0; $self->{isCollection} = 0; $self->{isTranslator} = 0; $self->{isGenre} = 0; return $self; } sub preProcess { my ($self, $html) = @_; $self->{isTitle} = 0; $self->{isAuthor} = 0; $self->{isPublisher} = 0; $self->{isSerie} = 0; $self->{isPublication} = 0; $self->{isAnalyse} = 0; $self->{isDescription} = 0; $self->{isISBN} = 0; $self->{isLanguage} = 0; $self->{isCollection} = 0; $self->{isTranslator} = 0; $self->{isGenre} = 0; if ($self->{parsingList}) { $html =~ s|||gi; $html =~ s|||gi; $html =~ s|,|,|gi; $html =~ s|(auteur)|(auteur)|gi; } else { $html =~ s|||gi; #$html =~ s|||gi; $html =~ s|||gi; $html =~ s|
  • |\n* |gi; $html =~ s|
    |\n|gi; $html =~ s|
    |\n|gi; $html =~ s|||gi; $html =~ s|||gi; $html =~ s|||gi; $html =~ s|||gi; $html =~ s|

    |\n|gi; $html =~ s|

    ||gi; $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; $html =~ s|\n+|\n|gi; } return $html; } sub getSearchUrl { my ($self, $word) = @_; # utilisation d'une requête GET à la place d'un POST #$word =~ s/\+/ /g; #return ('http://www.chapitre.com/CHAPITRE/fr/search/Default.aspx?search=true', ["quicksearch" => "$word"] ); #return ('http://www.chapitre.com/CHAPITRE/fr/search/Default.aspx?search=true&quicksearch='.$word); return "http://www.chapitre.com/CHAPITRE/fr/search/Default.aspx?quicksearch=" . $word . "&optSearch=BOOKS"; } sub getItemUrl { my ($self, $url) = @_; return $url; } sub getName { return "Chapitre.com"; } sub getCharset { my $self = shift; return "ISO-8859-15"; } sub getAuthor { return 'TPF - Kerenoc'; } sub getLang { return 'FR'; } sub getSearchFieldsArray { return ['isbn', 'title']; } } 1;