From 4f542c99cfe1d94cfb67c7021ae6feab134767cf Mon Sep 17 00:00:00 2001 From: themohooks <81331307+themohooks@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:41:10 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=B2=D0=BC=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D1=8C=20=D1=81=20api=20transphot?= =?UTF-8?q?o.org?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controllers/Api/Images/LoadRecent.php | 72 +++++++++++++++++++++++ app/Controllers/ApiController.php | 4 ++ app/Core/Routes.php | 2 +- app/Services/HTMLParser.php | 43 ++++++++++++++ views/pages/Photo.php | 2 +- 5 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 app/Controllers/Api/Images/LoadRecent.php create mode 100644 app/Services/HTMLParser.php diff --git a/app/Controllers/Api/Images/LoadRecent.php b/app/Controllers/Api/Images/LoadRecent.php new file mode 100644 index 0000000..4173a5a --- /dev/null +++ b/app/Controllers/Api/Images/LoadRecent.php @@ -0,0 +1,72 @@ + $p['user_id']))[0]; + + $response[] = [ + 'id' => $p['id'], + 'place' => htmlspecialchars($p['place']), + 'date' => $date, + 'user_name' => $user['username'], + 'user_id' => $p['user_id'], + 'photourl' => $p['photourl'], + 'photourl_small' => 'https://' . $_SERVER['SERVER_NAME'] . '/api/photo/compress?url=' . $p['photourl'] + ]; + } + } else { + $url = 'https://transphoto.org/api.php?action=get-recent-photos&width=802&lastpid=0&hidden=0'; + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $responsed = curl_exec($ch); + if (curl_errno($ch)) { + $response = [ + 'error' => 1, + 'errorcode' => 'СТТС не отвечает. Попробуйте позже' + ]; + } else { + $data = json_decode($responsed, true); + foreach ($data as $d) { + $response[] = [ + 'id' => $d['pid'], + 'place' => strip_tags($d['links']), + 'date' => $d['pdate'], + 'photourl_small' => 'https://transphoto.org'.$d['prw'], + ]; + } + } + + curl_close($ch); + + + + } + + + header('Content-Type: application/json'); + echo json_encode($response); + } +} diff --git a/app/Controllers/ApiController.php b/app/Controllers/ApiController.php index c9934b6..d4f903a 100644 --- a/app/Controllers/ApiController.php +++ b/app/Controllers/ApiController.php @@ -10,6 +10,7 @@ use \App\Controllers\Api\Images\{Upload}; use \App\Controllers\Api\Images\Rate as PhotoVote; use \App\Controllers\Api\Images\Compress as PhotoCompress; use \App\Controllers\Api\Images\CheckAll as PhotoCheckAll; +use \App\Controllers\Api\Images\LoadRecent as PhotoLoadRecent; use \App\Controllers\Api\Images\Stats as PhotoStats; use \App\Controllers\Api\Images\Comments\Create as PhotoComment; use \App\Controllers\Api\Images\Comments\Load as PhotoCommentLoad; @@ -58,6 +59,9 @@ class ApiController public static function checkallphotos() { return new PhotoCheckAll(); } + public static function recentphotos() { + return new PhotoLoadRecent(); + } public static function photostats() { return new PhotoStats(); } diff --git a/app/Core/Routes.php b/app/Core/Routes.php index 00ca6f9..eab679e 100644 --- a/app/Core/Routes.php +++ b/app/Core/Routes.php @@ -30,7 +30,7 @@ class Routes Router::get('/top30', 'MainController@top30'); Router::get('/photoext', 'PhotoController@photoext'); Router::get('/api/photo/compress', 'ApiController@photocompress'); - + Router::get('/api/photo/loadrecent', 'ApiController@recentphotos'); diff --git a/app/Services/HTMLParser.php b/app/Services/HTMLParser.php new file mode 100644 index 0000000..2e64a14 --- /dev/null +++ b/app/Services/HTMLParser.php @@ -0,0 +1,43 @@ +loadHTML($response); + + $xpath = new DOMXPath($dom); + + $nodes = $xpath->query($mask); + + if ($nodes->length > 0) { + $firstNode = $nodes->item(0)->textContent; + $cleanedContent = ltrim($firstNode); + return $cleanedContent; + } + + + } +} diff --git a/views/pages/Photo.php b/views/pages/Photo.php index b464ab3..aebc909 100644 --- a/views/pages/Photo.php +++ b/views/pages/Photo.php @@ -6,7 +6,7 @@ use App\Models\{User, Vote, Comment}; $id = explode('/', $_SERVER['REQUEST_URI'])[2]; $photo = new \App\Models\Photo($id); if ($photo->i('id') !== null) { - if ($photo->content('video') != 'null') { + if ($photo->content('video') != null) { $extname = 'видео'; $extnamef = 'видеоролик'; } else {