Updated wrapper

This commit is contained in:
Pablo Ferreiro 2022-05-24 14:10:41 +02:00
parent 14a8829087
commit 55e1e9057b
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
19 changed files with 110 additions and 73 deletions

View file

@ -9,7 +9,7 @@ use App\Models\FeedTemplate;
class DiscoverController { class DiscoverController {
static public function get() { static public function get() {
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getDiscover(); $feed = $api->discover();
if ($feed->meta->success) { if ($feed->meta->success) {
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('discover'), new FeedTemplate('Discover', $feed)); $latte->render(Misc::getView('discover'), new FeedTemplate('Discover', $feed));

View file

@ -4,19 +4,21 @@ namespace App\Controllers;
use App\Helpers\ErrorHandler; use App\Helpers\ErrorHandler;
use App\Helpers\Misc; use App\Helpers\Misc;
use App\Helpers\Wrappers; use App\Helpers\Wrappers;
use App\Models\FeedTemplate; use App\Models\FullTemplate;
class MusicController { class MusicController {
static public function get(string $music_id) { static public function get(string $music_id) {
$cursor = Misc::getCursor(); $cursor = Misc::getCursor();
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getMusicFeed($music_id, $cursor); $music = $api->music($music_id);
if ($feed->meta->success) { $music->feed($cursor);
if ($music->ok()) {
$data = $music->getFull();
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed)); $latte->render(Misc::getView('music'), new FullTemplate('Music', $data));
} else { } else {
ErrorHandler::show($feed->meta); ErrorHandler::show($music->error());
} }
} }
} }

View file

@ -4,28 +4,32 @@ namespace App\Controllers;
use App\Helpers\ErrorHandler; use App\Helpers\ErrorHandler;
use App\Helpers\Misc; use App\Helpers\Misc;
use App\Helpers\Wrappers; use App\Helpers\Wrappers;
use App\Models\FeedTemplate; use App\Models\FullTemplate;
use App\Models\RSSTemplate; use App\Models\RSSTemplate;
class TagController { class TagController {
static public function get(string $name) { static public function get(string $name) {
$cursor = Misc::getCursor(); $cursor = Misc::getCursor();
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getHashtagFeed($name, $cursor); $hashtag = $api->hashtag($name);
if ($feed->meta->success) { $hashtag->feed($cursor);
if ($hashtag->ok()) {
$data = $hashtag->getFull();
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); $latte->render(Misc::getView('tag'), new FullTemplate('Tag', $data));
} else { } else {
ErrorHandler::show($feed->meta); ErrorHandler::show($hashtag->error());
} }
} }
static public function rss(string $name) { static public function rss(string $name) {
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getHashtagFeed($name); $hashtag = $api->hashtag($name);
if ($feed->meta->success) { $hashtag->feed();
if ($hashtag->ok()) {
$data = $hashtag->getFull();
$latte = Wrappers::latte(); $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));
} }
} }
} }

View file

@ -12,24 +12,30 @@ class TrendingController {
$api = Wrappers::api(); $api = Wrappers::api();
// Ttwid if standard, cursor if legacy // Ttwid if standard, cursor if legacy
if ($api::MODE === 'STANDARD') { if ($api->isLegacy()) {
$cursor = Misc::getTtwid();
} else {
$cursor = Misc::getCursor(); $cursor = Misc::getCursor();
} else {
$cursor = Misc::getTtwid();
} }
$feed = $api->getTrending($cursor); $trending = $api->trending();
if ($feed->meta->success) { $trending->feed($cursor);
$feed = $trending->getFeed();
if ($feed && $feed->meta->success) {
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed));
} else { } else {
ErrorHandler::show($feed->meta); ErrorHandler::show($trending->error());
} }
} }
static public function rss() { static public function rss() {
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getTrending(); $trending = $api->trending();
if ($feed->meta->success) { $trending->feed();
$feed = $trending->getFeed();
if ($feed && $feed->meta->success) {
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('rss'), new RSSTemplate('Trending', 'Trending on TikTok', '/trending', $feed->items)); $latte->render(Misc::getView('rss'), new RSSTemplate('Trending', 'Trending on TikTok', '/trending', $feed->items));
} }

View file

@ -4,7 +4,7 @@ namespace App\Controllers;
use App\Helpers\ErrorHandler; use App\Helpers\ErrorHandler;
use App\Helpers\Misc; use App\Helpers\Misc;
use App\Helpers\Wrappers; use App\Helpers\Wrappers;
use App\Models\FeedTemplate; use App\Models\FullTemplate;
use App\Models\RSSTemplate; use App\Models\RSSTemplate;
use App\Models\VideoTemplate; use App\Models\VideoTemplate;
@ -12,37 +12,43 @@ class UserController {
static public function get(string $username) { static public function get(string $username) {
$cursor = Misc::getCursor(); $cursor = Misc::getCursor();
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getUserFeed($username, $cursor); $user = $api->user($username);
if ($feed->meta->success) { $user->feed($cursor);
if ($feed->info->detail->privateAccount) { if ($user->ok()) {
$data = $user->getFull();
if ($data->info->detail->privateAccount) {
http_response_code(403); http_response_code(403);
echo 'Private account detected! Not supported'; echo 'Private account detected! Not supported';
exit; exit;
} }
$latte = Wrappers::latte(); $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 { } else {
ErrorHandler::show($feed->meta); ErrorHandler::show($user->error());
} }
} }
static public function video(string $username, string $video_id) { static public function video(string $username, string $video_id) {
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getVideoByID($video_id); $video = $api->video($video_id);
if ($feed->meta->success) { $video->feed();
if ($video->ok()) {
$data = $video->getFull();
$latte = Wrappers::latte(); $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 { } else {
ErrorHandler::show($feed->meta); ErrorHandler::show($video->error());
} }
} }
static public function rss(string $username) { static public function rss(string $username) {
$api = Wrappers::api(); $api = Wrappers::api();
$feed = $api->getUserFeed($username); $user = $api->user($username);
if ($feed->meta->success) { $user->feed();
if ($user->ok()) {
$data = $user->getFull();
$latte = Wrappers::latte(); $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));
} }
} }
} }

View file

@ -2,9 +2,10 @@
namespace App\Helpers; namespace App\Helpers;
use App\Models\ErrorTemplate; use App\Models\ErrorTemplate;
use TikScraper\Models\Meta;
class ErrorHandler { class ErrorHandler {
static public function show(object $meta) { static public function show(Meta $meta) {
http_response_code($meta->http_code); http_response_code($meta->http_code);
$latte = Wrappers::latte(); $latte = Wrappers::latte();
$latte->render(Misc::getView('error'), new ErrorTemplate($meta)); $latte->render(Misc::getView('error'), new ErrorTemplate($meta));

View file

@ -42,11 +42,10 @@ class Wrappers {
return $latte; return $latte;
} }
/** /**
* Setup of TikTok Api wrapper * Setup of TikTok Api wrapper
* @return \TikScraper\Api|\TikScraper\Legacy
*/ */
static public function api() { static public function api(): \TikScraper\Api {
$options = [ $options = [
'use_test_endpoints' => Misc::env('API_TEST_ENDPOINTS', false), 'use_test_endpoints' => Misc::env('API_TEST_ENDPOINTS', false),
'signer' => [ 'signer' => [
@ -84,7 +83,6 @@ class Wrappers {
// Legacy mode // Legacy mode
$legacy = Misc::env('API_FORCE_LEGACY', false) || isset($_COOKIE['api-legacy']) && $_COOKIE['api-legacy'] === 'on'; $legacy = Misc::env('API_FORCE_LEGACY', false) || isset($_COOKIE['api-legacy']) && $_COOKIE['api-legacy'] === 'on';
return new \TikScraper\Api($options, $legacy, $cacheEngine);
return $legacy === false ? new \TikScraper\Api($options, $cacheEngine) : new \TikScraper\Legacy($options, $cacheEngine);
} }
} }

View file

@ -1,8 +1,10 @@
<?php <?php
namespace App\Models; namespace App\Models;
use TikScraper\Models\Meta;
class ErrorTemplate extends BaseTemplate { class ErrorTemplate extends BaseTemplate {
public object $error; public Meta $error;
function __construct(object $error) { function __construct(object $error) {
parent::__construct('Error'); parent::__construct('Error');

View file

@ -5,10 +5,12 @@ namespace App\Models;
* Base for templates with a feed * Base for templates with a feed
*/ */
class FeedTemplate extends BaseTemplate { class FeedTemplate extends BaseTemplate {
public object $feed; public object $data;
function __construct(string $title, object $feed) { function __construct(string $title, object $feed) {
parent::__construct($title); parent::__construct($title);
$this->feed = $feed; $this->data = (object) [
'feed' => $feed
];
} }
} }

View file

@ -0,0 +1,16 @@
<?php
namespace App\Models;
use TikScraper\Models\Full;
/**
* Base for templates with both info and feed
*/
class FullTemplate extends BaseTemplate {
public Full $data;
function __construct(string $title, Full $data) {
parent::__construct($title);
$this->data = $data;
}
}

View file

@ -2,7 +2,7 @@
<noscript>JavaScript is required for this section to work!</noscript> <noscript>JavaScript is required for this section to work!</noscript>
<section class="section"> <section class="section">
<div class="columns is-multiline is-vcentered"> <div class="columns is-multiline is-vcentered">
{foreach $feed->items as $item} {foreach $data->feed->items as $item}
{do $share_url = 'https://tiktok.com/@' . $item->author->uniqueId . '/video/' . $item->id} {do $share_url = 'https://tiktok.com/@' . $item->author->uniqueId . '/video/' . $item->id}
<div class="column is-one-quarter clickable-img" id="{$item->id}" onclick="openVideo(this.id)" <div class="column is-one-quarter clickable-img" id="{$item->id}" onclick="openVideo(this.id)"
data-video_url="{path('/stream?url=' . urlencode($item->video->playAddr))}" data-video_url="{path('/stream?url=' . urlencode($item->video->playAddr))}"
@ -16,7 +16,7 @@
<img class="hidden" loading="lazy" data-src="{path('/stream?url=' . urlencode($item->video->dynamicCover))}" /> <img class="hidden" loading="lazy" data-src="{path('/stream?url=' . urlencode($item->video->dynamicCover))}" />
</div> </div>
{/foreach} {/foreach}
{if empty($feed->items)} {if empty($data->feed->items)}
<p class="title">No items sent by TikTok!</p> <p class="title">No items sent by TikTok!</p>
{/if} {/if}
</div> </div>

View file

@ -1,8 +1,8 @@
<div n:ifset="$feed->info" class="buttons"> <div n:ifset="$data->info" class="buttons">
{* is_numeric is used to avoid having a back button with ttwid cursors *} {* is_numeric is used to avoid having a back button with ttwid cursors *}
{if isset($_GET['cursor']) && is_numeric($_GET['cursor']) && $_GET['cursor'] != 0 } {if isset($_GET['cursor']) && is_numeric($_GET['cursor']) && $_GET['cursor'] != 0 }
<a class="button is-danger" href="?cursor=0">First</a> <a class="button is-danger" href="?cursor=0">First</a>
<a class="button is-danger" href="?cursor={$feed->minCursor}">Back</a> <a class="button is-danger" href="?cursor={$data->feed->minCursor}">Back</a>
{/if} {/if}
<a n:attr="disabled => !$feed->hasMore" class="button is-success" href="?cursor={$feed->maxCursor}">Next</a> <a n:attr="disabled => !$data->feed->hasMore" class="button is-success" href="?cursor={$data->feed->maxCursor}">Next</a>
</div> </div>

View file

@ -1,5 +1,5 @@
<div class="container"> <div class="container">
{foreach $feed->items as $item} {foreach $data->feed->items as $item}
<article class="media"> <article class="media">
<figure class="media-left"> <figure class="media-left">
<p class="image is-64x64"> <p class="image is-64x64">
@ -29,7 +29,7 @@
</div> </div>
</article> </article>
{/foreach} {/foreach}
{if empty($feed->items)} {if empty($data->feed->items)}
<p class="title">No items sent by TikTok!</p> <p class="title">No items sent by TikTok!</p>
{/if} {/if}
</div> </div>

View file

@ -1,7 +1,7 @@
{ {
"name": "pablouser1/proxitok", "name": "pablouser1/proxitok",
"description": "An alternative frontend for TikTok", "description": "An alternative frontend for TikTok",
"version": "2.2.2.0", "version": "2.3.0.0",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"type": "project", "type": "project",
"authors": [ "authors": [
@ -24,7 +24,7 @@
"latte/latte": "^2.11", "latte/latte": "^2.11",
"bramus/router": "^1.6", "bramus/router": "^1.6",
"josegonzalez/dotenv": "dev-master", "josegonzalez/dotenv": "dev-master",
"pablouser1/tikscraper": "^1.3" "pablouser1/tikscraper": "^2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

14
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "f1c0945357108862633ea87a1f6bbdd8", "content-hash": "e0e24f4588d6e34423fa8931ae8ae9a3",
"packages": [ "packages": [
{ {
"name": "bramus/router", "name": "bramus/router",
@ -263,16 +263,16 @@
}, },
{ {
"name": "pablouser1/tikscraper", "name": "pablouser1/tikscraper",
"version": "v1.3.5.2", "version": "v2.0.1.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pablouser1/TikScraperPHP.git", "url": "https://github.com/pablouser1/TikScraperPHP.git",
"reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf" "reference": "45812af395b376dff6835af192827f34ba7a4cf9"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/cb2580e9868cd4f77f3ab93987786fbd57e4efaf", "url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/45812af395b376dff6835af192827f34ba7a4cf9",
"reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf", "reference": "45812af395b376dff6835af192827f34ba7a4cf9",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -298,9 +298,9 @@
"description": "Get data from TikTok API", "description": "Get data from TikTok API",
"support": { "support": {
"issues": "https://github.com/pablouser1/TikScraperPHP/issues", "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", "name": "php-webdriver/webdriver",

View file

@ -5,7 +5,7 @@
{/block} {/block}
{block content} {block content}
{foreach $feed->items as $type => $items} {foreach $data->feed->items as $type => $items}
<p class="title">{$type|firstUpper}</p> <p class="title">{$type|firstUpper}</p>
<div class="columns is-multiline is-vcentered"> <div class="columns is-multiline is-vcentered">
{foreach $items as $item} {foreach $items as $item}

View file

@ -1,9 +1,9 @@
{layout '../layouts/default.latte'} {layout '../layouts/default.latte'}
{block header} {block header}
<p class="title">{$feed->info->detail->title}</p> <p class="title">{$data->info->detail->title}</p>
<p class="subtitle">{$feed->info->detail->desc}</p> <p class="subtitle">{$data->info->detail->desc}</p>
<p>Videos: {number($feed->info->stats->videoCount)}</p> <p>Videos: {number($data->info->stats->videoCount)}</p>
{/block} {/block}
{block content} {block content}

View file

@ -1,14 +1,14 @@
{layout '../layouts/default.latte'} {layout '../layouts/default.latte'}
{block header} {block header}
{if $feed->info->detail->profileThumb !== ''} {if $data->info->detail->profileThumb !== ''}
<figure class="figure is-96x96"> <figure class="figure is-96x96">
<img src="{path('/stream?url=' . urlencode($feed->info->detail->profileThumb))}" /> <img src="{path('/stream?url=' . urlencode($data->info->detail->profileThumb))}" />
</figure> </figure>
{/if} {/if}
<p class="title">{$feed->info->detail->title}</p> <p class="title">{$data->info->detail->title}</p>
<p class="subtitle">{include '../components/rss.latte'}</p> <p class="subtitle">{include '../components/rss.latte'}</p>
<p>Videos: {number($feed->info->stats->videoCount)} / Views: {number($feed->info->stats->viewCount)}</p> <p>Videos: {number($data->info->stats->videoCount)} / Views: {number($data->info->stats->viewCount)}</p>
{/block} {/block}
{block content} {block content}

View file

@ -2,13 +2,13 @@
{block header} {block header}
<figure class="figure is-96x96"> <figure class="figure is-96x96">
<img src="{path('/stream?url=' . urlencode($feed->info->detail->avatarThumb))}" /> <img src="{path('/stream?url=' . urlencode($data->info->detail->avatarThumb))}" />
</figure> </figure>
<p class="title">{$feed->info->detail->uniqueId}</p> <p class="title">{$data->info->detail->uniqueId}</p>
<p class="subtitle">{include '../components/rss.latte'}</p> <p class="subtitle">{include '../components/rss.latte'}</p>
<p>{$feed->info->detail->signature}</p> <p>{$data->info->detail->signature}</p>
<p>Following: {number($feed->info->stats->followingCount)} / Followers: {number($feed->info->stats->followerCount)}</p> <p>Following: {number($data->info->stats->followingCount)} / Followers: {number($data->info->stats->followerCount)}</p>
<p>Hearts: {number($feed->info->stats->heartCount)} / Videos: {$feed->info->stats->videoCount}</p> <p>Hearts: {number($data->info->stats->heartCount)} / Videos: {$data->info->stats->videoCount}</p>
{/block} {/block}
{block content} {block content}