1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\Movie_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A Movie plugin for reading a movie overview from TheMovieDb |
12
|
|
|
* @author Stefano Bianchini |
13
|
|
|
* @website http://www.stefanobianchini.net |
14
|
|
|
*/ |
15
|
|
|
class Movie_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
16
|
|
|
/** |
17
|
|
|
* Priority of plugin |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
var $priority = 2; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the behaviour of plugin |
24
|
|
|
* @param string $command |
25
|
|
|
*/ |
26
|
|
|
function answer($command) { |
|
|
|
|
27
|
|
|
$answer = ''; |
|
|
|
|
28
|
|
|
JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
29
|
|
|
|
30
|
|
|
//Understand the movie |
31
|
|
|
preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command, $matches); |
32
|
|
|
|
33
|
|
|
$movie_title = end($matches); |
34
|
|
|
|
35
|
|
|
$_THEMOVIEDB_API_KEY = ''; |
36
|
|
|
|
37
|
|
|
//Load API key from json config |
38
|
|
|
if(file_exists('Plugins/Movie_plugin/api-key.json')) { |
39
|
|
|
//Create your own api key and put it in api-key.json |
40
|
|
|
$json_config = json_decode(file_get_contents('Plugins/Movie_plugin/api-key.json')); |
41
|
|
|
$_THEMOVIEDB_API_KEY = $json_config->themoviedb_key; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$token = new \Tmdb\ApiToken($_THEMOVIEDB_API_KEY); |
45
|
|
|
$client = new \Tmdb\Client($token, ['secure' => false]); |
46
|
|
|
|
47
|
|
|
$result = $client->getSearchApi()->searchMovies($movie_title, ['language'=>_LANGUAGE, 'page'=>1]); |
|
|
|
|
48
|
|
|
|
49
|
|
|
if(count($result['results'])>0) { |
50
|
|
|
$answer = ($result['results'][0]['overview']) ? ($result['results'][0]['overview']) : JarvisLanguage::translate('no_results',get_called_class()); |
51
|
|
|
} else { |
52
|
|
|
//No results |
53
|
|
|
$answer = JarvisLanguage::translate('no_results',get_called_class()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
JarvisTTS::speak($answer); |
57
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
58
|
|
|
$response->send(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get plugin's priority |
63
|
|
|
* @return int |
64
|
|
|
*/ |
65
|
|
|
function getPriority() { |
|
|
|
|
66
|
|
|
return $this->priority; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Is it the right plugin for the command? |
71
|
|
|
* @param string $command |
72
|
|
|
* @return boolean |
73
|
|
|
*/ |
74
|
|
|
function isLikely($command) { |
|
|
|
|
75
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Does the plugin need a session? |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
|
|
function hasSession() { |
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.