From 93de0c2d2322b733a69ab39ac9369648fe2f6373 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro Date: Tue, 11 Jan 2022 19:25:30 +0100 Subject: [PATCH] Error handler and allow video IDs from app share --- composer.json | 5 +-- composer.lock | 2 +- helpers/Error.php | 25 ++++++++++++++ patches/0001-Fixed-getUser.patch | 2 +- ...port-for-username-as-well-as-user-id.patch | 2 +- patches/0003-Fixed-getVideoByUrl.patch | 17 ++++++---- ...004-Allow-IDs-from-Android-iOS-share.patch | 33 +++++++++++++++++++ routes/index.php | 19 ++++++----- views/error.latte | 22 +++++++++++++ 9 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 helpers/Error.php create mode 100644 patches/0004-Allow-IDs-from-Android-iOS-share.patch create mode 100644 views/error.latte diff --git a/composer.json b/composer.json index 96d519d..22c924c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "pablouser1/tiktok-viewer", "description": "An alternative frontend for TikTok", - "version": "1.1.0", + "version": "1.1.1", "license": "AGPL-3.0-or-later", "require": { "ext-curl": "*", @@ -26,7 +26,8 @@ "ssovit/tiktok-api": [ "patches/0001-Fixed-getUser.patch", "patches/0002-Added-support-for-username-as-well-as-user-id.patch", - "patches/0003-Fixed-getVideoByUrl.patch" + "patches/0003-Fixed-getVideoByUrl.patch", + "patches/0004-Allow-IDs-from-Android-iOS-share.patch" ] } } diff --git a/composer.lock b/composer.lock index 9b43fa4..357e933 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": "f82588da7787761ecda16112b8c1d1b1", + "content-hash": "0d80b92323fe946eed65f7e1c64f67a4", "packages": [ { "name": "cweagans/composer-patches", diff --git a/helpers/Error.php b/helpers/Error.php new file mode 100644 index 0000000..ce521a5 --- /dev/null +++ b/helpers/Error.php @@ -0,0 +1,25 @@ + [ + 'code' => 500, + 'message' => 'API unknown error, please check back later' + ], + 'latte' => [ + 'code' => 500, + 'message' => 'Template render crash, please check back later' + ] + ]; + + static public function show(string $type) { + $keys = array_keys(self::list); + if (in_array($type, $keys)) { + $error = self::list[$type]; + http_response_code($error['code']); + $latte = Misc::latte(); + $latte->render(Misc::getView('error'), ['type' => $type, 'error' => $error]); + } + } +} diff --git a/patches/0001-Fixed-getUser.patch b/patches/0001-Fixed-getUser.patch index b0023ae..04919d2 100644 --- a/patches/0001-Fixed-getUser.patch +++ b/patches/0001-Fixed-getUser.patch @@ -1,7 +1,7 @@ From 7c52f9291fbd15d912fa78d0301939e3aa9deec4 Mon Sep 17 00:00:00 2001 From: attend-dunce <96889312+attend-dunce@users.noreply.github.com> Date: Thu, 30 Dec 2021 21:35:14 +0100 -Subject: [PATCH 1/3] Fixed `getUser` +Subject: [PATCH 1/4] Fixed `getUser` --- lib/TikTok/Api.php | 16 +++++++--------- diff --git a/patches/0002-Added-support-for-username-as-well-as-user-id.patch b/patches/0002-Added-support-for-username-as-well-as-user-id.patch index d2294f9..afb4d85 100644 --- a/patches/0002-Added-support-for-username-as-well-as-user-id.patch +++ b/patches/0002-Added-support-for-username-as-well-as-user-id.patch @@ -1,7 +1,7 @@ From c948bda5d6f30929b448fe78a0920739d5792d4b Mon Sep 17 00:00:00 2001 From: attend-dunce <96889312+attend-dunce@users.noreply.github.com> Date: Fri, 31 Dec 2021 00:55:23 +0100 -Subject: [PATCH 2/3] Added support for username as well as user id +Subject: [PATCH 2/4] Added support for username as well as user id --- lib/TikTok/Api.php | 3 ++- diff --git a/patches/0003-Fixed-getVideoByUrl.patch b/patches/0003-Fixed-getVideoByUrl.patch index ae3a809..3936085 100644 --- a/patches/0003-Fixed-getVideoByUrl.patch +++ b/patches/0003-Fixed-getVideoByUrl.patch @@ -1,14 +1,14 @@ -From c23e8dbf0bd722799cf19182142d4b177f8fd933 Mon Sep 17 00:00:00 2001 +From 816ebe168d12923944b81b3f10bb22baf58efd66 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro -Date: Sat, 8 Jan 2022 15:47:21 +0100 -Subject: [PATCH 3/3] Fixed getVideoByUrl +Date: Tue, 11 Jan 2022 19:09:08 +0100 +Subject: [PATCH 3/4] Fixed getVideoByUrl --- - lib/TikTok/Api.php | 19 +++++++++++++------ - 1 file changed, 13 insertions(+), 6 deletions(-) + lib/TikTok/Api.php | 20 ++++++++++++++------ + 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/TikTok/Api.php b/lib/TikTok/Api.php -index 8fd42ae..f800555 100644 +index 8fd42ae..21ebff1 100644 --- a/lib/TikTok/Api.php +++ b/lib/TikTok/Api.php @@ -483,20 +483,27 @@ if (!\class_exists('\Sovit\TikTok\Api')) { @@ -45,6 +45,11 @@ index 8fd42ae..f800555 100644 ]; if ($this->cacheEnabled) { $this->cacheEngine->set($cacheKey, $result, $this->_config['cache-timeout']); +@@ -590,3 +597,4 @@ if (!\class_exists('\Sovit\TikTok\Api')) { + } + } + } ++ -- 2.34.1 diff --git a/patches/0004-Allow-IDs-from-Android-iOS-share.patch b/patches/0004-Allow-IDs-from-Android-iOS-share.patch new file mode 100644 index 0000000..8624e98 --- /dev/null +++ b/patches/0004-Allow-IDs-from-Android-iOS-share.patch @@ -0,0 +1,33 @@ +From e8dcb7cf3824803d8b431dc923c479cd10102cbc Mon Sep 17 00:00:00 2001 +From: Pablo Ferreiro +Date: Tue, 11 Jan 2022 19:18:16 +0100 +Subject: [PATCH 4/4] Allow IDs from Android/iOS share + +--- + lib/TikTok/Api.php | 10 +++++++++- + 1 file changed, 9 insertions(+), 1 deletion(-) + +diff --git a/lib/TikTok/Api.php b/lib/TikTok/Api.php +index 21ebff1..f83f5bd 100644 +--- a/lib/TikTok/Api.php ++++ b/lib/TikTok/Api.php +@@ -462,7 +462,15 @@ if (!\class_exists('\Sovit\TikTok\Api')) { + if (empty($video_id)) { + throw new \Exception("Invalid VIDEO ID"); + } +- return $this->getVideoByUrl('https://m.tiktok.com/v/' . $video_id . '.html'); ++ ++ $url = ''; ++ if (is_numeric($video_id)) { ++ $url = 'https://m.tiktok.com/v/' . $video_id . '.html'; ++ } else { ++ $url = 'https://vm.tiktok.com/' . $video_id; ++ } ++ ++ return $this->getVideoByUrl($url); + } + /** + * Get Video By URL +-- +2.34.1 + diff --git a/routes/index.php b/routes/index.php index fa78cc5..34dfb8b 100644 --- a/routes/index.php +++ b/routes/index.php @@ -4,6 +4,7 @@ require __DIR__ . '/settings.php'; require __DIR__ . '/following.php'; use Steampixel\Route; use Helpers\Misc; +use Helpers\Error; Route::add('/', function () { $latte = Misc::latte(); @@ -20,13 +21,13 @@ Route::add("/trending", function () { if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { $cursor = (int) $_GET['cursor']; } - $latte = Misc::latte(); $api = Misc::api(); $feed = $api->getTrendingFeed($cursor); if ($feed) { + $latte = Misc::latte(); $latte->render(Misc::getView('trending'), ['feed' => $feed]); } else { - return 'ERROR!'; + Error::show('api'); } }); @@ -35,7 +36,6 @@ Route::add("/@([^/]+)", function (string $username) { if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { $cursor = (int) $_GET['cursor']; } - $latte = Misc::latte(); $api = Misc::api(); $feed = $api->getUserFeed($username, $cursor); if ($feed) { @@ -43,20 +43,21 @@ Route::add("/@([^/]+)", function (string $username) { http_response_code(400); return 'Private account detected! Not supported'; } + $latte = Misc::latte(); $latte->render(Misc::getView('user'), ['feed' => $feed]); } else { - return 'ERROR!'; + Error::show('api'); } }); -Route::add('/video/(\d+)', function (string $video_id) { - $latte = Misc::latte(); +Route::add('/video/([^/]+)', function (string $video_id) { $api = Misc::api(); $item = $api->getVideoByID($video_id); if ($item) { + $latte = Misc::latte(); $latte->render(Misc::getView('video'), ['item' => $item]); } else { - return 'ERROR!'; + Error::show('api'); } }); @@ -65,12 +66,12 @@ Route::add('/tag/(\w+)', function (string $name) { if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { $cursor = (int) $_GET['cursor']; } - $latte = Misc::latte(); $api = Misc::api(); $feed = $api->getChallengeFeed($name, $cursor); if ($feed) { + $latte = Misc::latte(); $latte->render(Misc::getView('tag'), ['feed' => $feed]); } else { - return 'ERROR!'; + Error::show('api'); } }); diff --git a/views/error.latte b/views/error.latte new file mode 100644 index 0000000..782e150 --- /dev/null +++ b/views/error.latte @@ -0,0 +1,22 @@ + + + +{include 'components/head.latte', title: 'ERROR'} + + +
+
+ {include 'components/navbar.latte'} +
+
+
+

{$type|firstUpper} error

+

{$error['message']}

+
+
+
+ {include 'components/footer.latte'} +
+
+ +