Templates based on classes, cache custom paths
This commit is contained in:
parent
dde4185ad7
commit
6d6ec109ca
14
.env.example
14
.env.example
|
@ -1,7 +1,11 @@
|
||||||
# APP_SUBDIR=/ # Subpath your app is running, defaults to /
|
# APP_SUBDIR=/ # Subpath your app is running, leave commented for /
|
||||||
# APP_CACHE=redis # Cache engine for TikTok Api, (more info on README)
|
|
||||||
|
|
||||||
# Redis, used on Helpers\CahceEngines\RedisCache (CHOOSE ONE)
|
# 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_URL=redis://:password@host:port # If using password
|
# Redis cache, used on Helpers\CacheEngines\RedisCache (CHOOSE ONE)
|
||||||
# REDIS_URL=redis://host:port # If not using password
|
# API_CACHE_REDIS_URL=redis://:password@host:port # If using password
|
||||||
|
# API_CACHE_REDIS_URL=redis://host:port # If not using password
|
||||||
|
|
||||||
|
# JSON cache, used on Helpers\CacheEngines\JSONCache
|
||||||
|
# API_CACHE_JSON=/tmp/proxitok_api # Path for JSON API Cache, leave commented for ./cache/api
|
||||||
|
|
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -2,7 +2,7 @@ node_modules
|
||||||
/.env
|
/.env
|
||||||
/.vscode
|
/.vscode
|
||||||
/vendor
|
/vendor
|
||||||
/cache/views/*
|
/cache/latte/*
|
||||||
!/cache/views/.gitkeep
|
!/cache/latte/.gitkeep
|
||||||
/cache/api/*
|
/cache/api/*
|
||||||
!/cache/api/.gitkeep
|
!/cache/api/.gitkeep
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
<footer class="footer">
|
<footer class="footer">
|
||||||
<div class="content">
|
<div class="content has-text-centered">
|
||||||
<p class="has-text-centered">
|
<p>
|
||||||
Made with <span style="color: #e25555;">♥</span> in <a href="https://github.com/pablouser1/ProxiTok">Github</a>
|
Made with <span style="color: #e25555;">♥</span> in <a href="https://github.com/pablouser1/ProxiTok">Github</a>
|
||||||
</p>
|
</p>
|
||||||
|
<p>Version: {version()}</p>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "pablouser1/proxitok",
|
"name": "pablouser1/proxitok",
|
||||||
"description": "An alternative frontend for TikTok",
|
"description": "An alternative frontend for TikTok",
|
||||||
"version": "1.1.2",
|
"version": "1.2.0",
|
||||||
"license": "AGPL-3.0-or-later",
|
"license": "AGPL-3.0-or-later",
|
||||||
"repositories": [
|
"repositories": [
|
||||||
{
|
{
|
||||||
|
@ -18,7 +18,8 @@
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Helpers\\": "helpers/"
|
"Helpers\\": "helpers/",
|
||||||
|
"Views\\Models\\": "views/models/"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,23 +2,30 @@
|
||||||
namespace Helpers\CacheEngines;
|
namespace Helpers\CacheEngines;
|
||||||
|
|
||||||
class JSONCache {
|
class JSONCache {
|
||||||
const CACHE_PATH = __DIR__ . '/../../cache/api/';
|
private string $cache_path = __DIR__ . '/../../cache/api';
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
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|false {
|
public function get(string $cache_key): object|false {
|
||||||
if (is_file(self::CACHE_PATH . $cache_key . '.json')) {
|
$filename = $this->cache_path . '/' . $cache_key . '.json';
|
||||||
|
if (is_file($filename)) {
|
||||||
$time = time();
|
$time = time();
|
||||||
$json_string = file_get_contents(self::CACHE_PATH . $cache_key . '.json');
|
$json_string = file_get_contents($filename);
|
||||||
$element = json_decode($json_string);
|
$element = json_decode($json_string);
|
||||||
if ($time < $element->expires) {
|
if ($time < $element->expires) {
|
||||||
return $element->data;
|
return $element->data;
|
||||||
}
|
}
|
||||||
// Remove file if expired
|
// Remove file if expired
|
||||||
unlink(self::CACHE_PATH . $cache_key . '.json');
|
unlink($filename);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
||||||
file_put_contents(self::CACHE_PATH . $cache_key . '.json', json_encode([
|
file_put_contents($this->cache_path . '/' . $cache_key . '.json', json_encode([
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
'expires' => time() + $timeout
|
'expires' => time() + $timeout
|
||||||
]));
|
]));
|
||||||
|
|
|
@ -25,8 +25,8 @@ class Misc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Cache config
|
// Cache config
|
||||||
if (isset($_ENV['APP_CACHE'])) {
|
if (isset($_ENV['API_CACHE'])) {
|
||||||
switch ($_ENV['APP_CACHE']) {
|
switch ($_ENV['API_CACHE']) {
|
||||||
case 'json':
|
case 'json':
|
||||||
$cacheEngine = new JSONCache();
|
$cacheEngine = new JSONCache();
|
||||||
break;
|
break;
|
||||||
|
@ -54,15 +54,24 @@ class Misc {
|
||||||
$subdir = '';
|
$subdir = '';
|
||||||
}
|
}
|
||||||
$latte = new \Latte\Engine;
|
$latte = new \Latte\Engine;
|
||||||
$latte->setTempDirectory(__DIR__ . '/../cache/views');
|
$cache_path = isset($_ENV['LATTE_CACHE']) && !empty($_ENV['LATTE_CACHE']) ? $_ENV['LATTE_CACHE'] : __DIR__ . '/../cache/latte';
|
||||||
|
$latte->setTempDirectory($cache_path);
|
||||||
|
|
||||||
|
// -- CUSTOM FUNCTIONS -- //
|
||||||
|
// Import assets
|
||||||
$latte->addFunction('assets', function (string $name, string $type) use ($subdir) {
|
$latte->addFunction('assets', function (string $name, string $type) use ($subdir) {
|
||||||
$path = "{$subdir}/{$type}/{$name}";
|
$path = "{$subdir}/{$type}/{$name}";
|
||||||
return $path;
|
return $path;
|
||||||
});
|
});
|
||||||
|
// Relative path
|
||||||
$latte->addFunction('path', function (string $name) use ($subdir) {
|
$latte->addFunction('path', function (string $name) use ($subdir) {
|
||||||
$path = "{$subdir}/{$name}";
|
$path = "{$subdir}/{$name}";
|
||||||
return $path;
|
return $path;
|
||||||
});
|
});
|
||||||
|
// Version being used
|
||||||
|
$latte->addFunction('version', function () {
|
||||||
|
return \Composer\InstalledVersions::getVersion('pablouser1/proxitok');
|
||||||
|
});
|
||||||
// https://stackoverflow.com/a/36365553
|
// https://stackoverflow.com/a/36365553
|
||||||
$latte->addFunction('number', function (int $x) {
|
$latte->addFunction('number', function (int $x) {
|
||||||
if($x > 1000) {
|
if($x > 1000) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
use Helpers\Following;
|
use Helpers\Following;
|
||||||
use Helpers\Misc;
|
use Helpers\Misc;
|
||||||
use Steampixel\Route;
|
use Steampixel\Route;
|
||||||
|
use Views\Models\FollowingTemplate;
|
||||||
|
|
||||||
// Showing
|
// Showing
|
||||||
Route::add('/following', function () {
|
Route::add('/following', function () {
|
||||||
|
@ -28,9 +29,5 @@ Route::add('/following', function () {
|
||||||
'hasMore' => false
|
'hasMore' => false
|
||||||
];
|
];
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('following'), [
|
$latte->render(Misc::getView('following'), new FollowingTemplate($following, $feed));
|
||||||
'following' => $following,
|
|
||||||
'feed' => $feed,
|
|
||||||
'title' => 'Following'
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,18 +2,22 @@
|
||||||
require __DIR__ . '/assets.php';
|
require __DIR__ . '/assets.php';
|
||||||
require __DIR__ . '/settings.php';
|
require __DIR__ . '/settings.php';
|
||||||
require __DIR__ . '/following.php';
|
require __DIR__ . '/following.php';
|
||||||
|
|
||||||
use Steampixel\Route;
|
use Steampixel\Route;
|
||||||
use Helpers\Misc;
|
use Helpers\Misc;
|
||||||
use Helpers\Error;
|
use Helpers\Error;
|
||||||
|
use Views\Models\BaseTemplate;
|
||||||
|
use Views\Models\FeedTemplate;
|
||||||
|
use Views\Models\ItemTemplate;
|
||||||
|
|
||||||
Route::add('/', function () {
|
Route::add('/', function () {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('home'), ['title' => 'Home']);
|
$latte->render(Misc::getView('home'), new BaseTemplate('Home'));
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::add('/about', function () {
|
Route::add('/about', function () {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('about'), ['title' => 'About']);
|
$latte->render(Misc::getView('about'), new BaseTemplate('About'));
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::add("/trending", function () {
|
Route::add("/trending", function () {
|
||||||
|
@ -25,10 +29,7 @@ Route::add("/trending", function () {
|
||||||
$feed = $api->getTrendingFeed($cursor);
|
$feed = $api->getTrendingFeed($cursor);
|
||||||
if ($feed->meta->success) {
|
if ($feed->meta->success) {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('trending'), [
|
$latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed));
|
||||||
'feed' => $feed,
|
|
||||||
'title' => 'Trending'
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
Error::show($feed->meta);
|
Error::show($feed->meta);
|
||||||
}
|
}
|
||||||
|
@ -47,10 +48,7 @@ Route::add("/@([^/]+)", function (string $username) {
|
||||||
return 'Private account detected! Not supported';
|
return 'Private account detected! Not supported';
|
||||||
}
|
}
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('user'), [
|
$latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed));
|
||||||
'feed' => $feed,
|
|
||||||
'title' => $feed->info->detail->user->nickname
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
Error::show($feed->meta);
|
Error::show($feed->meta);
|
||||||
}
|
}
|
||||||
|
@ -61,10 +59,7 @@ Route::add('/video/([^/]+)', function (string $video_id) {
|
||||||
$item = $api->getVideoByID($video_id);
|
$item = $api->getVideoByID($video_id);
|
||||||
if ($item->meta->success) {
|
if ($item->meta->success) {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('video'), [
|
$latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item));
|
||||||
'item' => $item,
|
|
||||||
'title' => $item->info->detail->user->nickname
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
Error::show($item->meta);
|
Error::show($item->meta);
|
||||||
}
|
}
|
||||||
|
@ -80,10 +75,7 @@ Route::add('/music/([^/]+)', function (string $music_id) {
|
||||||
$feed = $api->getMusicFeed($music_id, $cursor);
|
$feed = $api->getMusicFeed($music_id, $cursor);
|
||||||
if ($feed->meta->success) {
|
if ($feed->meta->success) {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('music'), [
|
$latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed));
|
||||||
'feed' => $feed,
|
|
||||||
'title' => 'Music'
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
Error::show($feed->meta);
|
Error::show($feed->meta);
|
||||||
}
|
}
|
||||||
|
@ -98,10 +90,7 @@ Route::add('/tag/(\w+)', function (string $name) {
|
||||||
$feed = $api->getChallengeFeed($name, $cursor);
|
$feed = $api->getChallengeFeed($name, $cursor);
|
||||||
if ($feed->meta->success) {
|
if ($feed->meta->success) {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('tag'), [
|
$latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed));
|
||||||
'feed' => $feed,
|
|
||||||
'title' => 'Tag'
|
|
||||||
]);
|
|
||||||
} else {
|
} else {
|
||||||
Error::show($feed->meta);
|
Error::show($feed->meta);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,12 @@
|
||||||
use Helpers\Following;
|
use Helpers\Following;
|
||||||
use Helpers\Settings;
|
use Helpers\Settings;
|
||||||
use Helpers\Misc;
|
use Helpers\Misc;
|
||||||
|
use Views\Models\SettingsTemplate;
|
||||||
use Steampixel\Route;
|
use Steampixel\Route;
|
||||||
|
|
||||||
Route::add("/settings", function () {
|
Route::add("/settings", function () {
|
||||||
$latte = Misc::latte();
|
$latte = Misc::latte();
|
||||||
$latte->render(Misc::getView('settings'), [
|
$latte->render(Misc::getView('settings'), new SettingsTemplate());
|
||||||
"proxy_elements" => Settings::PROXY,
|
|
||||||
"following" => Following::get(),
|
|
||||||
"title" => "Settings"
|
|
||||||
]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::add("/settings/proxy", function () {
|
Route::add("/settings/proxy", function () {
|
||||||
|
|
|
@ -33,9 +33,9 @@ bulmaswatch@^0.8.1:
|
||||||
integrity sha512-7HGm5v9If6gzxbTht4/oVS0dhySp6g/JyTrxmpSXHXgDQXivvxiuVmcJOZo3PFv9GAOn4om7SK36I2V8W81sgw==
|
integrity sha512-7HGm5v9If6gzxbTht4/oVS0dhySp6g/JyTrxmpSXHXgDQXivvxiuVmcJOZo3PFv9GAOn4om7SK36I2V8W81sgw==
|
||||||
|
|
||||||
"chokidar@>=3.0.0 <4.0.0":
|
"chokidar@>=3.0.0 <4.0.0":
|
||||||
version "3.5.2"
|
version "3.5.3"
|
||||||
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75"
|
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd"
|
||||||
integrity sha512-ekGhOnNVPgT77r4K/U3GDhu+FQ2S8TnK/s2KbIGXi0SZWuwkZ2QNyfWdZW+TVfn84DpEP7rLeCt2UI6bJ8GwbQ==
|
integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==
|
||||||
dependencies:
|
dependencies:
|
||||||
anymatch "~3.1.2"
|
anymatch "~3.1.2"
|
||||||
braces "~3.0.2"
|
braces "~3.0.2"
|
||||||
|
@ -113,18 +113,18 @@ readdirp@~3.6.0:
|
||||||
picomatch "^2.2.1"
|
picomatch "^2.2.1"
|
||||||
|
|
||||||
sass@^1.46.0:
|
sass@^1.46.0:
|
||||||
version "1.48.0"
|
version "1.49.0"
|
||||||
resolved "https://registry.yarnpkg.com/sass/-/sass-1.48.0.tgz#b53cfccc1b8ab4be375cc54f306fda9d4711162c"
|
resolved "https://registry.yarnpkg.com/sass/-/sass-1.49.0.tgz#65ec1b1d9a6bc1bae8d2c9d4b392c13f5d32c078"
|
||||||
integrity sha512-hQi5g4DcfjcipotoHZ80l7GNJHGqQS5LwMBjVYB/TaT0vcSSpbgM8Ad7cgfsB2M0MinbkEQQPO9+sjjSiwxqmw==
|
integrity sha512-TVwVdNDj6p6b4QymJtNtRS2YtLJ/CqZriGg0eIAbAKMlN8Xy6kbv33FsEZSF7FufFFM705SQviHjjThfaQ4VNw==
|
||||||
dependencies:
|
dependencies:
|
||||||
chokidar ">=3.0.0 <4.0.0"
|
chokidar ">=3.0.0 <4.0.0"
|
||||||
immutable "^4.0.0"
|
immutable "^4.0.0"
|
||||||
source-map-js ">=0.6.2 <2.0.0"
|
source-map-js ">=0.6.2 <2.0.0"
|
||||||
|
|
||||||
"source-map-js@>=0.6.2 <2.0.0":
|
"source-map-js@>=0.6.2 <2.0.0":
|
||||||
version "1.0.1"
|
version "1.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.1.tgz#a1741c131e3c77d048252adfa24e23b908670caf"
|
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"
|
||||||
integrity sha512-4+TN2b3tqOCd/kaGRJ/sTYA0tR0mdXx26ipdolxcwtJVqEnqNYvlCAt1q3ypy4QMlYus+Zh34RNtYLoq2oQ4IA==
|
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==
|
||||||
|
|
||||||
to-regex-range@^5.0.1:
|
to-regex-range@^5.0.1:
|
||||||
version "5.0.1"
|
version "5.0.1"
|
||||||
|
|
14
views/models/BaseTemplate.php
Normal file
14
views/models/BaseTemplate.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
namespace Views\Models;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base for all templates, needs a Title to set
|
||||||
|
*/
|
||||||
|
class BaseTemplate {
|
||||||
|
public string $title;
|
||||||
|
public string $version;
|
||||||
|
|
||||||
|
function __construct(string $title) {
|
||||||
|
$this->title = $title;
|
||||||
|
}
|
||||||
|
}
|
14
views/models/FeedTemplate.php
Normal file
14
views/models/FeedTemplate.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
namespace Views\Models;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base for templates with a feed
|
||||||
|
*/
|
||||||
|
class FeedTemplate extends BaseTemplate {
|
||||||
|
public object $feed;
|
||||||
|
|
||||||
|
function __construct(string $title, object $feed) {
|
||||||
|
parent::__construct($title);
|
||||||
|
$this->feed = $feed;
|
||||||
|
}
|
||||||
|
}
|
14
views/models/FollowingTemplate.php
Normal file
14
views/models/FollowingTemplate.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
namespace Views\Models;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exclusive for /following
|
||||||
|
*/
|
||||||
|
class FollowingTemplate extends FeedTemplate {
|
||||||
|
public array $following;
|
||||||
|
|
||||||
|
function __construct(array $following, object $feed) {
|
||||||
|
parent::__construct('Following', $feed);
|
||||||
|
$this->following = $following;
|
||||||
|
}
|
||||||
|
}
|
14
views/models/ItemTemplate.php
Normal file
14
views/models/ItemTemplate.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
namespace Views\Models;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base for templates with item (only /video at the time)
|
||||||
|
*/
|
||||||
|
class ItemTemplate extends BaseTemplate {
|
||||||
|
public object $item;
|
||||||
|
|
||||||
|
function __construct(string $title, object $item) {
|
||||||
|
parent::__construct($title);
|
||||||
|
$this->item = $item;
|
||||||
|
}
|
||||||
|
}
|
19
views/models/SettingsTemplate.php
Normal file
19
views/models/SettingsTemplate.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
namespace Views\Models;
|
||||||
|
|
||||||
|
use \Helpers\Following;
|
||||||
|
use \Helpers\Settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exclusive for /settings
|
||||||
|
*/
|
||||||
|
class SettingsTemplate extends BaseTemplate {
|
||||||
|
public array $proxy_elements = [];
|
||||||
|
public array $following = [];
|
||||||
|
|
||||||
|
function __construct() {
|
||||||
|
parent::__construct('Settings');
|
||||||
|
$this->proxy_elements = Settings::PROXY;
|
||||||
|
$this->following = Following::get();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue