diff --git a/.env.example b/.env.example index ea615e7..29e3e5c 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,4 @@ # APP_URL="http://localhost:8000" # Full url path, PLEASE REPLACE TO YOUR OWN ONE -# LATTE_CACHE=/tmp/proxitok_api # Path for Latte cache, leave commented for ./cache/latte # API CONFIG # USE_TEST_ENDPOINTS=1 # Discomment for usage of testing TikTok endpoints, may help sometimes @@ -11,9 +10,10 @@ # PROXY_PORT=8080 # PROXY_USERNAME=USERNAME # PROXY_PASSWORD=PASSWORD +# LATTE_CACHE=/tmp/proxitok_api # Path for Latte cache, leave commented for ./cache/latte # API_CACHE=redis # Cache engine for TikTok Api, (more info on README) -# Redis cache, used on Helpers\CacheEngines\RedisCache +# Redis cache, used on Helpers\CacheEngines\RedisCache (CHOOSE ONE) # REDIS_HOST=localhost # Host or path to unix socket # REDIS_PORT=6379 # Set to -1 to use unix socket # REDIS_PASSWORD=example # Leave commented for no password diff --git a/README.md b/README.md index db30d92..52812ff 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,24 @@ Use Tiktok with an alternative frontend, inspired by Nitter. * See tags * See video by id * Discovery -* Create a following list, which you can later use to see all the feeds from those users * RSS Feed for user, trending and tag (just add /rss to the url) +## Extension +If you want to automatically redirect Tiktok links to ProxiTok you can use: +* [Libredirect](https://github.com/libredirect/libredirect) +* [Redirector](https://github.com/einaregilsson/Redirector) + +You can use the following config if you want to use Redirector (you can change https://proxitok.herokuapp.com with whatever instance you want to use): +``` +Description: TikTok to ProxiTok +Example URL: https://www.tiktok.com/@tiktok +Include pattern: (.*//.*)(tiktok.com)(.*) +Redirect to: https://proxitok.herokuapp.com$3 +Example result: https://proxitok.herokuapp.com/@tiktok +Pattern type: Regular Expression +Apply to: Main window (address bar) +``` + ## Installation Clone the repository and fetch the requiered external packages with: ```bash @@ -60,7 +75,7 @@ location /proxitok/.env { * i18n ## Credits -* [TikScraper](https://github.com/pablouser1/TikScraperPHP) +* [TikTok-API-PHP](https://github.com/ssovit/TikTok-API-PHP) (Currently using my personal fork) * [Latte](https://github.com/nette/latte) * [bramus/router](https://github.com/bramus/router) * [PHP dotenv](https://github.com/vlucas/phpdotenv) diff --git a/app/Cache/JSONCache.php b/app/Cache/JSONCache.php index f113433..7b16c60 100644 --- a/app/Cache/JSONCache.php +++ b/app/Cache/JSONCache.php @@ -1,31 +1,33 @@ cache_path = Misc::env('API_CACHE_JSON', __DIR__ . '/../../cache/api'); + if (isset($_ENV['API_CACHE_JSON']) && !empty($_ENV['API_CACHE_JSON'])) { + $this->cache_path = $_ENV['API_CACHE_JSON']; + } } - - public function get(string $cache_key): ?object { + public function get(string $cache_key): object|false { $filename = $this->cache_path . '/' . $cache_key . '.json'; if (is_file($filename)) { + $time = time(); $json_string = file_get_contents($filename); $element = json_decode($json_string); - return $element; + if ($time < $element->expires) { + return $element->data; + } + // Remove file if expired + unlink($filename); } - return null; + return false; } - public function exists(string $cache_key): bool { - $filename = $this->cache_path . '/' . $cache_key . '.json'; - return is_file($filename); - } - - public function set(string $cache_key, string $data, $timeout = 3600) { - file_put_contents($this->cache_path . '/' . $cache_key . '.json', $data); + public function set(string $cache_key, mixed $data, $timeout = 3600) { + file_put_contents($this->cache_path . '/' . $cache_key . '.json', json_encode([ + 'data' => $data, + 'expires' => time() + $timeout + ])); } } diff --git a/app/Cache/RedisCache.php b/app/Cache/RedisCache.php index 82063a0..55a6dca 100644 --- a/app/Cache/RedisCache.php +++ b/app/Cache/RedisCache.php @@ -27,11 +27,7 @@ class RedisCache { return null; } - public function exists(string $cache_key): bool { - return $this->client->exists($cache_key); - } - - public function set(string $cache_key, string $data, $timeout = 3600) { - $this->client->set($cache_key, $data, $timeout); + public function set(string $cache_key, array $data, $timeout = 3600) { + $this->client->set($cache_key, json_encode($data), $timeout); } } diff --git a/app/Controllers/DiscoverController.php b/app/Controllers/DiscoverController.php index dbd6e70..ea564e2 100644 --- a/app/Controllers/DiscoverController.php +++ b/app/Controllers/DiscoverController.php @@ -3,7 +3,7 @@ namespace App\Controllers; use App\Helpers\ErrorHandler; use App\Helpers\Misc; -use App\Models\DiscoverTemplate; +use App\Models\FeedTemplate; class DiscoverController { static public function get() { @@ -11,7 +11,7 @@ class DiscoverController { $feed = $api->getDiscover(); if ($feed->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('discover'), new DiscoverTemplate($feed)); + $latte->render(Misc::getView('discover'), new FeedTemplate('Discover', $feed)); } else { ErrorHandler::show($feed->meta); } diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php index 077b628..5ed8c26 100644 --- a/app/Controllers/TagController.php +++ b/app/Controllers/TagController.php @@ -10,7 +10,7 @@ class TagController { static public function get(string $name) { $cursor = Misc::getCursor(); $api = Misc::api(); - $feed = $api->getHashtagFeed($name, $cursor); + $feed = $api->getChallengeFeed($name, $cursor); if ($feed->meta->success) { $latte = Misc::latte(); $latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); @@ -21,9 +21,9 @@ class TagController { static public function rss(string $name) { $api = Misc::api(); - $feed = $api->getHashtagFeed($name); + $feed = $api->getChallengeFeed($name); if ($feed->meta->success) { - $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->desc, $feed->items); + $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->challenge->desc, $feed->items); // Setup headers RSS::setHeaders('tag.rss'); echo $feed; diff --git a/app/Controllers/TrendingController.php b/app/Controllers/TrendingController.php index f9c77cc..b474d16 100644 --- a/app/Controllers/TrendingController.php +++ b/app/Controllers/TrendingController.php @@ -28,7 +28,7 @@ class TrendingController { static public function rss() { $api = Misc::api(); - $feed = $api->getTrending(); + $feed = $api->getTrendingFeed(); if ($feed->meta->success) { $feed = RSS::build('/trending', 'Trending', 'Tiktok trending', $feed->items); // Setup headers diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 3912f7d..47c106f 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -18,7 +18,7 @@ class UserController { exit; } $latte = Misc::latte(); - $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->nickname, $feed)); + $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed)); } else { ErrorHandler::show($feed->meta); } @@ -28,7 +28,7 @@ class UserController { $api = Misc::api(); $feed = $api->getUserFeed($username); if ($feed->meta->success) { - $feed = RSS::build('/@'.$username, $feed->info->detail->nickname, $feed->info->detail->signature, $feed->items); + $feed = RSS::build('/@'.$username, $feed->info->detail->user->nickname, $feed->info->detail->user->signature, $feed->items); // Setup headers RSS::setHeaders('user.rss'); echo $feed; diff --git a/app/Controllers/VideoController.php b/app/Controllers/VideoController.php index 3e1285a..ae1a6a6 100644 --- a/app/Controllers/VideoController.php +++ b/app/Controllers/VideoController.php @@ -3,7 +3,7 @@ namespace App\Controllers; use App\Helpers\ErrorHandler; use App\Helpers\Misc; -use App\Models\FeedTemplate; +use App\Models\ItemTemplate; class VideoController { static public function get(string $video_id) { @@ -11,7 +11,7 @@ class VideoController { $item = $api->getVideoByID($video_id); if ($item->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('video'), new FeedTemplate($item->info->detail->nickname, $item)); + $latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item)); } else { ErrorHandler::show($item->meta); } diff --git a/app/Helpers/Misc.php b/app/Helpers/Misc.php index f4c9871..22ee30b 100644 --- a/app/Helpers/Misc.php +++ b/app/Helpers/Misc.php @@ -9,10 +9,6 @@ class Misc { return isset($_GET['cursor']) && is_numeric($_GET['cursor']) ? (int) $_GET['cursor'] : 0; } - static public function getTtwid(): string { - return $_GET['cursor'] ?? ''; - } - static public function url(string $endpoint = '') { return self::env('APP_URL', '') . $endpoint; } @@ -70,7 +66,7 @@ class Misc { } else { $host = $_ENV['REDIS_HOST']; $port = (int) $_ENV['REDIS_PORT']; - $password = $_ENV['REDIS_PASSWORD'] ?? null; + $password = isset($_ENV['REDIS_PASSWORD']) ? $_ENV['REDIS_PASSWORD'] : null; } $cacheEngine = new RedisCache($host, $port, $password); break; diff --git a/app/Helpers/RSS.php b/app/Helpers/RSS.php index ee6495c..510aa2d 100644 --- a/app/Helpers/RSS.php +++ b/app/Helpers/RSS.php @@ -2,7 +2,7 @@ namespace App\Helpers; use \FeedWriter\RSS2; -use TikScraper\Download; +use \Sovit\TikTok\Download; class RSS { static public function build(string $endpoint, string $title, string $description, array $items): string { diff --git a/app/Models/DiscoverTemplate.php b/app/Models/DiscoverTemplate.php deleted file mode 100644 index a240830..0000000 --- a/app/Models/DiscoverTemplate.php +++ /dev/null @@ -1,16 +0,0 @@ -feed = $feed; - } -} diff --git a/app/Models/FeedTemplate.php b/app/Models/FeedTemplate.php index a26e490..836fc29 100644 --- a/app/Models/FeedTemplate.php +++ b/app/Models/FeedTemplate.php @@ -1,15 +1,13 @@ feed = $feed; } diff --git a/components/feed.latte b/components/feed.latte index eab4aa0..241141c 100644 --- a/components/feed.latte +++ b/components/feed.latte @@ -17,7 +17,7 @@ {/foreach} -
+
{if isset($_GET['cursor']) && $_GET['cursor'] != 0 } First diff --git a/composer.json b/composer.json index 5fc5fc6..8f61935 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,16 @@ "homepage": "https://github.com/pablouser1" } ], + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/pablouser1/TikTok-API-PHP" + } + ], "require": { "ext-redis": "^5.3.2", "ext-mbstring": "*", - "pablouser1/tikscraper": "^1.0", + "ssovit/tiktok-api": "dev-rework", "latte/latte": "^2.10", "vlucas/phpdotenv": "^5.4", "bramus/router": "^1.6", diff --git a/composer.lock b/composer.lock index dfd97d6..34b1a7e 100644 --- a/composer.lock +++ b/composer.lock @@ -414,6 +414,58 @@ ], "time": "2021-12-04T23:24:31+00:00" }, + { + "name": "ssovit/tiktok-api", + "version": "dev-rework", + "source": { + "type": "git", + "url": "https://github.com/pablouser1/TikTok-API-PHP.git", + "reference": "60c00cfd321888b80eba93b6214c8c4003e98e57" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pablouser1/TikTok-API-PHP/zipball/60c00cfd321888b80eba93b6214c8c4003e98e57", + "reference": "60c00cfd321888b80eba93b6214c8c4003e98e57", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Sovit\\": "lib" + } + }, + "archive": { + "exclude": [ + "/example", + "/.github", + "/.editorconfig" + ] + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Sovit Tamrakar", + "email": "sovit.tamrakar@gmail.com", + "homepage": "https://github.com/ssovit/", + "role": "Developer" + } + ], + "description": "Unofficial TikTok API for PHP", + "homepage": "https://github.com/ssovit/TikTok-API-PHP", + "keywords": [ + "tiktok", + "tiktok-api", + "tiktok-scraper" + ], + "support": { + "source": "https://github.com/ssovit/TikTok-API-PHP", + "issues": "https://github.com/ssovit/TikTok-API-PHP/issues", + "email": "sovit.tamrakar@gmail.com" + }, + "time": "2022-02-07T22:43:53+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.25.0", @@ -746,7 +798,9 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "ssovit/tiktok-api": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/views/about.latte b/views/about.latte index d81f9dd..6516ff0 100644 --- a/views/about.latte +++ b/views/about.latte @@ -24,7 +24,7 @@

This project wouldn't be possible without the help of the following projects: