| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Getopt::Long; | ||
| 7 | use Pod::Usage; | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use English qw/-no_match_vars/; | ||
| 11 | use Readonly; | ||
| 12 | use IO::All -utf8; | ||
| 13 | use List::Compare; | ||
| 14 | use IO::Interactive qw/is_interactive/; | ||
| 15 | use FilmAffinity::Movie; | ||
| 16 | use FilmAffinity::Utils qw/data2tsv/; | ||
| 17 | use FilmAffinity::UserRating; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | filmaffinity-get-all-info.pl | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Get information from filmaffinity about a film and all ratings from a user | ||
| 26 | |||
| 27 | =head1 VERSION | ||
| 28 | |||
| 29 | Version 1.01 | ||
| 30 | |||
| 31 | =head1 USAGE | ||
| 32 | |||
| 33 | filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder | ||
| 34 | |||
| 35 | filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder --delay=2 | ||
| 36 | |||
| 37 | filmaffinity-get-all-info.pl --userid=123456 --destination=path/to/my/folder --force | ||
| 38 | |||
| 39 | =head1 REQUIRED ARGUMENTS | ||
| 40 | |||
| 41 | =over 2 | ||
| 42 | |||
| 43 | =item --userid=123456 | ||
| 44 | |||
| 45 | userid from filmaffinity | ||
| 46 | |||
| 47 | =item --destination=/home/william/filmaffinity | ||
| 48 | |||
| 49 | destination folder | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =head1 OPTIONS | ||
| 54 | |||
| 55 | =over 2 | ||
| 56 | |||
| 57 | =item --delay=3 | ||
| 58 | |||
| 59 | delay between requests | ||
| 60 | |||
| 61 | =item --force | ||
| 62 | |||
| 63 | force to retrieve all movies | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | our $VERSION = '1.01'; | ||
| 70 | |||
| 71 | Readonly my $DELAY => 5; | ||
| 72 | |||
| 73 | my ( $userID, $delay, $destination, $force, $help ); | ||
| 74 | |||
| 75 | GetOptions( | ||
| 76 | 'userid=i' => \$userID, | ||
| 77 | 'delay=i' => \$delay, | ||
| 78 | 'destination=s' => \$destination, | ||
| 79 | 'force' => \$force, | ||
| 80 | 'help' => \$help, | ||
| 81 | ) || pod2usage(2); | ||
| 82 | |||
| 83 | if ( $help || !$userID || !$destination ) { | ||
| 84 | pod2usage(1); | ||
| 85 | exit 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | setFileSystem(); | ||
| 89 | |||
| 90 | my $userParser = FilmAffinity::UserRating->new( | ||
| 91 | userID => $userID, | ||
| 92 | delay => $delay || $DELAY, | ||
| 93 | ); | ||
| 94 | my $ref_movies = $userParser->parse(); | ||
| 95 | my $tsv = data2tsv($ref_movies); | ||
| 96 | $tsv > io( $destination . '/ratings.list' ); | ||
| 97 | |||
| 98 | my @listOfRemoteMovieId = keys %{$ref_movies}; | ||
| 99 | my @listOfLocalMovieId = getListOfLocalMovieId(); | ||
| 100 | |||
| 101 | my $listCompare = | ||
| 102 | List::Compare->new( \@listOfLocalMovieId, \@listOfRemoteMovieId, ); | ||
| 103 | |||
| 104 | my @listOfMovieToRetrieve = | ||
| 105 | $force ? @listOfRemoteMovieId : $listCompare->get_Ronly(); | ||
| 106 | |||
| 107 | my $progress; | ||
| 108 | if ( is_interactive() ) { | ||
| 109 | my $value = eval { | ||
| 110 | require Term::ProgressBar; | ||
| 111 | $progress = Term::ProgressBar->new( | ||
| 112 | { | ||
| 113 | name => 'jsonize movie information', | ||
| 114 | count => scalar @listOfMovieToRetrieve, | ||
| 115 | remove => 1 | ||
| 116 | } | ||
| 117 | ); | ||
| 118 | }; | ||
| 119 | if ($EVAL_ERROR) { | ||
| 120 | carp | ||
| 121 | 'Could not create progress bar. We can continue, but no progress will be reported'; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | my $count = 0; | ||
| 126 | foreach my $id (@listOfMovieToRetrieve) { | ||
| 127 | |||
| 128 | my $movie = FilmAffinity::Movie->new( | ||
| 129 | id => $id, | ||
| 130 | delay => $delay || $DELAY, | ||
| 131 | ); | ||
| 132 | $movie->parse(); | ||
| 133 | $movie->myrating( $ref_movies->{$id}->{rating} ); | ||
| 134 | |||
| 135 | my $json = $movie->toJSON(); | ||
| 136 | $json > io( $destination . '/json/' . $id . '.json' ); | ||
| 137 | |||
| 138 | $count++; | ||
| 139 | $progress->update($count) if $progress; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub setFileSystem { | ||
| 143 | mkdir $destination; | ||
| 144 | mkdir $destination . '/json'; | ||
| 145 | return; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub getListOfLocalMovieId { | ||
| 149 | my @listOfLocalMovie = (); | ||
| 150 | my @content = io( $destination . '/json' )->all(); | ||
| 151 | foreach my $file (@content) { | ||
| 152 | my $filename = $file->filename; | ||
| 153 | $filename =~ s/[.]json//xms; | ||
| 154 | push @listOfLocalMovie, $filename; | ||
| 155 | } | ||
| 156 | return @listOfLocalMovie; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | William Belle, C<< <william.belle at gmail.com> >> | ||
| 162 | |||
| 163 | =head1 BUGS AND LIMITATIONS | ||
| 164 | |||
| 165 | Please report any bugs or feature requests to C<bug-filmaffinity-userrating at rt.cpan.org>, or through | ||
| 166 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FilmAffinity-UserRating>. I will be notified, and then you'll | ||
| 167 | automatically be notified of progress on your bug as I make changes. | ||
| 168 | |||
| 169 | =head1 SUPPORT | ||
| 170 | |||
| 171 | You can find documentation for this module with the perldoc command. | ||
| 172 | |||
| 173 | perldoc filmaffinity-get-all-info.pl | ||
| 174 | |||
| 175 | You can also look for information at: | ||
| 176 | |||
| 177 | =over 4 | ||
| 178 | |||
| 179 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 180 | |||
| 181 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FilmAffinity-UserRating> | ||
| 182 | |||
| 183 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 184 | |||
| 185 | L<http://annocpan.org/dist/FilmAffinity-UserRating> | ||
| 186 | |||
| 187 | =item * CPAN Ratings | ||
| 188 | |||
| 189 | L<http://cpanratings.perl.org/d/FilmAffinity-UserRating> | ||
| 190 | |||
| 191 | =item * Search CPAN | ||
| 192 | |||
| 193 | L<http://search.cpan.org/dist/FilmAffinity-UserRating/> | ||
| 194 | |||
| 195 | =back | ||
| 196 | |||
| 197 | =head1 SEE ALSO | ||
| 198 | |||
| 199 | L<http://www.filmaffinity.com> | ||
| 200 | |||
| 201 | =head1 LICENSE AND COPYRIGHT | ||
| 202 | |||
| 203 | Copyright 2013 William Belle. | ||
| 204 | |||
| 205 | This program is free software; you can redistribute it and/or modify it | ||
| 206 | under the terms of either: the GNU General Public License as published | ||
| 207 | by the Free Software Foundation; or the Artistic License. | ||
| 208 | |||
| 209 | See http://dev.perl.org/licenses/ for more information. | ||
| 210 | |||
| 211 | =cut |