1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This script will search for source for needed movies that does not have sources yet |
5
|
|
|
*/ |
6
|
|
|
require_once __DIR__ . '/../public/index.php'; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Process a movie and pause between each of them |
10
|
|
|
* @param Closure $func |
11
|
|
|
* @param \mQueue\Model\Movie[] $movies |
12
|
|
|
* @param int $seconds |
13
|
|
|
*/ |
14
|
|
|
function movieProcessor(Closure $func, $movies, $seconds) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$total = $movies->count(); |
|
|
|
|
17
|
|
|
$count = 0; |
18
|
|
|
foreach ($movies as $movie) { |
19
|
|
|
echo '[' . str_pad(++$count, 5, ' ', STR_PAD_LEFT) . '/' . str_pad($total, 5, ' ', STR_PAD_LEFT) . '] ' . $movie->getImdbUrl('akas') . "\t"; |
20
|
|
|
flush(); |
21
|
|
|
|
22
|
|
|
$func($movie); |
23
|
|
|
|
24
|
|
|
echo "\n"; |
25
|
|
|
|
26
|
|
|
sleep($seconds); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $total; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Search source |
34
|
|
|
*/ |
35
|
|
|
function searchSource() |
36
|
|
|
{ |
37
|
|
|
$movies = \mQueue\Model\MovieMapper::findAllForSearch(); |
38
|
|
|
$searcher = function (\mQueue\Model\Movie $movie) { |
39
|
|
|
$searchEngine = new mQueue\Service\SearchEngine(); |
40
|
|
|
|
41
|
|
|
$movie->fetchData(); // Refresh movie data to be sure we have latest available title |
42
|
|
|
$title = $movie->getTitle(); |
43
|
|
|
$data = $searchEngine->search($title); |
44
|
|
|
$scores = $searchEngine->computeScores($title, $data); |
45
|
|
|
|
46
|
|
|
$best = @reset($scores); |
47
|
|
|
$movie->setSource($best); |
48
|
|
|
$movie->save(); |
49
|
|
|
|
50
|
|
|
echo $movie->source ? : '[source not found]'; |
|
|
|
|
51
|
|
|
}; |
52
|
|
|
|
53
|
|
|
// 5 minutes pause between search, not to stress third-party servers |
54
|
|
|
$total = movieProcessor($searcher, $movies, 5 * 60); |
55
|
|
|
|
56
|
|
|
echo $total . " movie sources updated in database\n"; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fetch movie data for those that need it |
61
|
|
|
* @param int $limit |
|
|
|
|
62
|
|
|
* @param int $sleep seconds to sleep between each movie |
63
|
|
|
*/ |
64
|
|
|
function fetchMovieData($limit = null, $sleep = 0) |
65
|
|
|
{ |
66
|
|
|
$movies = \mQueue\Model\MovieMapper::findAllForFetching($limit); |
67
|
|
|
$fetcher = function (\mQueue\Model\Movie $movie) { |
68
|
|
|
$movie->fetchData(); |
69
|
|
|
echo $movie->title; |
|
|
|
|
70
|
|
|
}; |
71
|
|
|
|
72
|
|
|
$total = movieProcessor($fetcher, $movies, $sleep); |
73
|
|
|
|
74
|
|
|
echo $total . " movies updated in database\n"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// we only do things if this file were NOT included (otherwise, the file was included to access misc functions) |
78
|
|
|
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) { |
79
|
|
|
// Clean up obsolete sources |
80
|
|
|
\mQueue\Model\MovieMapper::deleteObsoleteSources(); |
81
|
|
|
|
82
|
|
|
// Fetch movie data to update title and release date |
83
|
|
|
fetchMovieData(20, 1 * 60); |
84
|
|
|
|
85
|
|
|
// Search source for movies with release date |
86
|
|
|
searchSource(); |
87
|
|
|
} |
88
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.