From 55e1e9057b2ba7a1096b9b1ff804545ee09b20d5 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro Date: Tue, 24 May 2022 14:10:41 +0200 Subject: [PATCH] Updated wrapper --- app/Controllers/DiscoverController.php | 2 +- app/Controllers/MusicController.php | 12 ++++++---- app/Controllers/TagController.php | 20 +++++++++------- app/Controllers/TrendingController.php | 22 ++++++++++------- app/Controllers/UserController.php | 32 +++++++++++++++---------- app/Helpers/ErrorHandler.php | 3 ++- app/Helpers/Wrappers.php | 8 +++---- app/Models/ErrorTemplate.php | 4 +++- app/Models/FeedTemplate.php | 6 +++-- app/Models/FullTemplate.php | 16 +++++++++++++ components/themes/card.latte | 4 ++-- components/themes/common/controls.latte | 6 ++--- components/themes/default.latte | 4 ++-- composer.json | 4 ++-- composer.lock | 14 +++++------ views/discover.latte | 2 +- views/music.latte | 6 ++--- views/tag.latte | 8 +++---- views/user.latte | 10 ++++---- 19 files changed, 110 insertions(+), 73 deletions(-) create mode 100644 app/Models/FullTemplate.php diff --git a/app/Controllers/DiscoverController.php b/app/Controllers/DiscoverController.php index f5c2c3e..2087138 100644 --- a/app/Controllers/DiscoverController.php +++ b/app/Controllers/DiscoverController.php @@ -9,7 +9,7 @@ use App\Models\FeedTemplate; class DiscoverController { static public function get() { $api = Wrappers::api(); - $feed = $api->getDiscover(); + $feed = $api->discover(); if ($feed->meta->success) { $latte = Wrappers::latte(); $latte->render(Misc::getView('discover'), new FeedTemplate('Discover', $feed)); diff --git a/app/Controllers/MusicController.php b/app/Controllers/MusicController.php index 53aa51a..c0b80e6 100644 --- a/app/Controllers/MusicController.php +++ b/app/Controllers/MusicController.php @@ -4,19 +4,21 @@ namespace App\Controllers; use App\Helpers\ErrorHandler; use App\Helpers\Misc; use App\Helpers\Wrappers; -use App\Models\FeedTemplate; +use App\Models\FullTemplate; class MusicController { static public function get(string $music_id) { $cursor = Misc::getCursor(); $api = Wrappers::api(); - $feed = $api->getMusicFeed($music_id, $cursor); - if ($feed->meta->success) { + $music = $api->music($music_id); + $music->feed($cursor); + if ($music->ok()) { + $data = $music->getFull(); $latte = Wrappers::latte(); - $latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed)); + $latte->render(Misc::getView('music'), new FullTemplate('Music', $data)); } else { - ErrorHandler::show($feed->meta); + ErrorHandler::show($music->error()); } } } diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php index c2ed1c8..eca0345 100644 --- a/app/Controllers/TagController.php +++ b/app/Controllers/TagController.php @@ -4,28 +4,32 @@ namespace App\Controllers; use App\Helpers\ErrorHandler; use App\Helpers\Misc; use App\Helpers\Wrappers; -use App\Models\FeedTemplate; +use App\Models\FullTemplate; use App\Models\RSSTemplate; class TagController { static public function get(string $name) { $cursor = Misc::getCursor(); $api = Wrappers::api(); - $feed = $api->getHashtagFeed($name, $cursor); - if ($feed->meta->success) { + $hashtag = $api->hashtag($name); + $hashtag->feed($cursor); + if ($hashtag->ok()) { + $data = $hashtag->getFull(); $latte = Wrappers::latte(); - $latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); + $latte->render(Misc::getView('tag'), new FullTemplate('Tag', $data)); } else { - ErrorHandler::show($feed->meta); + ErrorHandler::show($hashtag->error()); } } static public function rss(string $name) { $api = Wrappers::api(); - $feed = $api->getHashtagFeed($name); - if ($feed->meta->success) { + $hashtag = $api->hashtag($name); + $hashtag->feed(); + if ($hashtag->ok()) { + $data = $hashtag->getFull(); $latte = Wrappers::latte(); - $latte->render(Misc::getView('rss'), new RSSTemplate($name, $feed->info->detail->desc, "/tag/{$name}", $feed->items)); + $latte->render(Misc::getView('rss'), new RSSTemplate($name, $data->info->detail->desc, "/tag/{$name}", $data->feed->items)); } } } diff --git a/app/Controllers/TrendingController.php b/app/Controllers/TrendingController.php index a0af76c..d514781 100644 --- a/app/Controllers/TrendingController.php +++ b/app/Controllers/TrendingController.php @@ -12,24 +12,30 @@ class TrendingController { $api = Wrappers::api(); // Ttwid if standard, cursor if legacy - if ($api::MODE === 'STANDARD') { - $cursor = Misc::getTtwid(); - } else { + if ($api->isLegacy()) { $cursor = Misc::getCursor(); + } else { + $cursor = Misc::getTtwid(); } - $feed = $api->getTrending($cursor); - if ($feed->meta->success) { + $trending = $api->trending(); + $trending->feed($cursor); + + $feed = $trending->getFeed(); + if ($feed && $feed->meta->success) { $latte = Wrappers::latte(); $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); } else { - ErrorHandler::show($feed->meta); + ErrorHandler::show($trending->error()); } } static public function rss() { $api = Wrappers::api(); - $feed = $api->getTrending(); - if ($feed->meta->success) { + $trending = $api->trending(); + $trending->feed(); + + $feed = $trending->getFeed(); + if ($feed && $feed->meta->success) { $latte = Wrappers::latte(); $latte->render(Misc::getView('rss'), new RSSTemplate('Trending', 'Trending on TikTok', '/trending', $feed->items)); } diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 8fe8d07..5e0b61b 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -4,7 +4,7 @@ namespace App\Controllers; use App\Helpers\ErrorHandler; use App\Helpers\Misc; use App\Helpers\Wrappers; -use App\Models\FeedTemplate; +use App\Models\FullTemplate; use App\Models\RSSTemplate; use App\Models\VideoTemplate; @@ -12,37 +12,43 @@ class UserController { static public function get(string $username) { $cursor = Misc::getCursor(); $api = Wrappers::api(); - $feed = $api->getUserFeed($username, $cursor); - if ($feed->meta->success) { - if ($feed->info->detail->privateAccount) { + $user = $api->user($username); + $user->feed($cursor); + if ($user->ok()) { + $data = $user->getFull(); + if ($data->info->detail->privateAccount) { http_response_code(403); echo 'Private account detected! Not supported'; exit; } $latte = Wrappers::latte(); - $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->nickname, $feed)); + $latte->render(Misc::getView('user'), new FullTemplate($data->info->detail->nickname, $data)); } else { - ErrorHandler::show($feed->meta); + ErrorHandler::show($user->error()); } } static public function video(string $username, string $video_id) { $api = Wrappers::api(); - $feed = $api->getVideoByID($video_id); - if ($feed->meta->success) { + $video = $api->video($video_id); + $video->feed(); + if ($video->ok()) { + $data = $video->getFull(); $latte = Wrappers::latte(); - $latte->render(Misc::getView('video'), new VideoTemplate($feed->items[0], $feed->info->detail)); + $latte->render(Misc::getView('video'), new VideoTemplate($data->feed->items[0], $data->info->detail)); } else { - ErrorHandler::show($feed->meta); + ErrorHandler::show($video->error()); } } static public function rss(string $username) { $api = Wrappers::api(); - $feed = $api->getUserFeed($username); - if ($feed->meta->success) { + $user = $api->user($username); + $user->feed(); + if ($user->ok()) { + $data = $user->getFull(); $latte = Wrappers::latte(); - $latte->render(Misc::getView('rss'), new RSSTemplate($username, $feed->info->detail->signature, '/@' . $username, $feed->items)); + $latte->render(Misc::getView('rss'), new RSSTemplate($username, $data->info->detail->signature, '/@' . $username, $data->feed->items)); } } } diff --git a/app/Helpers/ErrorHandler.php b/app/Helpers/ErrorHandler.php index 139092e..4010182 100644 --- a/app/Helpers/ErrorHandler.php +++ b/app/Helpers/ErrorHandler.php @@ -2,9 +2,10 @@ namespace App\Helpers; use App\Models\ErrorTemplate; +use TikScraper\Models\Meta; class ErrorHandler { - static public function show(object $meta) { + static public function show(Meta $meta) { http_response_code($meta->http_code); $latte = Wrappers::latte(); $latte->render(Misc::getView('error'), new ErrorTemplate($meta)); diff --git a/app/Helpers/Wrappers.php b/app/Helpers/Wrappers.php index 71caf40..5d4d816 100644 --- a/app/Helpers/Wrappers.php +++ b/app/Helpers/Wrappers.php @@ -42,11 +42,10 @@ class Wrappers { return $latte; } - /** + /** * Setup of TikTok Api wrapper - * @return \TikScraper\Api|\TikScraper\Legacy */ - static public function api() { + static public function api(): \TikScraper\Api { $options = [ 'use_test_endpoints' => Misc::env('API_TEST_ENDPOINTS', false), 'signer' => [ @@ -84,7 +83,6 @@ class Wrappers { // Legacy mode $legacy = Misc::env('API_FORCE_LEGACY', false) || isset($_COOKIE['api-legacy']) && $_COOKIE['api-legacy'] === 'on'; - - return $legacy === false ? new \TikScraper\Api($options, $cacheEngine) : new \TikScraper\Legacy($options, $cacheEngine); + return new \TikScraper\Api($options, $legacy, $cacheEngine); } } diff --git a/app/Models/ErrorTemplate.php b/app/Models/ErrorTemplate.php index 5b51b2d..ad38f9f 100644 --- a/app/Models/ErrorTemplate.php +++ b/app/Models/ErrorTemplate.php @@ -1,8 +1,10 @@ feed = $feed; + $this->data = (object) [ + 'feed' => $feed + ]; } } diff --git a/app/Models/FullTemplate.php b/app/Models/FullTemplate.php new file mode 100644 index 0000000..23c7888 --- /dev/null +++ b/app/Models/FullTemplate.php @@ -0,0 +1,16 @@ +data = $data; + } +} diff --git a/components/themes/card.latte b/components/themes/card.latte index 50ee90e..5d80609 100644 --- a/components/themes/card.latte +++ b/components/themes/card.latte @@ -2,7 +2,7 @@
- {foreach $feed->items as $item} + {foreach $data->feed->items as $item} {do $share_url = 'https://tiktok.com/@' . $item->author->uniqueId . '/video/' . $item->id}
{/foreach} - {if empty($feed->items)} + {if empty($data->feed->items)}

No items sent by TikTok!

{/if}
diff --git a/components/themes/common/controls.latte b/components/themes/common/controls.latte index 2aa4489..bb0aad5 100644 --- a/components/themes/common/controls.latte +++ b/components/themes/common/controls.latte @@ -1,8 +1,8 @@ -
+
{* is_numeric is used to avoid having a back button with ttwid cursors *} {if isset($_GET['cursor']) && is_numeric($_GET['cursor']) && $_GET['cursor'] != 0 } First - Back + Back {/if} - Next + Next
diff --git a/components/themes/default.latte b/components/themes/default.latte index 6b07517..0ca5149 100644 --- a/components/themes/default.latte +++ b/components/themes/default.latte @@ -1,5 +1,5 @@
- {foreach $feed->items as $item} + {foreach $data->feed->items as $item}

@@ -29,7 +29,7 @@

{/foreach} - {if empty($feed->items)} + {if empty($data->feed->items)}

No items sent by TikTok!

{/if}
diff --git a/composer.json b/composer.json index 1c34e99..0a30295 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "pablouser1/proxitok", "description": "An alternative frontend for TikTok", - "version": "2.2.2.0", + "version": "2.3.0.0", "license": "AGPL-3.0-or-later", "type": "project", "authors": [ @@ -24,7 +24,7 @@ "latte/latte": "^2.11", "bramus/router": "^1.6", "josegonzalez/dotenv": "dev-master", - "pablouser1/tikscraper": "^1.3" + "pablouser1/tikscraper": "^2.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 1e7a272..f0ec56b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f1c0945357108862633ea87a1f6bbdd8", + "content-hash": "e0e24f4588d6e34423fa8931ae8ae9a3", "packages": [ { "name": "bramus/router", @@ -263,16 +263,16 @@ }, { "name": "pablouser1/tikscraper", - "version": "v1.3.5.2", + "version": "v2.0.1.3", "source": { "type": "git", "url": "https://github.com/pablouser1/TikScraperPHP.git", - "reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf" + "reference": "45812af395b376dff6835af192827f34ba7a4cf9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/cb2580e9868cd4f77f3ab93987786fbd57e4efaf", - "reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf", + "url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/45812af395b376dff6835af192827f34ba7a4cf9", + "reference": "45812af395b376dff6835af192827f34ba7a4cf9", "shasum": "" }, "require": { @@ -298,9 +298,9 @@ "description": "Get data from TikTok API", "support": { "issues": "https://github.com/pablouser1/TikScraperPHP/issues", - "source": "https://github.com/pablouser1/TikScraperPHP/tree/v1.3.5.2" + "source": "https://github.com/pablouser1/TikScraperPHP/tree/v2.0.1.3" }, - "time": "2022-05-22T09:33:26+00:00" + "time": "2022-05-24T12:08:22+00:00" }, { "name": "php-webdriver/webdriver", diff --git a/views/discover.latte b/views/discover.latte index 5780744..93b61eb 100644 --- a/views/discover.latte +++ b/views/discover.latte @@ -5,7 +5,7 @@ {/block} {block content} - {foreach $feed->items as $type => $items} + {foreach $data->feed->items as $type => $items}

{$type|firstUpper}

{foreach $items as $item} diff --git a/views/music.latte b/views/music.latte index 04b6806..1c1f3ec 100644 --- a/views/music.latte +++ b/views/music.latte @@ -1,9 +1,9 @@ {layout '../layouts/default.latte'} {block header} -

{$feed->info->detail->title}

-

{$feed->info->detail->desc}

-

Videos: {number($feed->info->stats->videoCount)}

+

{$data->info->detail->title}

+

{$data->info->detail->desc}

+

Videos: {number($data->info->stats->videoCount)}

{/block} {block content} diff --git a/views/tag.latte b/views/tag.latte index f2f70b4..82853b2 100644 --- a/views/tag.latte +++ b/views/tag.latte @@ -1,14 +1,14 @@ {layout '../layouts/default.latte'} {block header} - {if $feed->info->detail->profileThumb !== ''} + {if $data->info->detail->profileThumb !== ''}
- +
{/if} -

{$feed->info->detail->title}

+

{$data->info->detail->title}

{include '../components/rss.latte'}

-

Videos: {number($feed->info->stats->videoCount)} / Views: {number($feed->info->stats->viewCount)}

+

Videos: {number($data->info->stats->videoCount)} / Views: {number($data->info->stats->viewCount)}

{/block} {block content} diff --git a/views/user.latte b/views/user.latte index 91d45b8..2eb68b7 100644 --- a/views/user.latte +++ b/views/user.latte @@ -2,13 +2,13 @@ {block header}
- +
-

{$feed->info->detail->uniqueId}

+

{$data->info->detail->uniqueId}

{include '../components/rss.latte'}

-

{$feed->info->detail->signature}

-

Following: {number($feed->info->stats->followingCount)} / Followers: {number($feed->info->stats->followerCount)}

-

Hearts: {number($feed->info->stats->heartCount)} / Videos: {$feed->info->stats->videoCount}

+

{$data->info->detail->signature}

+

Following: {number($data->info->stats->followingCount)} / Followers: {number($data->info->stats->followerCount)}

+

Hearts: {number($data->info->stats->heartCount)} / Videos: {$data->info->stats->videoCount}

{/block} {block content}