summaryrefslogtreecommitdiff
path: root/lib/gcstar/GCPlugins/GCfilms/GCCsfd.pm
blob: ea84b45d6b72c44c3885ebe2027c8fcc54bea9e2 (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# Replace SiteTemplate with your plugin name.
# The package name must exactly match the file name (.pm)
package GCPlugins::GCfilms::GCCsfd;

###################################################
#
#  Copyright 2005-2009 Tian
#  Copyright 2007,2011 Petr Gajdůšek <gajdusek.petr@centrum.cz>
#
#  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 warnings;
use utf8;

use GCPlugins::GCfilms::GCfilmsCommon;

{

    # Replace SiteTemplate with your exporter name
    # It must be the same name as the one used for file and main package name
    package GCPlugins::GCfilms::GCPluginCsfd;

    use base qw(GCPlugins::GCfilms::GCfilmsPluginsBase);

    # getSearchCharset
    # Charset of search term
    sub getSearchCharset
    {
        return 'UTF-8';
    }

    # getSearchUrl
    # Used to get the URL that to be used to perform searches.
    # $word is the query
    # Returns the full URL.
    sub getSearchUrl
    {
        my ($self, $word) = @_;
        return "http://www.csfd.cz/hledat/?q=$word";
    }

    # getItemUrl
    # Used to get the full URL of a movie page.
    # Useful when url on results pages are relative.
    # $url is the URL as found with a search.
    # Returns the absolute URL.
    sub getItemUrl
    {
        my ($self, $url) = @_;

        $url = "http://www.csfd.cz" . $url if ($url !~ /^http:/);
        return $url;
    }

    # getCharset
    # Used to convert charset in web pages.
    # Returns the charset as specified in pages.
    #sub getCharset {
    #	my $self = shift;
    #
    #	return "UTF-8";
    #}

    # getName
    # Used to display plugin name in GUI.
    # Returns the plugin name.
    sub getName
    {
        return "CSFD.cz";
    }

    # getAuthor
    # Used to display the plugin author in GUI.
    # Returns the plugin author name.
    sub getAuthor
    {
        return 'Petr Gajdůšek';
    }

    # getLang
    # Used to fill in plugin list with user language plugins
    # Return the language used for this site (2 letters code).
    sub getLang
    {
        return 'CS';
    }

    # hasSearchYear
    # Used to hide year column in search results
    # Return 0 to hide column, 1 to show it.
    sub hasSearchYear
    {
        return 1;
    }

    # hasSearchDirector
    # Used to hide director column in search results
    # Return 0 to hide column, 1 to show it.
    sub hasSearchDirector
    {
        return 1;
    }

    # hasSearchActors
    # Used to hide actors column in search results
    # Return 0 to hide column, 1 to show it.
    sub hasSearchActors
    {
        return 1;
    }

    # getExtra
    # Used if the plugin wants an extra column to be displayed in search results
    # Return the column title or empty string to hide the column.
    sub getExtra
    {

        return 'Žánr';
    }

    # changeUrl
    # Can be used to change URL if movie URL and the one used to
    # extract information are different.
    # Return the modified URL.
    sub changeUrl
    {
        my ($self, $url) = @_;

        return $url;
    }

	# preProcess
	# Called before each page is processed. You can use it to do some substitutions.
	# $html is the page content.
	# Returns modified version of page content.
    sub preProcess
    {
        my ($self, $html) = @_;
        $self->{parsingEnded} = 0;
        if ($self->{parsingList})
        {
            # Search results

	        # Initial values for search results parsing
	        # There are two movies list:
	        # First with detailed info (title, genre, origin country, year, directors, actors)
	        # Second with brief list of other movies (title, year)

            # We are in brief list containing other movies without details
            $self->{insideOtherMovies} = 0;
            # Movie link; movie's details follow if not in brief list
            $self->{isMovie} = 0;

            ## Details:

            # Movie's details will follow: Genre, origin, actors, directors, year
            $self->{insideDetails} = 0;
            # In movie's details after paragraph with Genre, origin and date
            $self->{wasDetailsInfo} = 0;
            # In movie's details: directors and actors
            $self->{directors}        = ();
            $self->{directorsCounter} = 0;
            $self->{actors}           = ();
            $self->{actorsCounter}    = 0;
            $self->{insideDirectors}  = 0;
            $self->{insideActors}     = 0;

            # Movie year
            $self->{isYear} = 0;

            ## Preprocess

            # directors and actors
            $html =~ s/\n\s*Režie:\s([^\n]*)/<div class="directors">$1<\/div>/g;
            $html =~ s/\n\s*Hrají:\s([^\n].*)/<div class="actors">$1<\/div>/g;
            # year
            $html =~ s/<span class="film-year">\(([0-9]+)\)<\/span>/<span class="film-year">$1<\/span>/g;
        }
        else
        {
            # Movie page

            # Initial values for search results parsing

            # array containg other movie titles (not exported to GCStar)
            $self->{titles} = ();
            # in list containing other movie titles
            $self->{isTitles} = 0;
            # in the original title (title for same country as movie's origin)
            $self->{isOrigTitle} = 0;
            # original title (if not set during parsing it will be set to main title at the end)
            $self->{origTitle}     = undef;
            $self->{titlesCounter} = 0;

            $self->{insideGenre} = 0;

            $self->{awaitingSynopsis} = 0;
            $self->{insideSynopsis}   = 0;

            # inside details with country, date (year) and time (length)
            $self->{insideInfo} = 0;

            $self->{insideRating} = 0;

            # User comments
            # Each comment consists of commenter (user) and his comment

            $self->{insideCommentAuthor} = 0;
            $self->{awaitingComment}     = 0;
            $self->{insideComment}       = 0;

            # In directors and actors
            $self->{insideDirectors}  = 0;
            $self->{insideActors}     = 0;
            $self->{directors}        = ();
            $self->{directorsCounter} = 0;
            $self->{actors}           = ();
            $self->{actorsCounter}    = 0;

            ## Preprocess

            # removee <br /> and <br>
            $html =~ s/<br( \/)?>/\n/g;
            ## Synopsis
            # remove list bullet
            $html =~ s/<img src="http:\/\/img.csfd.cz\/sites\/web\/images\/common\/li.gif"[^>]*>//g;
            # remove hyperlink to user profile
            $html =~ s/(&nbsp;<span class="source[^\(]*\()<a[^>]*>([^<]*)<\/a>/$1uživatel $2/g;
            # remove <span></span> around synopsis source
            $html =~ s/&nbsp;<span class="source[^\(]*\(([^\)]*)\)<\/span>/\n-- $1/g;
            $html =~ s/<div data-truncate="570">([^<]*)<\/div>/$1/g;
        }
        return $html;
    }

    # In processing functions below, self->{parsingList} can be used.
    # If true, we are processing a search results page
    # If false, we are processing a movie information page.

    # $self->{inside}->{tagname} (with correct value for tagname) can be used to test
    # if we are in the corresponding tag.

	# You have a counter $self->{movieIdx} that have to be used when processing search results.
	# It is your responsability to increment it!

    # When processing search results, you have to fill (if available) following fields:
    #
    #  $self->{movieList}[$self->{movieIdx}]->{title}
    #  $self->{movieList}[$self->{movieIdx}]->{url}
    #  $self->{movieList}[$self->{movieIdx}]->{actors}
    #  $self->{movieList}[$self->{movieIdx}]->{director}
    #  $self->{movieList}[$self->{movieIdx}]->{date}
    #  $self->{movieList}[$self->{movieIdx}]->{extra}

	# When processing a movie page, you need to fill the fields (if available) in $self->{curInfo}. They are:
	#
	#  $self->{curInfo}->{title}
	#  $self->{curInfo}->{director}
	#  $self->{curInfo}->{original}        (Original title)
	#  $self->{curInfo}->{actors}
	#  $self->{curInfo}->{genre}        (Comma separated list of movie type)
	#  $self->{curInfo}->{country}        (Movie Nationality or country)
	#  $self->{curInfo}->{date}
	#  $self->{curInfo}->{time}
	#  $self->{curInfo}->{synopsis}
	#  $self->{curInfo}->{image}
	#  $self->{curInfo}->{audio}
	#  $self->{curInfo}->{subt}
	#  $self->{curInfo}->{age}          0     : No information
	#                                   1     : Unrated
	#                                   2     : All audience
	#                                   5     : Parental Guidance
	#                                   >= 10 : Minimum age value

    # start
    # Called each time a new HTML tag begins.
    # $tagname is the tag name.
    # $attr is reference to an associative array of tag attributes.
    # $attrseq is an array reference containing all the attributes name.
    # $origtext is the tag text as found in source file
    # Returns nothing
    sub start
    {
        my ($self, $tagname, $attr, $attrseq, $origtext) = @_;
        $self->{inside}->{$tagname}++;


        if ($self->{parsingEnded})
        {
            return;
        }

        if ($self->{parsingList})
        {

            # in brief list of other movies (without details)
            if ($tagname eq "ul" and $attr->{class} eq "films others")
            {
                $self->{insideOtherMovies} = 1;
            }

            # in link to movie page
            if ($tagname eq "a" and $attr->{href} =~ m/\/film\/[0-9]+-.*/)
            {
                $self->{isMovie} = 1;
                $self->{itemIdx}++;
                $self->{itemsList}[ $self->{itemIdx} ]->{url} = $attr->{href};
                $self->{insideDetails} = 1 if ($self->{insideOtherMovies} != 1);
                $self->{wasDetailsInfo} = 0;
            }

            # directors and actors
            if ($tagname eq "div")
            {
                $self->{insideDirectors} = 1 if ($attr->{class} eq "directors");
                $self->{insideActors}    = 1 if ($attr->{class} eq "actors");
            }

            # year
            if ($tagname eq "span")
            {
                $self->{isYear} = 1 if ($attr->{class} eq "film-year");
            }
        }
        else
        {

            # Synopsis
            if (    $tagname eq "div"
                and $attr->{class} eq "content"
                and $self->{awaitingSynopsis})
            {
                $self->{insideSynopsis}   = 1;
                $self->{awaitingSynopsis} = 0;
            }

            # Poster
            if (    $tagname eq "img"
                and $attr->{src} =~ /^http:\/\/img\.csfd\.cz\/posters\//)
            {
                $self->{curInfo}->{image} = $attr->{src};
            }

            # Original name and other names
            if ($tagname eq "ul" and $attr->{class} eq "names")
            {
                $self->{isTitles} = 1;
            }

            if ($tagname eq "img" and $self->{isTitles})
            {
                $self->{isOrigTitle} = 1 if ($attr->{alt} !~ /název$/);
                $self->{isSKTitle}   = 1 if ($attr->{alt} =~ /SK název$/);
            }

            # Genre
            if ($tagname eq "p" and $attr->{class} eq "genre")
            {
                $self->{insideGenre} = 1;
            }

            # Info (country ,date, time = duration)
            if ($tagname eq "p" and $attr->{class} eq "origin")
            {
                $self->{insideInfo} = 1;
            }

            # Rating
            if ($tagname eq "h2" and $attr->{class} eq "average")
            {
                $self->{insideRating} = 1;
            }

            # Comments
            if ($tagname eq "h5" and $attr->{class} eq "author")
            {
                $self->{insideCommentAuthor} = 1;
            }
            if ($self->{awaitingComment} and $tagname eq "p" and $attr->{class} eq "post")
            {
                $self->{awaitingComment} = 0;
                $self->{insideComment}   = 1;
            }

        }
    }

    # end
    # Called each time a HTML tag ends.
    # $tagname is the tag name.
    sub end
    {
        my ($self, $tagname) = @_;
        $self->{inside}->{$tagname}--;

        if ($self->{parsingList})
        {

            # movie details
            $self->{insideDetails} = 0
              if ($tagname eq "div")
              and $self->{insideDetails};

            # directors and actors
            if ($tagname eq "div")
            {
                if ($self->{insideDirectors})
                {
                    $self->{insideDirectors} = 0;
                    $self->{itemsList}[ $self->{itemIdx} ]->{director} =
                      join(', ', @{$self->{directors}});
		            $self->{directors}        = ();
		            $self->{directorsCounter} = 0;
                }
                if ($self->{insideActors})
                {
                    $self->{insideActors} = 0;
                    $self->{itemsList}[ $self->{itemIdx} ]->{actors} =
                      join(', ', @{$self->{actors}});
                    $self->{actors}           = ();
                    $self->{actorsCounter}    = 0;
                }
            }
        }
        else
        {

            # Synopsis
            $self->{insideSynopsis} = 0 if ($tagname eq "div");

            # Titles
            if ($tagname eq "ul" and $self->{isTitles})
            {
                $self->{isTitles} = 0;
            }

            if ( $tagname eq "body" )
            {
            	$self->{curInfo}->{original} ||= $self->{curInfo}->{title};
            } 

            # Actors
            if ($tagname eq "div" and $self->{insideActors})
            {
                $self->{curInfo}->{actors} = join(', ', @{$self->{actors}});
                $self->{insideActors} = 0;
            }

            # Directors
            if ($tagname eq "div" and $self->{insideDirectors})
            {
                $self->{curInfo}->{director} = join(', ', @{$self->{directors}});
                $self->{insideDirectors} = 0;
            }

            # Comment

            $self->{insideCommentAuthor} = 0
              if ($tagname eq "h5" and $self->{insideCommentAuthor});

            if ($tagname eq "li" and $self->{isComment})
            {
                $self->{curInfo}->{comment} .= "\n";
                $self->{isComment} = 0;
            }

            # Debug
            if ($tagname eq "body" and $self->{debug})
            {
                use Data::Dumper;
                print Dumper $self->{curInfo};
            }
        }
    }

    # text
    # Called each time some plain text (between tags) is processed.
    # $origtext is the read text.
    sub text
    {
        my ($self, $origtext) = @_;

        return if length($origtext) < 2;
        $origtext =~ s/^\s+|\s+$//g;

        return if ($self->{parsingEnded});
        
        if ($self->{parsingList})
        {
            if ($self->{inside}->{h1} && $origtext !~ m/Vyhledávání/i)
            {
                $self->{parsingEnded} = 1;
                $self->{itemIdx} = 0;
                $self->{itemsList}[0]->{url} = $self->{loadedUrl};
            }

            # Movie title
            if ($self->{isMovie})
            {
                $self->{itemsList}[ $self->{itemIdx} ]->{"title"} = $origtext;
                $self->{isMovie} = 0;
                return;
            }

            # Date (year)
            elsif ($self->{isYear})
            {
                $self->{itemsList}[ $self->{itemIdx} ]->{"date"} = $origtext;
                $self->{isYear} = 0;
            }

            # Extra movie info: genre, origin, date
            elsif ( $self->{inside}->{p}
                and $self->{insideDetails}
                and $self->{wasDetailsInfo} == 0)
            {
                my @tmp = split(', ', $origtext);
                my $pos = $#tmp;
                my ($year, $country, $genre) = (undef, undef, undef);
                $year = $tmp[$pos] if ($tmp[$pos] =~ /^\d+$/);
                $pos--;
                $country = $tmp[$pos] if ($pos >= 0);
                $pos--;
                $genre = $tmp[$pos] if ($pos >= 0);

                $self->{itemsList}[ $self->{itemIdx} ]->{date} = $year if (defined $year);
                $self->{itemsList}[ $self->{itemIdx} ]->{country} = $country
                  if (defined $country);
                $self->{itemsList}[ $self->{itemIdx} ]->{extra} = $genre
                  if (defined $genre);
                $self->{wasDetailsInfo} = 1;
            }

            # Directors
            elsif ($self->{inside}->{a} and $self->{insideDirectors})
            {
                push @{$self->{directors}}, $origtext;
                $self->{directorsCounter}++;
            }

            # Actors
            elsif ($self->{inside}->{a} and $self->{insideActors})
            {
                push @{$self->{actors}}, $origtext;
                $self->{actorsCounter}++;
            }
        }
        else
        {

            # Movie titles
            if ($self->{inside}->{h1})
            {
                $self->{curInfo}->{title} = $origtext
                  if !$self->{curInfo}->{title};
            }
            if ($self->{inside}->{h3} and $self->{isTitles})
            {
                $self->{titlesCounter}++;
                $self->{titles}[ $self->{titlesCounter} ] = $origtext;
                if ($self->{isOrigTitle})
                {
                	$self->{curInfo}->{original} ||= $origtext; 
                    $self->{isOrigTitle} = 0;
                }
                if ($self->{isSKTitle} and $self->{lang} eq "SK")
                {
                    $self->{curInfo}->{title} = $origtext;
                    $self->{isSKTitle} = 0;
                }                
            }

            # Genre
            if ($self->{insideGenre})
            {
                $origtext =~ s/ \/ /,/g;
                $self->{curInfo}->{genre} = $origtext;
                $self->{insideGenre} = 0;
            }

            # Extra movie info: country, date (year), time
            if ($self->{insideInfo})
            {
                my ($country, $year, $time) = split(', ', $origtext);
                $country =~ s/ \/ /,/g;

                $self->{curInfo}->{country} = $country;
                $self->{curInfo}->{date}    = $year;
                $self->{curInfo}->{time}    = $time;

                $self->{insideInfo} = 0;
            }

            # Directors and Actors
            if ($self->{inside}->{h4})
            {
                $self->{insideDirectors} = 1 if ($origtext =~ /^Režie:/);
                $self->{insideActors}    = 1 if ($origtext =~ /^Hrají:/);
            }

            if ($self->{inside}->{a} and $self->{insideDirectors})
            {
                push @{$self->{directors}}, $origtext;
                $self->{directorsCounter}++;
            }
            if ($self->{inside}->{a} and $self->{insideActors})
            {
                #push @{$self->{curInfo}->{actors}}, [$origtext]
                #  if ($self->{actorsCounter} <
                #    $GCPlugins::GCfilms::GCfilmsCommon::MAX_ACTORS);
                #$self->{actorsCounter}++;
                push @{$self->{actors}}, $origtext;
                $self->{actorsCounter}++;
            }

            # Synopsis
            if ($self->{inside}->{h3})
            {
                $self->{awaitingSynopsis} = 1 if ($origtext eq "Obsah");
            }
            if ($self->{inside}->{li} and $self->{insideSynopsis})
            {
                $self->{curInfo}->{synopsis} .= $origtext . "\n\n\n";
            }

            # Rating
            if ($self->{insideRating})
            {
                $origtext =~ s/([0-9]+)%/$1/;
                $self->{curInfo}->{ratingpress} = int($origtext / 10 + .5)
                  if ($origtext ne "");
                $self->{insideRating} = 0;
            }

            # Comments
            if ($self->{inside}->{a} and $self->{insideCommentAuthor})
            {
                $self->{curInfo}->{comment} .= $origtext . " napsal(a):\n";
                $self->{awaitingComment} = 1;
            }
            if ($self->{insideComment})
            {
                $self->{curInfo}->{comment} .= $origtext . "\n\n";
                $self->{insideComment} = 0;
            }
        }
    }

    # new
    # Constructor.
    # Returns object reference.
    sub new
    {
        my $proto = shift;
        my $class = ref($proto) || $proto;
        my $self  = $class->SUPER::new();

        # Do your init stuff here

        bless($self, $class);

        $self->{hasField} = {
            title    => 1,
            date     => 1,
            director => 1,
            actors   => 1,
            country  => 1
        };
        
        $self->{lang} = "CS";

        $self->{curName} = undef;
        $self->{curUrl}  = undef;
        
        $self->{debug} = ($ENV{GCS_DEBUG_PLUGIN_PHASE} > 0);        

        return $self;
    }

}

1;