-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetflixBotCommand.php
More file actions
176 lines (159 loc) · 6.55 KB
/
Copy pathNetflixBotCommand.php
File metadata and controls
176 lines (159 loc) · 6.55 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
173
174
175
176
<?php
/**
* Class NetflixBotCommand
* This class returns list of movies or series available in Netflix, from the given movie or series name.
*
* Usage:
* NETFLIX <Movie or Series Name>
*
* @author: Angelito Sardez, Jr.
* @date: 16/11/2017
*/
class NetflixBotCommand extends BotCommand {
public function __construct($sender, $user) {
parent::__construct("NETFLIX", $sender, $user);
}
protected function executeCommand($parameter) {
$pageNum = $this->getPageNum($parameter);
$keyword = $this->getKeyword($pageNum, $parameter);
if (trim($keyword) == "") {
$this->sendTextWithHelp("Sorry ".$this->user->getFirstName().", you need to specify the Netflix movie or series title that you want to search.");
return;
}
$titles = $this->queryTitles($pageNum, $keyword);
if ($titles['COUNT'] == 0) {
$this->send("No movie or series found with the title \"".$keyword."\", ".$this->user->getFirstName().". The movie or series you're looking for is not in Netflix.");
}
else {
$this->sendTitles($pageNum, $keyword, $titles);
}
}
function getPageNum($parameter) {
if (trim($parameter) == '') {
return 1;
}
else {
if (!strpos(trim($parameter), '~!@#')) {
return 1;
}
else {
return substr(trim($parameter), 0, strpos(trim($parameter), '~!@#'));
}
}
}
function getKeyword($pageNum, $parameter) {
if (trim($parameter) == '') {
return '';
}
else {
return ltrim(trim($parameter), $pageNum.'~!@#');
}
}
function queryTitles($pageNum, $title) {
$header = [
"http" => [
"method" => "GET",
"header" => "Accept: application/json, text/javascript, */*; q=0.01\r\n".
"Accept-Encoding: gzip, deflate, br\r\n".
"Accept-Language: en-US,en;q=0.9\r\n".
"Connection: keep-alive\r\n".
"Host: unogs.com\r\n".
"Referer: https://unogs.com/?q=".urlencode($title)."&st=b\r\n".
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36\r\n".
"X-Requested-With: XMLHttpRequest"
]
];
$headerContext = stream_context_create($header);
return json_decode(file_get_contents('https://unogs.com/nf.cgi?u=vgrv6hjhe2tlajqj6o7cjnhe71&q='.urlencode($title)."&t=ns&cl=&st=bs&ob=&p=".$pageNum."&l=8&inc=&ao=and", false, $headerContext), true);
}
function getResponseTemplate() {
return ["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"generic",
"elements"=>array()
]
]];
}
function getCountFrom($pageNum) {
return (($pageNum - 1) * 8) + 1;
}
function getCountTo($pageNum, $currentCount, $totalCount) {
$countTo = (($pageNum - 1) * 8) + $currentCount;
if ($countTo > $totalCount) {
$countTo = $totalCount;
}
return $countTo;
}
function getResultsCountRatioTerm($pageNum, $currentCount, $totalCount) {
return $this->getCountFrom($pageNum)." to ".$this->getCountTo($pageNum, $currentCount, $totalCount)." of ".$totalCount;
}
function getSetRatioTerm($pageNum, $totalCount) {
return "Set ".$pageNum." of ".(ceil($totalCount / 8));
}
function sendTitles($pageNum, $title, $titles) {
$this->send("Displaying titles ".
$this->getResultsCountRatioTerm($pageNum, count($titles['ITEMS']), $titles['COUNT']).
" (".$this->getSetRatioTerm($pageNum, $titles['COUNT']).
") found in Netflix that matched \"".$title."\":");
$this->sendAction(SenderAction::typingOn);
$responseTemplate = $this->getResponseTemplate();
if ($pageNum > 1) {
$responseTemplate["attachment"]["payload"]["elements"][] = [
"title"=>'See previous results',
"image_url"=>'https://is238-group5.cf/bot/images/BackArrow.jpg',
"subtitle"=>'Display the previous or first set of titles.',
"buttons"=>[
[
"type"=>'postback',
"title"=>'Previous Titles',
"payload"=>$this->command.' '.($pageNum - 1).'~!@#'.$title
],
[
"type"=>'postback',
"title"=>'First Titles',
"payload"=>$this->command.' 1~!@#'.$title
]
]
];
}
foreach ($titles['ITEMS'] as &$item) {
$responseTemplate["attachment"]["payload"]["elements"][] = [
"title"=>html_entity_decode(strip_tags($item[1]), ENT_QUOTES)." (".ucfirst($item[6]).", ".$item[7].")",
"image_url"=>($item[2] == '') ? 'https://is238-group5.cf/bot/images/NoImageAvailable.jpg' : $item[2],
"subtitle"=>html_entity_decode(strip_tags($item[3]), ENT_QUOTES),
"default_action"=>[
"type"=>'web_url',
"url"=>'https://unogs.com/video/?v='.$item[4]
],
"buttons"=>[
[
"type"=>'web_url',
"url"=>'https://unogs.com/video/?v='.$item[4],
"title"=>'View Details'
]
]
];
}
if (($titles['COUNT'] - ($pageNum * 8)) > 0) {
$responseTemplate["attachment"]["payload"]["elements"][] = [
"title"=>'See next results',
"image_url"=>'https://is238-group5.cf/bot/images/NextArrow.jpg',
"subtitle"=>'Display the next or last set of titles.',
"buttons"=>[
[
"type"=>'postback',
"title"=>'Next Titles',
"payload"=>$this->command.' '.($pageNum + 1).'~!@#'.$title
],
[
"type"=>'postback',
"title"=>'Last Titles',
"payload"=>$this->command.' '.(ceil($titles['COUNT'] / 8)).'~!@#'.$title
]
]
];
}
$this->send($responseTemplate);
}
}