· 4 years ago · Jun 26, 2021, 01:34 PM
1<?php
2
3
4ini_set('error_reporting', E_ALL);
5ini_set('display_errors', 1);
6ini_set('display_startup_errors', 1);
7header("Access-Control-Allow-Origin: *");
8require_once __DIR__ . '/dbConnect/dbConnect.php';
9require_once __DIR__ . '/vendor/autoload.php';
10$dbh = connect_db();
11function DBquery($query, $type)
12{
13 global $dbh;
14 try {
15 if (!$type) {
16 $obj = $dbh->query($query)->fetchAll(\PDO::FETCH_ASSOC);//для селекта множества строк
17 return $obj;
18 } else if ($type == 1) {
19 $obj = $dbh->query($query)->fetch(\PDO::FETCH_ASSOC);//для селекта одной строки
20 return $obj;
21 } else
22 $dbh->query($query);//Для апдейта, инсерта и т.д
23 } catch (\Exception $ex) {
24
25 }
26}
27function logsg($text){
28 file_put_contents("log.txt",file_get_contents("log.txt")."\n$text");
29}
30$cfg = DBquery("SELECT * FROM `config` WHERE `id` = 1", 1);
31$api = $cfg['api_key'];
32$grp_key = $cfg['grp_key'];
33$year = date("Y");
34$page = rand(1,100);
35echo $page;
36$film = json_decode(file_get_contents("https://api1600771522.apicollaps.cc/list?token=$api&page=$page&type=films&year=$year"));
37$seria = json_decode(file_get_contents("https://api1600771522.apicollaps.cc/list?token=$api&page=$page&type=serials&year=$year"));
38$catoons = json_decode(file_get_contents("https://api1600771522.apicollaps.cc/list?token=$api&page=$page&type=cartoon&year=$year"));
39$fl->results = [];
40$sr->results = [];
41$cr->results = [];
42
43foreach ($film->results as $result) {
44 if ($result->poster !== null && $result->genre !== null /*&& $result->kinopoisk !== null*/) {
45 /*if (floor($result->kinopoisk) > 6.5)*/
46 array_push($fl->results, $result);
47 }
48}
49foreach ($seria->results as $result) {
50 if ($result->poster !== null && $result->genre !== null /*&& $result->kinopoisk !== null*/) {
51 /*if (floor($result->kinopoisk) > 6.5)*/
52 array_push($sr->results, $result);
53 }
54}
55foreach ($catoons->results as $result) {
56 if ($result->poster !== null && $result->genre !== null /*&& $result->kinopoisk !== null*/) {
57 /* if (floor($result->kinopoisk) > 6.5)*/
58 array_push($cr->results, $result);
59 }
60}
61function shuffle_assoc(&$array) {
62 $keys = array_keys($array);
63
64 shuffle($keys);
65
66 foreach($keys as $key) {
67 $new[$key] = $array[$key];
68 }
69
70 $array = $new;
71
72 return $array;
73}
74
75
76$fl->results = shuffle_assoc($fl->results);
77$sr->results = shuffle_assoc($sr->results);
78$cr->results = shuffle_assoc($cr->results);
79
80$rsel->results = [];
81$f = 0;
82$c = 0;
83$s = 0;
84for ($i = 0; $i < 4; $i++) {
85 if(count($rsel->results)<10) {
86 if (count($fl->results) - 1 >= $f)
87 array_push($rsel->results, $fl->results[$f]);
88 if (count($sr->results) - 1 >= $s)
89 array_push($rsel->results, $sr->results[$s]);
90 if (count($cr->results) - 1 >= $c)
91 array_push($rsel->results, $cr->results[$c]);
92 $f++;
93 $c++;
94 $s++;
95 }
96}
97
98$arr = [];
99function resize_image($file, $w, $h, $RF, $crop = FALSE)
100{
101 list($width, $height) = getimagesize($file);
102 $r = $width / $height;
103 if ($crop) {
104 if ($width > $height) {
105 $width = ceil($width - ($width * abs($r - $w / $h)));
106 } else {
107 $height = ceil($height - ($height * abs($r - $w / $h)));
108 }
109 $newwidth = $w;
110 $newheight = $h;
111 } else {
112 if ($w / $h > $r) {
113 $newwidth = $w;
114 $newheight = $h;
115 } else {
116 $newheight = $h;
117 $newwidth = $w;
118 }
119 }
120 $src = imagecreatefromjpeg($file);
121 $dst = imagecreatetruecolor($newwidth, $newheight);
122
123 imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
124 imagejpeg($dst, $RF, 100);
125 return $RF;
126}
127
128function uploadPhoto($post)
129{
130 $url = $post;
131 $time = time();
132 $bb = explode(".", $post)[count(explode(".", $post)) - 1];
133 $path = "./$time.$bb";
134 file_put_contents($path, file_get_contents($url));
135 $rr = resize_image($path, 480, 720, "./R$time.$bb");
136
137 $request_params = array(
138 'image_type' => '160x240',
139 'access_token' => "5b826847d3ab82d841eab49a698544d693622c55c84e1135fc9a4ee2e80beec3b1d2df8cd0fc1b27cb21e",
140 'v' => '5.95'
141 );
142 $url = 'https://api.vk.com/method/appWidgets.getGroupImageUploadServer';
143 $requestpost = file_get_contents($url, false, stream_context_create(array(
144 'http' => array(
145 'method' => 'POST',
146 'header' => 'Content-type: application/x-www-form-urlencoded',
147 'content' => http_build_query($request_params)
148 )
149 )));
150 $server = json_decode($requestpost)->response->upload_url;
151
152 $ch = curl_init($server);
153 curl_setopt($ch, CURLOPT_HEADER, false);
154 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
155 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
156 curl_setopt($ch, CURLOPT_POST, true);
157
158 if (class_exists('\CURLFile')) {
159 curl_setopt($ch, CURLOPT_POSTFIELDS, ['image' => new \CURLFile($rr)]);
160 } else {
161 curl_setopt($ch, CURLOPT_POSTFIELDS, ['image' => "@$rr"]);
162 }
163
164 $data = curl_exec($ch);
165 curl_close($ch);
166 unlink($path);
167 unlink($rr);
168 $resp = json_decode($data, true);
169 $request_params = array(
170 'hash' => $resp['hash'],
171 'image' => $resp['image'],
172 'access_token' => "5b826847d3ab82d841eab49a698544d693622c55c84e1135fc9a4ee2e80beec3b1d2df8cd0fc1b27cb21e",
173 'v' => '5.95'
174 );
175 $url = 'https://api.vk.com/method/appWidgets.saveGroupImage';
176 $requestpost = file_get_contents($url, false, stream_context_create(array(
177 'http' => array(
178 'method' => 'POST',
179 'header' => 'Content-type: application/x-www-form-urlencoded',
180 'content' => http_build_query($request_params)
181 )
182 )));
183 return json_decode($requestpost)->response->id;
184}
185
186foreach ($rsel->results as $result) {
187 if ($result->poster !== null && $result->genre !== null && count($arr) < 10) {
188 $name = html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", $result->name), ENT_NOQUOTES, 'UTF-8');
189 $genre = html_entity_decode(preg_replace("/U\+([0-9A-F]{4})/", "&#x\\1;", implode(",", json_decode(json_encode($result->genre), true))), ENT_NOQUOTES, 'UTF-8');
190 $id = uploadPhoto($result->poster);
191
192 array_push($arr, [
193 "title" => $name,
194 "descr" => $genre,
195 "icon_id" => $id,
196 "film_id" => $result->id,
197 'link' => 'Смотреть',
198 'link_url' => "https://vk.com/app7731606#$result->id",
199 'url' => "https://vk.com/app7731606#$result->id",
200 ]);
201 }
202}
203$films = json_encode($arr, JSON_UNESCAPED_UNICODE);
204
205/*$respU = $vk->users()->get($grp_key, [
206 "user_ids" => 'sasha_naye'
207]);
208var_dump($respU);*/
209$request_params = array(
210 'type' => 'tiles',
211 'access_token' => "5b826847d3ab82d841eab49a698544d693622c55c84e1135fc9a4ee2e80beec3b1d2df8cd0fc1b27cb21e",
212 'code' => 'return {
213 "title": "Фильмы",
214 "tiles": ' . $films . '
215};',
216 'v' => '5.95'
217);
218var_dump($films);
219
220echo "<br><br>";
221$url = 'https://api.vk.com/method/appWidgets.update';
222$requestpost = file_get_contents($url, false, stream_context_create(array(
223 'http' => array(
224 'method' => 'POST',
225 'header' => 'Content-type: application/x-www-form-urlencoded',
226 'content' => http_build_query($request_params)
227 )
228)));
229var_dump($requestpost);
230?>
231