summaryrefslogtreecommitdiff
path: root/lib/gcstar/GCPlugins/GCbooks/GCInternetBokHandeln.pm
blob: 3b553e93ccb6c712024a86fce00bea82afa347c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package GCPlugins::GCbooks::GCbooksInternetBokHandeln;

###################################################
#
#  Copyright 2005-2010 Christian Jodar
#
#  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 GCPlugins::GCbooks::GCbooksCommon;

{
    package GCPlugins::GCbooks::GCPluginInternetBokHandeln;

    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 'span') && ($attr->{class} eq 'title1'))
            {
                $self->{isFound} = 1 ;
                $self->{itemIdx}++;
                $self->{itemsList}[$self->{itemIdx}]->{url} = $self->{loadedUrl};
            }
            elsif (($tagname eq 'td') && ($attr->{rowspan} eq '4') && ($self->{isBook} eq '0') && ($self->{isFound} eq 0))
            {
                # En fait la sequence est un peu tordue. Je cherche le deuxieme passage dans la sequence
                # rowspan/a
                $self->{isBook} = 1 ;
                $self->{isUrl} = 1 ;
            }
            elsif (($tagname eq 'img') && ($self->{isBook} eq '1') && ($self->{isUrl}))
            {
                $self->{isBook} = 2 ;
            }
            elsif (($tagname eq 'a') && ($self->{isBook} eq '2') && ($self->{isUrl}))
            {
                $self->{itemIdx}++;
                $self->{itemsList}[$self->{itemIdx}]->{url} = "http://www.internetbokhandeln.se" . $attr->{href};
                $self->{isUrl} = 0 ;
                $self->{isTitle} = 1 ;
            }
            elsif (($tagname eq 'span') && ($attr->{class} eq 'author') && ($self->{isFound} eq 0))
            {
                $self->{isAuthor} = 1 ;
                $self->{isBook} = 0 ;
            }
            elsif (($tagname eq 'span') && ($attr->{class} eq 'shaded') && ($self->{isFound} eq 0))
            {
                $self->{isEditor_Publication_Format_Lang} = 1 ;
                $self->{isBook} = 0 ;
            }
        }
        else
        {
            if ($self->{isAuthor} eq 1)
            {
                $self->{isAuthor} = 2 ;
            }
            elsif ($self->{isPublisher} eq 1)
            {
                $self->{isPublisher} = 2 ;
            }
            elsif ($self->{isISBN} eq 1)
            {
                $self->{isISBN} = 2 ;
            }
            elsif ($self->{isFormat} eq 1)
            {
                $self->{isFormat} = 2 ;
            }
            elsif ($self->{isEdition} eq 1)
            {
                $self->{isEdition} = 2 ;
            }
            elsif ($self->{isPage} eq 1)
            {
                $self->{isPage} = 2 ;
            }
            elsif ($self->{isLanguage} eq 1)
            {
                $self->{isLanguage} = 2 ;
            }
            elsif ($self->{isPublication} eq 1)
            {
                $self->{isPublication} = 2 ;
            }
            elsif ($self->{isSerie} eq 1)
            {
                $self->{isSerie} = 2 ;
            }
            elsif (($tagname eq 'span') && ($attr->{class} eq 'title1'))
            {
                $self->{isTitle} = 1 ;
                # On initialise la variable ( sinon d une fiche sur l autre est n est pas reinitialisee )
                $self->{isDescription} = 0;
            }
            elsif (($tagname eq 'span') && ($attr->{class} eq 'font5'))
            {
                $self->{isAnalyse} = 1 ;
            }
            elsif (($tagname eq 'p') && ($self->{curInfo}->{isbn} ne '') && ($self->{curInfo}->{description} eq '') && ($self->{isDescription} ne 2))
            {
                $self->{isDescription} = 1 ;
            }
            elsif (($tagname eq 'div') && ($attr->{id} eq 'largebook'))
            {
                # Pour etre sur s il n y a pas de commentaire de ne pas prendre n importe quoi
                $self->{isDescription} = 2 ;
            }
            elsif (($tagname eq 'td') && ($attr->{class} eq 'pricecolumn'))
            {
                $self->{isCover} = 1 ;
            }
            elsif (($tagname eq 'img') && ($self->{isCover} eq 1))
            {
                # le but est de determiner s il y a une couverture ou non, et s il y en a une, on recuperera
                # la version grand format qui est bien plus tard
                if ($attr->{onclick} eq 'return showBig();')
                {
                   $self->{isCover} = 2 ;
                }
                else
                {
                   if ($attr->{src} eq '/i/dummy.gif')
                   {
                      # Il n y a pas d image
                      $self->{isCover} = 3 ;
                   }
	                   else
                   {
                      $self->{curInfo}->{cover} = $attr->{src} ;
                      $self->{isCover} = 3 ;
                   }
                }
            }
            elsif (($tagname eq 'img') && ($attr->{onclick} eq 'return hideBig();') && ($self->{isCover} eq 2))
            {
                $self->{curInfo}->{cover} = $attr->{src} ;
                $self->{isCover} = 3 ;
            }

        }
    }

    sub end
    {
        my ($self, $tagname) = @_;

        $self->{isFound} = 0;
        $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})
            {
                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->{itemsList}[$self->{itemIdx}]->{authors} eq '')
                   {
                      if ($nom_prenom[1] ne '')
                      {
                         $self->{itemsList}[$self->{itemIdx}]->{authors} = $nom_prenom[1] ." " . $nom_prenom[0];
                      }
                      else
                      {
                         $self->{itemsList}[$self->{itemIdx}]->{authors} = $nom_prenom[0];
                      }
                    }
                   else
                   {
                      if ($nom_prenom[1] ne '')
                      {
                         $self->{itemsList}[$self->{itemIdx}]->{authors} .= ", " . $nom_prenom[1] ." " . $nom_prenom[0];
                      }
                      else
                      {
                         $self->{itemsList}[$self->{itemIdx}]->{authors} .= ", " . $nom_prenom[0];
                      }
                   }
                }

                $self->{isAuthor} = 0 ;
            }
            elsif ($self->{isEditor_Publication_Format_Lang})
            {
                my @Editor_Publication_Format_Lang = split(/\|/,$origtext);

                $self->{itemsList}[$self->{itemIdx}]->{publication} = $Editor_Publication_Format_Lang[1];
                $self->{itemsList}[$self->{itemIdx}]->{publication} =~ s/^\s+//;
                $self->{itemsList}[$self->{itemIdx}]->{publication} =~ s/\s$+//;

                $self->{itemsList}[$self->{itemIdx}]->{format} = $Editor_Publication_Format_Lang[2];
                $self->{itemsList}[$self->{itemIdx}]->{format} =~ s/^\s+//;
                $self->{itemsList}[$self->{itemIdx}]->{format} =~ s/\s$+//;

                $self->{isEditor_Publication_Format_Lang} = 0 ;
            }
        }
       	else
        {
            # Enleve les blancs en debut de chaine
            $origtext =~ s/^\s+//;
            if ($self->{isTitle})
            {
                $self->{curInfo}->{title} = $origtext;
                $self->{isTitle} = 0 ;
            }
            elsif ($self->{isAnalyse})
            {
                $self->{isAuthor} = 1 if ($origtext =~ m/F.*rfattare/i);
                $self->{isISBN} = 1 if ($origtext =~ m/ISBN/i);
                $self->{isPublisher} = 1 if ($origtext =~ m/F.*rlag/i);
                $self->{isFormat} = 1 if ($origtext =~ m/Band/i);
                $self->{isEdition} = 1 if ($origtext =~ m/Upplagenr/i);
                $self->{isPage} = 1 if ($origtext =~ m/Sidor/i);
                $self->{isLanguage} = 1 if ($origtext =~ m/Spr.*k/i);
                $self->{isPublication} = 1 if ($origtext =~ m/Utgivning/i);
                $self->{isSerie} = 1 if ($origtext =~ m/Serie/i);

                $self->{isAnalyse} = 0 ;
            }
            elsif ($self->{isAuthor} eq 2)
            {
                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 ($nom_prenom[1] ne '')
                   {
                      $self->{curInfo}->{authors} .= $nom_prenom[1] ." " . $nom_prenom[0];
                   }
                   else
                   {
                      $self->{curInfo}->{authors} .= $nom_prenom[0];
                   }
                   $self->{curInfo}->{authors} .= ",";
                }

                $self->{isAuthor} = 0 ;
            }
            elsif ($self->{isISBN} eq 2)
            {
                # Il y a 2 ISBN sur le site, seul le premier m interesse
                if ($self->{curInfo}->{isbn} eq '')
                {
                    $self->{curInfo}->{isbn} = $origtext;
                }
                $self->{isISBN} = 0 ;
            }
            elsif ($self->{isPublisher} eq 2)
            {
                $self->{curInfo}->{publisher} = $origtext;
                $self->{isPublisher} = 0 ;
            }
            elsif ($self->{isFormat} eq 2)
            {
                my @array = split(/\n/,$origtext);

                $self->{curInfo}->{format} = $array[0];
                $self->{isFormat} = 0 ;
            }
            elsif ($self->{isEdition} eq 2)
            {
                # There is some trouble on the site with this field : it is not accurrate. For example for ISBN 9113014528
                # this field is set to 7000. So for instance this field isn't taken.
#                $self->{curInfo}->{edition} = $origtext;
                $self->{isEdition} = 0 ;
            }
            elsif ($self->{isPage} eq 2)
            {
                $self->{curInfo}->{pages} = $origtext;
                $self->{isPage} = 0 ;
            }
            elsif ($self->{isLanguage} eq 2)
            {
                $self->{curInfo}->{language} = $origtext;
                $self->{isLanguage} = 0 ;
            }
            elsif ($self->{isPublication} eq 2)
            {
                $self->{curInfo}->{publication} = $origtext;
                $self->{curInfo}->{publication} =~ s|([0-9]*) ([A-Za-z]*) ([0-9]*)|$1.'/'.$self->{monthNumber}->{$2}.'/'.$3|e;
                $self->{curInfo}->{publication} =~ s|([A-Za-z]*) ([0-9]*)|$self->{monthNumber}->{$1}.'/'.$2|e;
                $self->{isPublication} = 0 ;
            }
            elsif ($self->{isSerie} eq 2)
            {
                $self->{curInfo}->{serie} = $origtext;
                $self->{isSerie} = 0 ;
            }
            elsif ($self->{isDescription} eq 1)
            {
                $self->{curInfo}->{description} = $origtext;
                $self->{isDescription} = 2 ;
            }

        }
    } 

    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new();
        bless ($self, $class);

        $self->{monthNumber} = {
            Januari => '01',
            Februari => '02',
            Mars => '03',
            April => '04',
            Maj => '05',
            Juni => '06',
            Juli => '07',
            Augusti => '08',
            September => '09',
            Oktober => '10',
            November => '11',
            December => '12'
        };

        $self->{hasField} = {
            title => 1,
            authors => 1,
            publication => 1,
            format => 1,
            edition => 0,
        };

        $self->{isBook} = 0;
        $self->{isUrl} = 0;
        $self->{isEditor_Publication_Format_Lang} = 0 ;
        $self->{isAnalyse} = 0;
        $self->{isFound} = 0;
        $self->{isTitle} = 0;
        $self->{isAuthor} = 0;
        $self->{isPublisher} = 0;
        $self->{isISBN} = 0;
        $self->{isFormat} = 0;
        $self->{isEdition} = 0;
        $self->{isPage} = 0;
        $self->{isLanguage} = 0;
        $self->{isPublication} = 0;
        $self->{isSerie} = 0;
        $self->{isDescription} = 0;
        $self->{isCover} = 0;

        return $self;
    }

    sub preProcess
    {
        my ($self, $html) = @_;

        if ($self->{parsingList})
        {
            $html =~ s|<b>||gi;
            $html =~ s|</b>||gi;
        }
        else
        {
            $html =~ s|<li>|\n* |gi;
            $html =~ s|<br>|\n|gi;
            $html =~ s|<br />|\n|gi;
            $html =~ s|<b>||gi;
            $html =~ s|</b>||gi;
            $html =~ s|<i>||gi;
            $html =~ s|</i>||gi;
        }
        
        return $html;
    }
    
    sub getSearchUrl
    {
		my ($self, $word) = @_;
	
        return "http://www.internetbokhandeln.se/results.html?new_search=1&all_search=" . $word. "&search_media=all";

    }
    
    sub getItemUrl
    {
		my ($self, $url) = @_;
		
        return $url if $url;
        return 'http://www.internetbokhandeln.se/';
    }

    sub getName
    {
        return "InternetBokHandeln";
    }
    
    sub getAuthor
    {
        return 'TPF';
    }
    
    sub getLang
    {
        return 'SV';
    }

    sub getSearchFieldsArray
    {
        return ['isbn', 'title'];
    }

}

1;