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 {
static public function get() {
$api = Wrappers::api();
$feed = $api->getDiscover();
$feed = $api->discover();
if ($feed->meta->success) {
$latte = Wrappers::latte();
$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\Misc;
use App\Helpers\Wrappers;
use App\Models\FeedTemplate;
use App\Models\FullTemplate;
class MusicController {
static public function get(string $music_id) {
$cursor = Misc::getCursor();
$api = Wrappers::api();
$feed = $api->getMusicFeed($music_id, $cursor);
if ($feed->meta->success) {
$music = $api->music($music_id);
$music->feed($cursor);
if ($music->ok()) {
$data = $music->getFull();
$latte = Wrappers::latte();
$latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed));
$latte->render(Misc::getView('music'), new FullTemplate('Music', $data));
} 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\Misc;
use App\Helpers\Wrappers;
use App\Models\FeedTemplate;
use App\Models\FullTemplate;
use App\Models\RSSTemplate;
class TagController {
static public function get(string $name) {
$cursor = Misc::getCursor();
$api = Wrappers::api();
$feed = $api->getHashtagFeed($name, $cursor);
if ($feed->meta->success) {
$hashtag = $api->hashtag($name);
$hashtag->feed($cursor);
if ($hashtag->ok()) {
$data = $hashtag->getFull();
$latte = Wrappers::latte();
$latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed));
$latte->render(Misc::getView('tag'), new FullTemplate('Tag', $data));
} else {
ErrorHandler::show($feed->meta);
ErrorHandler::show($hashtag->error());
}
}
static public function rss(string $name) {
$api = Wrappers::api();
$feed = $api->getHashtagFeed($name);
if ($feed->meta->success) {
$hashtag = $api->hashtag($name);
$hashtag->feed();
if ($hashtag->ok()) {
$data = $hashtag->getFull();
$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();
// Ttwid if standard, cursor if legacy
if ($api::MODE === 'STANDARD') {
$cursor = Misc::getTtwid();
} else {
if ($api->isLegacy()) {
$cursor = Misc::getCursor();
} else {
$cursor = Misc::getTtwid();
}
$feed = $api->getTrending($cursor);
if ($feed->meta->success) {
$trending = $api->trending();
$trending->feed($cursor);
$feed = $trending->getFeed();
if ($feed && $feed->meta->success) {
$latte = Wrappers::latte();
$latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed));
} else {
ErrorHandler::show($feed->meta);
ErrorHandler::show($trending->error());
}
}
static public function rss() {
$api = Wrappers::api();
$feed = $api->getTrending();
if ($feed->meta->success) {
$trending = $api->trending();
$trending->feed();
$feed = $trending->getFeed();
if ($feed && $feed->meta->success) {
$latte = Wrappers::latte();
$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\Misc;
use App\Helpers\Wrappers;
use App\Models\FeedTemplate;
use App\Models\FullTemplate;
use App\Models\RSSTemplate;
use App\Models\VideoTemplate;
@ -12,37 +12,43 @@ class UserController {
static public function get(string $username) {
$cursor = Misc::getCursor();
$api = Wrappers::api();
$feed = $api->getUserFeed($username, $cursor);
if ($feed->meta->success) {
if ($feed->info->detail->privateAccount) {
$user = $api->user($username);
$user->feed($cursor);
if ($user->ok()) {
$data = $user->getFull();
if ($data->info->detail->privateAccount) {
http_response_code(403);
echo 'Private account detected! Not supported';
exit;
}
$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 {
ErrorHandler::show($feed->meta);
ErrorHandler::show($user->error());
}
}
static public function video(string $username, string $video_id) {
$api = Wrappers::api();
$feed = $api->getVideoByID($video_id);
if ($feed->meta->success) {
$video = $api->video($video_id);
$video->feed();
if ($video->ok()) {
$data = $video->getFull();
$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 {
ErrorHandler::show($feed->meta);
ErrorHandler::show($video->error());
}
}
static public function rss(string $username) {
$api = Wrappers::api();
$feed = $api->getUserFeed($username);
if ($feed->meta->success) {
$user = $api->user($username);
$user->feed();
if ($user->ok()) {
$data = $user->getFull();
$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;
use App\Models\ErrorTemplate;
use TikScraper\Models\Meta;
class ErrorHandler {
static public function show(object $meta) {
static public function show(Meta $meta) {
http_response_code($meta->http_code);
$latte = Wrappers::latte();
$latte->render(Misc::getView('error'), new ErrorTemplate($meta));

View file

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

View file

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

View file

@ -5,10 +5,12 @@ namespace App\Models;
* Base for templates with a feed
*/
class FeedTemplate extends BaseTemplate {
public object $feed;
public object $data;
function __construct(string $title, object $feed) {
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>
<section class="section">
<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}
<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))}"
@ -16,7 +16,7 @@
<img class="hidden" loading="lazy" data-src="{path('/stream?url=' . urlencode($item->video->dynamicCover))}" />
</div>
{/foreach}
{if empty($feed->items)}
{if empty($data->feed->items)}
<p class="title">No items sent by TikTok!</p>
{/if}
</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 *}
{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={$feed->minCursor}">Back</a>
<a class="button is-danger" href="?cursor={$data->feed->minCursor}">Back</a>
{/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>

View file

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

View file

@ -1,7 +1,7 @@
{
"name": "pablouser1/proxitok",
"description": "An alternative frontend for TikTok",
"version": "2.2.2.0",
"version": "2.3.0.0",
"license": "AGPL-3.0-or-later",
"type": "project",
"authors": [
@ -24,7 +24,7 @@
"latte/latte": "^2.11",
"bramus/router": "^1.6",
"josegonzalez/dotenv": "dev-master",
"pablouser1/tikscraper": "^1.3"
"pablouser1/tikscraper": "^2.0"
},
"autoload": {
"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",
"This file is @generated automatically"
],
"content-hash": "f1c0945357108862633ea87a1f6bbdd8",
"content-hash": "e0e24f4588d6e34423fa8931ae8ae9a3",
"packages": [
{
"name": "bramus/router",
@ -263,16 +263,16 @@
},
{
"name": "pablouser1/tikscraper",
"version": "v1.3.5.2",
"version": "v2.0.1.3",
"source": {
"type": "git",
"url": "https://github.com/pablouser1/TikScraperPHP.git",
"reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf"
"reference": "45812af395b376dff6835af192827f34ba7a4cf9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/cb2580e9868cd4f77f3ab93987786fbd57e4efaf",
"reference": "cb2580e9868cd4f77f3ab93987786fbd57e4efaf",
"url": "https://api.github.com/repos/pablouser1/TikScraperPHP/zipball/45812af395b376dff6835af192827f34ba7a4cf9",
"reference": "45812af395b376dff6835af192827f34ba7a4cf9",
"shasum": ""
},
"require": {
@ -298,9 +298,9 @@
"description": "Get data from TikTok API",
"support": {
"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",

View file

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

View file

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

View file

@ -1,14 +1,14 @@
{layout '../layouts/default.latte'}
{block header}
{if $feed->info->detail->profileThumb !== ''}
{if $data->info->detail->profileThumb !== ''}
<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>
{/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>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 content}

View file

@ -2,13 +2,13 @@
{block header}
<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>
<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>{$feed->info->detail->signature}</p>
<p>Following: {number($feed->info->stats->followingCount)} / Followers: {number($feed->info->stats->followerCount)}</p>
<p>Hearts: {number($feed->info->stats->heartCount)} / Videos: {$feed->info->stats->videoCount}</p>
<p>{$data->info->detail->signature}</p>
<p>Following: {number($data->info->stats->followingCount)} / Followers: {number($data->info->stats->followerCount)}</p>
<p>Hearts: {number($data->info->stats->heartCount)} / Videos: {$data->info->stats->videoCount}</p>
{/block}
{block content}