forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryTmdb.php
83 lines (64 loc) · 2.41 KB
/
queryTmdb.php
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
<?php
define('TMDB_API_KEY', '9a3cc6d14b4765b19587e229556ae226');
function queryTmdb($catType, $mcId, $title, $year) {
if ($catType == 3) {
$catType = 'movie';
} elseif ($catType == 5) {
$catType = 'tv';
} else {
throw new Exception('Unknown cattype: ' . $catType);
} // else
$baseUrl = 'http://api.themoviedb.org/3/search/' . $catType . '?api_key=' . TMDB_API_KEY .
'&append_to_response=trailers,credits,images&language=en';
/*
* Build the query
*/
$url = $baseUrl .
'&query=' . urlencode($title) .
'&year=' . urlencode($year);
echo "Performing a TMDB search for: " . $title . " (" . $year . ")";
$json = file_get_contents($url);
$parsed = json_decode($json);
echo ", done." . PHP_EOL;
if (empty($parsed->results)) {
return null;
} // if
return array('title' => $parsed->results[0]->title,
'tmdbid' => $parsed->results[0]->id,
'mcid' => $mcId);
}
echo $argv[0] . " - query tmdb distribution script" . PHP_EOL . PHP_EOL;
if ($argc < 2) {
echo "Je moet de filenaam welke je gekregen hebt als eerste parameter meegeven" . PHP_EOL;
die();
}
if (!is_readable($argv[1])) {
echo "Bestand '" . $argv[1] . "' kan niet worden gelezen" . PHP_EOL;
die();
} // if
@file_put_contents('results.csv', '', FILE_APPEND);
if (!is_writeable('results.csv')) {
echo "Bestand 'results.csv' kan niet worden geschreven" . PHP_EOL;
die();
} // if
echo "Load the master collections table into memory, ";
$masters = unserialize(file_get_contents($argv[1]));
echo "done (" . count($masters) . " records)" . PHP_EOL;
$counter = 0;
$startTime = time();
foreach($masters as $mc) {
$result = queryTmdb($mc['mcid'], $mc['title'], $mc['year']);
$counter++;
if ($result !== null) {
file_put_contents('results.csv', $result['mcid'] . ';' . $result['tmdbid'] . ';'. $result['title'] . ';' . $mc['title'] . PHP_EOL, FILE_APPEND);
} else {
file_put_contents('results.csv', $mc['mcid'] . ';NULL;NULL;' . $mc['title'] . PHP_EOL, FILE_APPEND);
} // if
if ($counter > 29) {
echo " backing off for " . max(3, 12 - (time() - $startTime)) . " seconds" . PHP_EOL;
// we try to limit ourselves to 30 records per 12 seconds
sleep(max(3, 12 - (time() - $startTime)));
$counter = 0;
$startTime = time();
} // if
} // foreach