Merged with new-api
This commit is contained in:
commit
a4a4b0497c
21 changed files with 145 additions and 78 deletions
|
|
@ -1,31 +1,33 @@
|
|||
<?php
|
||||
namespace App\Cache;
|
||||
|
||||
use App\Helpers\Misc;
|
||||
|
||||
class JSONCache {
|
||||
private string $cache_path = '';
|
||||
private string $cache_path = __DIR__ . '/../../cache/api';
|
||||
|
||||
function __construct() {
|
||||
$this->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
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use TikScraper\Models\Discover;
|
||||
|
||||
/**
|
||||
* Base for templates with a feed
|
||||
*/
|
||||
class DiscoverTemplate extends BaseTemplate {
|
||||
public Discover $feed;
|
||||
|
||||
function __construct(Discover $feed) {
|
||||
parent::__construct('Discover');
|
||||
$this->feed = $feed;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,13 @@
|
|||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use TikScraper\Models\Feed;
|
||||
|
||||
/**
|
||||
* Base for templates with a feed
|
||||
*/
|
||||
class FeedTemplate extends BaseTemplate {
|
||||
public Feed $feed;
|
||||
public object $feed;
|
||||
|
||||
function __construct(string $title, Feed $feed) {
|
||||
function __construct(string $title, object $feed) {
|
||||
parent::__construct($title);
|
||||
$this->feed = $feed;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue