-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIMDBBot.php
More file actions
172 lines (134 loc) · 4.84 KB
/
Copy pathIMDBBot.php
File metadata and controls
172 lines (134 loc) · 4.84 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* API wrapper for Open Movie Database API (http://omdbapi.com/)
* with responses for Slack slash commands
*
* @version v1.2.0
* @license https://opensource.org/licenses/MIT
*
* require 'IMDBBot.php';
* use MartijnOud\IMDBBot;
* $IMDBBot = new IMDBBot();
* echo json_encode($IMDBBot->q('Shawshank redemption'));
*
*/
namespace MartijnOud;
class IMDBBot
{
// Optional OMDb (poster) API key
private $apikey = null;
/**
* Uses the main OMDB API (?i= or ?t=)
* Returns a single result with detailed info
* @param string $q Movie title or imdb ID
* @return array with Slackable response
*/
public function q($q)
{
// Make URL proof
$q = urlencode($q);
// Check if imdb ID or title
// Call API
if (preg_match("/tt\\d{7}/", $q)) {
$item = $this->call('http://omdbapi.com/?i='.$q);
} else {
$item = $this->call('http://omdbapi.com/?t='.$q);
}
if ($item->Response != "True") {
$payload = array(
"response_type" => "in_channel",
"text" => "Sorry I didn't find anything for _".urldecode($q)."_!",
);
} else {
// Prepare response
$payload = array(
"response_type" => "in_channel",
"text" => "This is what I've found for _".urldecode($q)."_:",
"attachments" => array(
array(
"title" => $item->Title . " (".$item->Year.")",
"title_link" => "http://www.imdb.com/title/".$item->imdbID."/",
"thumb_url" => ((!empty($item->imdbID) AND !empty($this->apikey)) ? 'http://img.omdbapi.com/?i='.$item->imdbID.'&apikey='.$this->apikey : null),
"text" => $item->Plot,
"fields" => array(
array(
"title" => "Released",
"value" => $item->Released,
"short" => true,
),
array(
"title" => "Runtime",
"value" => $item->Runtime,
"short" => true,
),
array(
"title" => "Actors",
"value" => $item->Actors,
"short" => true,
),
array(
"title" => "Rating",
"value" => ($item->imdbRating != "N/A" ? $item->imdbRating . "/10" : "N/A") . ($item->imdbVotes != "N/A" ? " (" . $item->imdbVotes . ")" : ""),
"short" => true,
),
),
),
),
);
}
return $payload;
}
/**
* UNUSED.
* Uses the search OMDB API (?s=)
* Returns multiple results with limited info
* @param string $q Search parameter
* @param int $limit Maximum number of responses to return
* @return array with Slackable response
*/
public function search($q, $limit = 5)
{
// Make URL proof
$q = urlencode($q);
// Call API
$items = $this->call('http://omdbapi.com/?s='.$q)->Search;
if (empty($items)) {
$payload = array(
"response_type" => "in_channel",
"text" => "Sorry I didn't find anything!",
);
} else {
// Prepare response
$payload = array(
"response_type" => "in_channel",
"text" => "This is what I've found for _".urldecode($q)."_:",
);
// Add results
foreach ($items as $item) {
if ($limit > $i) {
$payload['attachments'][] = array(
"title" => $item->Title . " (".$item->Year.")",
"title_link" => "http://www.imdb.com/title/".$item->imdbID."/",
"thumb_url" => ($item->Poster != "N/A" ? $item->Poster : null),
);
$i++;
}
}
}
return $payload;
}
/**
* Make a curl request to given $url and return json response
* @param string $search
* @return json_decoded contents of omdbapi
*/
private function call($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
return json_decode($response);
}
}