From a3ebb08d26b53263c1d7a16e7f399324f4ab92c6 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro Date: Sun, 13 Feb 2022 22:21:08 +0100 Subject: [PATCH] Using new wrapper --- .env.example | 4 +- README.md | 2 +- app/Cache/JSONCache.php | 44 +++++------ app/Cache/RedisCache.php | 4 + app/Controllers/DiscoverController.php | 4 +- app/Controllers/ProxyController.php | 7 +- app/Controllers/TagController.php | 6 +- app/Controllers/TrendingController.php | 5 +- app/Controllers/UserController.php | 6 +- app/Controllers/VideoController.php | 4 +- app/Helpers/Misc.php | 10 ++- app/Models/DiscoverTemplate.php | 16 ++++ app/Models/FeedTemplate.php | 6 +- app/Models/ItemTemplate.php | 14 ---- components/feed.latte | 2 +- composer.json | 10 +-- composer.lock | 105 +++++++++++-------------- views/about.latte | 2 +- views/music.latte | 6 +- views/tag.latte | 8 +- views/user.latte | 12 +-- views/video.latte | 18 ++--- 22 files changed, 141 insertions(+), 154 deletions(-) create mode 100644 app/Models/DiscoverTemplate.php delete mode 100644 app/Models/ItemTemplate.php diff --git a/.env.example b/.env.example index 60a168d..f3bdf21 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,9 @@ # APP_URL="http://localhost:8000" # Full url path, PLEASE REPLACE TO YOUR OWN ONE - +# SIGNER_URL="https://tiktok-sign.herokuapp.com/signature" # External signing service # 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 (CHOOSE ONE) +# Redis cache, used on Helpers\CacheEngines\RedisCache # 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 5bc31e9..3cacf49 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ location /proxitok/.env { * i18n ## Credits -* [TikTok-API-PHP](https://github.com/ssovit/TikTok-API-PHP) (Currently using my personal fork) +* [TikScraper](https://github.com/pablouser1/TikScraperPHP) * [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 7b16c60..f113433 100644 --- a/app/Cache/JSONCache.php +++ b/app/Cache/JSONCache.php @@ -1,33 +1,31 @@ cache_path = $_ENV['API_CACHE_JSON']; - } - } - 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); - if ($time < $element->expires) { - return $element->data; - } - // Remove file if expired - unlink($filename); - } - return false; + $this->cache_path = Misc::env('API_CACHE_JSON', __DIR__ . '/../../cache/api'); } - 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 - ])); + public function get(string $cache_key): ?object { + $filename = $this->cache_path . '/' . $cache_key . '.json'; + if (is_file($filename)) { + $json_string = file_get_contents($filename); + $element = json_decode($json_string); + return $element; + } + return null; + } + + 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); } } diff --git a/app/Cache/RedisCache.php b/app/Cache/RedisCache.php index 55a6dca..4b7ee33 100644 --- a/app/Cache/RedisCache.php +++ b/app/Cache/RedisCache.php @@ -27,6 +27,10 @@ class RedisCache { return null; } + public function exists(string $cache_key): bool { + return $this->client->exists($cache_key); + } + 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 ea564e2..dbd6e70 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\FeedTemplate; +use App\Models\DiscoverTemplate; 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 FeedTemplate('Discover', $feed)); + $latte->render(Misc::getView('discover'), new DiscoverTemplate($feed)); } else { ErrorHandler::show($feed->meta); } diff --git a/app/Controllers/ProxyController.php b/app/Controllers/ProxyController.php index cf0ec77..9b6d417 100644 --- a/app/Controllers/ProxyController.php +++ b/app/Controllers/ProxyController.php @@ -40,25 +40,24 @@ class ProxyController { static public function stream() { if (isset($_GET['download'])) { + $downloader = new \TikScraper\Download(); $watermark = isset($_GET['watermark']); if ($watermark) { self::checkUrl(); $filename = self::getFileName(); - $downloader = new \Sovit\TikTok\Download(); $downloader->url($_GET['url'], $filename, true); } else { if (!isset($_GET['id'])) { die('You need to send an ID!'); } $filename = self::getFileName(); - $downloader = new \Sovit\TikTok\Download(); $downloader->url($_GET['id'], $filename, false); } } else { self::checkUrl(); $url = $_GET['url']; - $streamer = new \Sovit\TikTok\Stream(); - $streamer->stream($url); + $streamer = new \TikScraper\Stream(); + $streamer->url($url); } } } diff --git a/app/Controllers/TagController.php b/app/Controllers/TagController.php index 5ed8c26..077b628 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->getChallengeFeed($name, $cursor); + $feed = $api->getHashtagFeed($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->getChallengeFeed($name); + $feed = $api->getHashtagFeed($name); if ($feed->meta->success) { - $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->challenge->desc, $feed->items); + $feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->desc, $feed->items); // Setup headers RSS::setHeaders('tag.rss'); echo $feed; diff --git a/app/Controllers/TrendingController.php b/app/Controllers/TrendingController.php index 2d1cffe..3c08594 100644 --- a/app/Controllers/TrendingController.php +++ b/app/Controllers/TrendingController.php @@ -9,8 +9,9 @@ use App\Helpers\RSS; class TrendingController { static public function get() { $cursor = Misc::getCursor(); + $page = $_GET['page'] ?? 0; $api = Misc::api(); - $feed = $api->getTrendingFeed($cursor); + $feed = $api->getTrending($cursor, $page); if ($feed->meta->success) { $latte = Misc::latte(); $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); @@ -21,7 +22,7 @@ class TrendingController { static public function rss() { $api = Misc::api(); - $feed = $api->getTrendingFeed(); + $feed = $api->getTrending(); 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 91398a9..343eb2f 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -12,12 +12,12 @@ class UserController { $api = Misc::api(); $feed = $api->getUserFeed($username, $cursor); if ($feed->meta->success) { - if ($feed->info->detail->user->privateAccount) { + if ($feed->info->detail->privateAccount) { http_response_code(400); echo 'Private account detected! Not supported'; } $latte = Misc::latte(); - $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed)); + $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->nickname, $feed)); } else { ErrorHandler::show($feed->meta); } @@ -27,7 +27,7 @@ class UserController { $api = Misc::api(); $feed = $api->getUserFeed($username); if ($feed->meta->success) { - $feed = RSS::build('/@'.$username, $feed->info->detail->user->nickname, $feed->info->detail->user->signature, $feed->items); + $feed = RSS::build('/@'.$username, $feed->info->detail->nickname, $feed->info->detail->signature, $feed->items); // Setup headers RSS::setHeaders('user.rss'); echo $feed; diff --git a/app/Controllers/VideoController.php b/app/Controllers/VideoController.php index ae1a6a6..3e1285a 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\ItemTemplate; +use App\Models\FeedTemplate; 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 ItemTemplate($item->info->detail->user->nickname, $item)); + $latte->render(Misc::getView('video'), new FeedTemplate($item->info->detail->nickname, $item)); } else { ErrorHandler::show($item->meta); } diff --git a/app/Helpers/Misc.php b/app/Helpers/Misc.php index 4bceddf..6a4ea36 100644 --- a/app/Helpers/Misc.php +++ b/app/Helpers/Misc.php @@ -27,8 +27,10 @@ class Misc { /** * Setup of TikTok Api wrapper */ - static public function api(): \Sovit\TikTok\Api { - $options = []; + static public function api(): \TikScraper\Api { + $options = [ + 'remote_signer' => self::env('SIGNER_URL', 'http://localhost:8080/signature') + ]; $cacheEngine = false; // Proxy config foreach(Cookies::PROXY as $proxy_element) { @@ -55,13 +57,13 @@ class Misc { } else { $host = $_ENV['REDIS_HOST']; $port = (int) $_ENV['REDIS_PORT']; - $password = isset($_ENV['REDIS_PASSWORD']) ? $_ENV['REDIS_PASSWORD'] : null; + $password = $_ENV['REDIS_PASSWORD'] ?? null; } $cacheEngine = new RedisCache($host, $port, $password); break; } } - $api = new \Sovit\TikTok\Api($options, $cacheEngine); + $api = new \TikScraper\Api($options, $cacheEngine); return $api; } diff --git a/app/Models/DiscoverTemplate.php b/app/Models/DiscoverTemplate.php new file mode 100644 index 0000000..a240830 --- /dev/null +++ b/app/Models/DiscoverTemplate.php @@ -0,0 +1,16 @@ +feed = $feed; + } +} diff --git a/app/Models/FeedTemplate.php b/app/Models/FeedTemplate.php index 836fc29..a26e490 100644 --- a/app/Models/FeedTemplate.php +++ b/app/Models/FeedTemplate.php @@ -1,13 +1,15 @@ feed = $feed; } diff --git a/app/Models/ItemTemplate.php b/app/Models/ItemTemplate.php deleted file mode 100644 index b4b749d..0000000 --- a/app/Models/ItemTemplate.php +++ /dev/null @@ -1,14 +0,0 @@ -item = $item; - } -} diff --git a/components/feed.latte b/components/feed.latte index d78e4da..60e29c2 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 01de09d..5a05bb4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "pablouser1/proxitok", "description": "An alternative frontend for TikTok", - "version": "1.4.1.4", + "version": "1.5.0", "license": "AGPL-3.0-or-later", "type": "project", "homepage": "https://github.com/pablouser1/ProxiTok", @@ -11,16 +11,10 @@ "homepage": "https://github.com/pablouser1" } ], - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/pablouser1/TikTok-API-PHP" - } - ], "require": { "ext-redis": "^5.3.2", "ext-mbstring": "*", - "ssovit/tiktok-api": "dev-rework", + "pablouser1/tikscraper": "^1.0", "latte/latte": "^2.10", "vlucas/phpdotenv": "^5.4", "bramus/router": "^1.6", diff --git a/composer.lock b/composer.lock index 7221a56..8653490 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": "e829a003ddc8a31048406dfd3ec20815", + "content-hash": "f5e451a254a58bf7d512421237641801", "packages": [ { "name": "bramus/router", @@ -304,6 +304,45 @@ }, "time": "2016-11-19T20:47:44+00:00" }, + { + "name": "pablouser1/tikscraper", + "version": "v1.2.1", + "source": { + "type": "git", + "url": "https://github.com/pablouser1/TikScraperPHP.git", + "reference": "2022b2c2d661383d09c4727091df7fae345844a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/2022b2c2d661383d09c4727091df7fae345844a5", + "reference": "2022b2c2d661383d09c4727091df7fae345844a5", + "shasum": "" + }, + "require": { + "php": ">=7.3|^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "TikScraper\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Pablo Ferreiro" + } + ], + "description": "Get data from TikTok API", + "support": { + "issues": "https://github.com/pablouser1/TikScraperPHP/issues", + "source": "https://github.com/pablouser1/TikScraperPHP/tree/v1.2.1" + }, + "time": "2022-02-13T20:28:38+00:00" + }, { "name": "phpoption/phpoption", "version": "1.8.1", @@ -375,58 +414,6 @@ ], "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.24.0", @@ -543,12 +530,12 @@ } }, "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, "files": [ "bootstrap.php" - ] + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -759,9 +746,7 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "ssovit/tiktok-api": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/views/about.latte b/views/about.latte index 0eca965..46e3a56 100644 --- a/views/about.latte +++ b/views/about.latte @@ -20,7 +20,7 @@

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

    -
  • TikTok-API-PHP
  • +
  • TikScraper
  • Latte
  • bramus/router
  • PHP dotenv
  • diff --git a/views/music.latte b/views/music.latte index 99cea03..04b6806 100644 --- a/views/music.latte +++ b/views/music.latte @@ -1,9 +1,9 @@ {layout '../layouts/default.latte'} {block header} -

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

    -

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

    -

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

    +

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

    +

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

    +

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

    {/block} {block content} diff --git a/views/tag.latte b/views/tag.latte index 9313214..fc39f04 100644 --- a/views/tag.latte +++ b/views/tag.latte @@ -1,10 +1,10 @@ {layout '../layouts/default.latte'} {block header} -

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

    -

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

    -

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

    - RSS +

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

    +

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

    +

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

    + RSS {/block} {block content} diff --git a/views/user.latte b/views/user.latte index bb47b5b..7e2b56c 100644 --- a/views/user.latte +++ b/views/user.latte @@ -2,13 +2,13 @@ {block header}
    - +
    -

    {$feed->info->detail->user->uniqueId}'s profile

    -

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

    -

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

    -

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

    - RSS +

    {$feed->info->detail->uniqueId}'s profile

    +

    {$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}

    + RSS {/block} {block content} diff --git a/views/video.latte b/views/video.latte index f9cd64a..f1fb7cb 100644 --- a/views/video.latte +++ b/views/video.latte @@ -3,20 +3,20 @@ {block content}
    -
    -

    Video by {$item->info->detail->user->uniqueId}

    -

    {$item->items[0]->desc}

    -

    Played {number($item->info->detail->stats->playCount)} times

    -

    Shared {number($item->info->detail->stats->shareCount)} times / {number($item->info->detail->stats->commentCount)} comments

    +

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

    +

    {$feed->items[0]->desc}

    +

    Played {number($feed->info->stats->playCount)} times

    +

    Shared {number($feed->info->stats->shareCount)} times / {number($feed->info->stats->commentCount)} comments


    - Download video -

    {$item->items[0]->music->title}

    - + Download video +

    {$feed->items[0]->music->title}

    +