Error handler and allow video IDs from app share

This commit is contained in:
Pablo Ferreiro 2022-01-11 19:25:30 +01:00
parent f18dd802d5
commit 93de0c2d23
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
9 changed files with 107 additions and 20 deletions

View file

@ -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"
]
}
}

2
composer.lock generated
View file

@ -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",

25
helpers/Error.php Normal file
View file

@ -0,0 +1,25 @@
<?php
namespace Helpers;
class Error {
const list = [
'api' => [
'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]);
}
}
}

View file

@ -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 +++++++---------

View file

@ -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 ++-

View file

@ -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 <pferreiromero@gmail.com>
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

View file

@ -0,0 +1,33 @@
From e8dcb7cf3824803d8b431dc923c479cd10102cbc Mon Sep 17 00:00:00 2001
From: Pablo Ferreiro <pferreiromero@gmail.com>
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

View file

@ -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');
}
});

22
views/error.latte Normal file
View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
{include 'components/head.latte', title: 'ERROR'}
<body>
<section class="hero is-danger is-fullheight">
<div class="hero-head">
{include 'components/navbar.latte'}
</div>
<div class="hero-body">
<div class="container">
<p class="title">{$type|firstUpper} error</p>
<p class="subtitle">{$error['message']}</p>
</div>
</div>
<div class="hero-foot is-danger">
{include 'components/footer.latte'}
</div>
</section>
</body>
</html>