| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Getopt::Long; | ||
| 7 | use Pod::Usage; | ||
| 8 | |||
| 9 | use IO::All -utf8; | ||
| 10 | use List::Compare; | ||
| 11 | use FilmAffinity::Movie; | ||
| 12 | use FilmAffinity::Utils qw/data2tsv/; | ||
| 13 | use FilmAffinity::UserRating; | ||
| 14 | |||
| 15 | =head1 NAME - filmaffinity-get-all-info.pl | ||
| 16 | |||
| 17 | get information from filmaffinity about a film and all ratings from a user | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | ./filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder | ||
| 22 | |||
| 23 | ./filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder --delay=2 | ||
| 24 | |||
| 25 | ./filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder --force | ||
| 26 | |||
| 27 | =head1 ARGUMENTS | ||
| 28 | |||
| 29 | =over 2 | ||
| 30 | |||
| 31 | =item --userid=123456 | ||
| 32 | |||
| 33 | userid from filmaffinity | ||
| 34 | |||
| 35 | =item --destination=/home/william/filmaffinity | ||
| 36 | |||
| 37 | destination folder | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =head1 OPTIONS | ||
| 42 | |||
| 43 | =over 2 | ||
| 44 | |||
| 45 | =item --delay=3 | ||
| 46 | |||
| 47 | delay between requests | ||
| 48 | |||
| 49 | =item --force | ||
| 50 | |||
| 51 | force to retrieve all movies | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | my ( $userID, $delay, $destination, $force, $help ); | ||
| 58 | |||
| 59 | GetOptions( | ||
| 60 | "userid=i" => \$userID, | ||
| 61 | "delay=i" => \$delay, | ||
| 62 | "destination=s" => \$destination, | ||
| 63 | "force" => \$force, | ||
| 64 | "help" => \$help, | ||
| 65 | ) | ||
| 66 | || pod2usage(2); | ||
| 67 | |||
| 68 | if ( $help || !$userID || !$destination ) { | ||
| 69 | pod2usage(1); | ||
| 70 | exit(0); | ||
| 71 | } | ||
| 72 | |||
| 73 | &setFileSystem( $destination ); | ||
| 74 | |||
| 75 | my $userParser = FilmAffinity::UserRating->new( | ||
| 76 | userID => $userID, | ||
| 77 | delay => $delay || 5, | ||
| 78 | ); | ||
| 79 | my $ref_movies = $userParser->parse(); | ||
| 80 | my $tsv = data2tsv( $ref_movies ); | ||
| 81 | $tsv > io($destination.'/ratings.list'); | ||
| 82 | |||
| 83 | my @listOfRemoteMovieId = keys %{$ref_movies}; | ||
| 84 | my @listOfLocalMovieId = &getListOfLocalMovieId( $destination ); | ||
| 85 | |||
| 86 | my $listCompare = List::Compare->new( | ||
| 87 | \@listOfLocalMovieId, | ||
| 88 | \@listOfRemoteMovieId, | ||
| 89 | ); | ||
| 90 | |||
| 91 | my @listOfMovieToRetrieve = $force ? @listOfRemoteMovieId : $listCompare->get_Ronly(); | ||
| 92 | |||
| 93 | my $progress; | ||
| 94 | if ( -t STDOUT ) { | ||
| 95 | eval { | ||
| 96 | require Term::ProgressBar; | ||
| 97 | $progress = Term::ProgressBar->new({ | ||
| 98 | name => 'jsonize movie information', | ||
| 99 | count => scalar @listOfMovieToRetrieve, | ||
| 100 | remove => 1 | ||
| 101 | }); | ||
| 102 | }; | ||
| 103 | if ($@) { | ||
| 104 | warn "Could not create progress bar. We can continue, but no progress will be reported"; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | my $count = 0; | ||
| 109 | foreach my $id ( @listOfMovieToRetrieve ){ | ||
| 110 | |||
| 111 | my $movie = FilmAffinity::Movie->new( | ||
| 112 | id => $id, | ||
| 113 | delay => $delay || 5, | ||
| 114 | ); | ||
| 115 | $movie->parse(); | ||
| 116 | |||
| 117 | my $json = $movie->toJSON(); | ||
| 118 | $json > io($destination.'/json/'.$id.'.json'); | ||
| 119 | |||
| 120 | $count++; | ||
| 121 | $progress->update($count) if $progress; | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | sub setFileSystem { | ||
| 126 | my ( $destination ) = shift; | ||
| 127 | mkdir $destination; | ||
| 128 | mkdir $destination.'/json'; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub getListOfLocalMovieId { | ||
| 132 | my ( $destination ) = shift; | ||
| 133 | |||
| 134 | my @listOfLocalMovie = (); | ||
| 135 | my @content = io($destination.'/json')->all(); | ||
| 136 | foreach my $file (@content){ | ||
| 137 | my $filename = $file->filename; | ||
| 138 | $filename =~ s/\.json//; | ||
| 139 | push @listOfLocalMovie, $filename; | ||
| 140 | } | ||
| 141 | return @listOfLocalMovie; | ||
| 142 | } | ||
| 143 | |||
| 144 | =head1 AUTHOR | ||
| 145 | |||
| 146 | William Belle, C<< <william.belle at gmail.com> >> | ||
| 147 | |||
| 148 | =head1 SEE ALSO | ||
| 149 | |||
| 150 | L<http://www.filmaffinity.com> | ||
| 151 | |||
| 152 | =head1 LICENSE AND COPYRIGHT | ||
| 153 | |||
| 154 | Copyright 2013 William Belle. | ||
| 155 | |||
| 156 | This program is free software; you can redistribute it and/or modify it | ||
| 157 | under the terms of either: the GNU General Public License as published | ||
| 158 | by the Free Software Foundation; or the Artistic License. | ||
| 159 | |||
| 160 | See http://dev.perl.org/licenses/ for more information. | ||
| 161 | |||
| 162 | =cut |