From 6d6ec109ca5c728effda95699261470d13d35a63 Mon Sep 17 00:00:00 2001 From: Pablo Ferreiro Date: Tue, 25 Jan 2022 17:20:11 +0100 Subject: [PATCH] Templates based on classes, cache custom paths --- .env.example | 14 ++-- .gitignore | 4 +- cache/{views => latte}/.gitkeep | 0 components/footer.latte | 5 +- composer.json | 5 +- helpers/CacheEngines/JSONCache.php | 17 +++-- helpers/Misc.php | 15 ++++- routes/following.php | 7 +- routes/index.php | 33 +++------ routes/settings.php | 7 +- styles/scss/yarn.lock | 18 ++--- views/about.latte | 2 +- views/error.latte | 10 +-- views/following.latte | 2 +- views/home.latte | 104 ++++++++++++++--------------- views/models/BaseTemplate.php | 14 ++++ views/models/FeedTemplate.php | 14 ++++ views/models/FollowingTemplate.php | 14 ++++ views/models/ItemTemplate.php | 14 ++++ views/models/SettingsTemplate.php | 19 ++++++ views/music.latte | 6 +- views/settings.latte | 16 ++--- views/tag.latte | 6 +- views/trending.latte | 2 +- views/user.latte | 14 ++-- views/video.latte | 30 ++++----- 26 files changed, 236 insertions(+), 156 deletions(-) rename cache/{views => latte}/.gitkeep (100%) create mode 100644 views/models/BaseTemplate.php create mode 100644 views/models/FeedTemplate.php create mode 100644 views/models/FollowingTemplate.php create mode 100644 views/models/ItemTemplate.php create mode 100644 views/models/SettingsTemplate.php diff --git a/.env.example b/.env.example index bf81d0f..e278745 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,11 @@ -# APP_SUBDIR=/ # Subpath your app is running, defaults to / -# APP_CACHE=redis # Cache engine for TikTok Api, (more info on README) +# APP_SUBDIR=/ # Subpath your app is running, leave commented for / -# 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_URL=redis://host:port # If not using password +# Redis cache, used on Helpers\CacheEngines\RedisCache (CHOOSE ONE) +# 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 diff --git a/.gitignore b/.gitignore index 17601c5..9792dee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ node_modules /.env /.vscode /vendor -/cache/views/* -!/cache/views/.gitkeep +/cache/latte/* +!/cache/latte/.gitkeep /cache/api/* !/cache/api/.gitkeep diff --git a/cache/views/.gitkeep b/cache/latte/.gitkeep similarity index 100% rename from cache/views/.gitkeep rename to cache/latte/.gitkeep diff --git a/components/footer.latte b/components/footer.latte index 0117a17..cc6a0b9 100644 --- a/components/footer.latte +++ b/components/footer.latte @@ -1,7 +1,8 @@ diff --git a/composer.json b/composer.json index a0bfb86..70c33b0 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "pablouser1/proxitok", "description": "An alternative frontend for TikTok", - "version": "1.1.2", + "version": "1.2.0", "license": "AGPL-3.0-or-later", "repositories": [ { @@ -18,7 +18,8 @@ }, "autoload": { "psr-4": { - "Helpers\\": "helpers/" + "Helpers\\": "helpers/", + "Views\\Models\\": "views/models/" } } } diff --git a/helpers/CacheEngines/JSONCache.php b/helpers/CacheEngines/JSONCache.php index 1d5da6c..982c8e3 100644 --- a/helpers/CacheEngines/JSONCache.php +++ b/helpers/CacheEngines/JSONCache.php @@ -2,23 +2,30 @@ namespace Helpers\CacheEngines; 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 { - if (is_file(self::CACHE_PATH . $cache_key . '.json')) { + $filename = $this->cache_path . '/' . $cache_key . '.json'; + if (is_file($filename)) { $time = time(); - $json_string = file_get_contents(self::CACHE_PATH . $cache_key . '.json'); + $json_string = file_get_contents($filename); $element = json_decode($json_string); if ($time < $element->expires) { return $element->data; } // Remove file if expired - unlink(self::CACHE_PATH . $cache_key . '.json'); + unlink($filename); } return false; } 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, 'expires' => time() + $timeout ])); diff --git a/helpers/Misc.php b/helpers/Misc.php index 9d116a1..0a56daa 100644 --- a/helpers/Misc.php +++ b/helpers/Misc.php @@ -25,8 +25,8 @@ class Misc { } } // Cache config - if (isset($_ENV['APP_CACHE'])) { - switch ($_ENV['APP_CACHE']) { + if (isset($_ENV['API_CACHE'])) { + switch ($_ENV['API_CACHE']) { case 'json': $cacheEngine = new JSONCache(); break; @@ -54,15 +54,24 @@ class Misc { $subdir = ''; } $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) { $path = "{$subdir}/{$type}/{$name}"; return $path; }); + // Relative path $latte->addFunction('path', function (string $name) use ($subdir) { $path = "{$subdir}/{$name}"; return $path; }); + // Version being used + $latte->addFunction('version', function () { + return \Composer\InstalledVersions::getVersion('pablouser1/proxitok'); + }); // https://stackoverflow.com/a/36365553 $latte->addFunction('number', function (int $x) { if($x > 1000) { diff --git a/routes/following.php b/routes/following.php index 3735045..1509907 100644 --- a/routes/following.php +++ b/routes/following.php @@ -2,6 +2,7 @@ use Helpers\Following; use Helpers\Misc; use Steampixel\Route; +use Views\Models\FollowingTemplate; // Showing Route::add('/following', function () { @@ -28,9 +29,5 @@ Route::add('/following', function () { 'hasMore' => false ]; $latte = Misc::latte(); - $latte->render(Misc::getView('following'), [ - 'following' => $following, - 'feed' => $feed, - 'title' => 'Following' - ]); + $latte->render(Misc::getView('following'), new FollowingTemplate($following, $feed)); }); diff --git a/routes/index.php b/routes/index.php index bed9369..39d7990 100644 --- a/routes/index.php +++ b/routes/index.php @@ -2,18 +2,22 @@ require __DIR__ . '/assets.php'; require __DIR__ . '/settings.php'; require __DIR__ . '/following.php'; + use Steampixel\Route; use Helpers\Misc; use Helpers\Error; +use Views\Models\BaseTemplate; +use Views\Models\FeedTemplate; +use Views\Models\ItemTemplate; Route::add('/', function () { $latte = Misc::latte(); - $latte->render(Misc::getView('home'), ['title' => 'Home']); + $latte->render(Misc::getView('home'), new BaseTemplate('Home')); }); Route::add('/about', function () { $latte = Misc::latte(); - $latte->render(Misc::getView('about'), ['title' => 'About']); + $latte->render(Misc::getView('about'), new BaseTemplate('About')); }); Route::add("/trending", function () { @@ -25,10 +29,7 @@ Route::add("/trending", function () { $feed = $api->getTrendingFeed($cursor); if ($feed->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('trending'), [ - 'feed' => $feed, - 'title' => 'Trending' - ]); + $latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed)); } else { Error::show($feed->meta); } @@ -47,10 +48,7 @@ Route::add("/@([^/]+)", function (string $username) { return 'Private account detected! Not supported'; } $latte = Misc::latte(); - $latte->render(Misc::getView('user'), [ - 'feed' => $feed, - 'title' => $feed->info->detail->user->nickname - ]); + $latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed)); } else { Error::show($feed->meta); } @@ -61,10 +59,7 @@ Route::add('/video/([^/]+)', function (string $video_id) { $item = $api->getVideoByID($video_id); if ($item->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('video'), [ - 'item' => $item, - 'title' => $item->info->detail->user->nickname - ]); + $latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item)); } else { Error::show($item->meta); } @@ -80,10 +75,7 @@ Route::add('/music/([^/]+)', function (string $music_id) { $feed = $api->getMusicFeed($music_id, $cursor); if ($feed->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('music'), [ - 'feed' => $feed, - 'title' => 'Music' - ]); + $latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed)); } else { Error::show($feed->meta); } @@ -98,10 +90,7 @@ Route::add('/tag/(\w+)', function (string $name) { $feed = $api->getChallengeFeed($name, $cursor); if ($feed->meta->success) { $latte = Misc::latte(); - $latte->render(Misc::getView('tag'), [ - 'feed' => $feed, - 'title' => 'Tag' - ]); + $latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed)); } else { Error::show($feed->meta); } diff --git a/routes/settings.php b/routes/settings.php index a3296bf..70f1f31 100644 --- a/routes/settings.php +++ b/routes/settings.php @@ -3,15 +3,12 @@ use Helpers\Following; use Helpers\Settings; use Helpers\Misc; +use Views\Models\SettingsTemplate; use Steampixel\Route; Route::add("/settings", function () { $latte = Misc::latte(); - $latte->render(Misc::getView('settings'), [ - "proxy_elements" => Settings::PROXY, - "following" => Following::get(), - "title" => "Settings" - ]); + $latte->render(Misc::getView('settings'), new SettingsTemplate()); }); Route::add("/settings/proxy", function () { diff --git a/styles/scss/yarn.lock b/styles/scss/yarn.lock index 59d7189..2fdfbf6 100644 --- a/styles/scss/yarn.lock +++ b/styles/scss/yarn.lock @@ -33,9 +33,9 @@ bulmaswatch@^0.8.1: integrity sha512-7HGm5v9If6gzxbTht4/oVS0dhySp6g/JyTrxmpSXHXgDQXivvxiuVmcJOZo3PFv9GAOn4om7SK36I2V8W81sgw== "chokidar@>=3.0.0 <4.0.0": - version "3.5.2" - resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.2.tgz#dba3976fcadb016f66fd365021d91600d01c1e75" - integrity sha512-ekGhOnNVPgT77r4K/U3GDhu+FQ2S8TnK/s2KbIGXi0SZWuwkZ2QNyfWdZW+TVfn84DpEP7rLeCt2UI6bJ8GwbQ== + version "3.5.3" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" + integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== dependencies: anymatch "~3.1.2" braces "~3.0.2" @@ -113,18 +113,18 @@ readdirp@~3.6.0: picomatch "^2.2.1" sass@^1.46.0: - version "1.48.0" - resolved "https://registry.yarnpkg.com/sass/-/sass-1.48.0.tgz#b53cfccc1b8ab4be375cc54f306fda9d4711162c" - integrity sha512-hQi5g4DcfjcipotoHZ80l7GNJHGqQS5LwMBjVYB/TaT0vcSSpbgM8Ad7cgfsB2M0MinbkEQQPO9+sjjSiwxqmw== + version "1.49.0" + resolved "https://registry.yarnpkg.com/sass/-/sass-1.49.0.tgz#65ec1b1d9a6bc1bae8d2c9d4b392c13f5d32c078" + integrity sha512-TVwVdNDj6p6b4QymJtNtRS2YtLJ/CqZriGg0eIAbAKMlN8Xy6kbv33FsEZSF7FufFFM705SQviHjjThfaQ4VNw== dependencies: chokidar ">=3.0.0 <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": - version "1.0.1" - resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.1.tgz#a1741c131e3c77d048252adfa24e23b908670caf" - integrity sha512-4+TN2b3tqOCd/kaGRJ/sTYA0tR0mdXx26ipdolxcwtJVqEnqNYvlCAt1q3ypy4QMlYus+Zh34RNtYLoq2oQ4IA== + version "1.0.2" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c" + integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw== to-regex-range@^5.0.1: version "5.0.1" diff --git a/views/about.latte b/views/about.latte index e8c9028..1299bef 100644 --- a/views/about.latte +++ b/views/about.latte @@ -1,7 +1,7 @@ {layout '../layouts/default.latte'} {block header} -

About

+

About

{/block} {block content} diff --git a/views/error.latte b/views/error.latte index 687e861..071a00d 100644 --- a/views/error.latte +++ b/views/error.latte @@ -1,9 +1,9 @@ {layout '../layouts/hero.latte'} {block content} -

There was an error processing your request!

-

HTTP Code: {$error->http_code}

-{if $error->tiktok_code} -

API error code {$error->tiktok_code} ({$error->tiktok_msg})

-{/if} +

There was an error processing your request!

+

HTTP Code: {$error->http_code}

+ {if $error->tiktok_code} +

API error code {$error->tiktok_code} ({$error->tiktok_msg})

+ {/if} {/block} diff --git a/views/following.latte b/views/following.latte index 3256286..e88251a 100644 --- a/views/following.latte +++ b/views/following.latte @@ -1,7 +1,7 @@ {layout '../layouts/default.latte'} {block header} -

Following

+

Following

{/block} {block content} diff --git a/views/home.latte b/views/home.latte index 15940db..a102bc9 100644 --- a/views/home.latte +++ b/views/home.latte @@ -1,58 +1,58 @@ {layout '../layouts/hero.latte'} {block content} -

Welcome to ProxiTok!

-

An alternative open source frontend for TikTok

-

Search user:

-
-
-
- -
-
- -
-
-
-
-

Search video by id:

-
-
-
- -
-
- -
-
-
-
-

Search tag:

-
-
-
- -
-
- -
-
-
-
-

Search music videos by id:

-
-
-
- -
-
- -
-
-
-
-

Trending:

-Go +

Welcome to ProxiTok!

+

An alternative open source frontend for TikTok

+

Search user:

+
+
+
+ +
+
+ +
+
+
+
+

Search video by id:

+
+
+
+ +
+
+ +
+
+
+
+

Search tag:

+
+
+
+ +
+
+ +
+
+
+
+

Search music videos by id:

+
+
+
+ +
+
+ +
+
+
+
+

Trending:

+ Go {/block} {block extra} diff --git a/views/models/BaseTemplate.php b/views/models/BaseTemplate.php new file mode 100644 index 0000000..e1179bf --- /dev/null +++ b/views/models/BaseTemplate.php @@ -0,0 +1,14 @@ +title = $title; + } +} diff --git a/views/models/FeedTemplate.php b/views/models/FeedTemplate.php new file mode 100644 index 0000000..855e53b --- /dev/null +++ b/views/models/FeedTemplate.php @@ -0,0 +1,14 @@ +feed = $feed; + } +} diff --git a/views/models/FollowingTemplate.php b/views/models/FollowingTemplate.php new file mode 100644 index 0000000..19e0fe2 --- /dev/null +++ b/views/models/FollowingTemplate.php @@ -0,0 +1,14 @@ +following = $following; + } +} diff --git a/views/models/ItemTemplate.php b/views/models/ItemTemplate.php new file mode 100644 index 0000000..cc23a75 --- /dev/null +++ b/views/models/ItemTemplate.php @@ -0,0 +1,14 @@ +item = $item; + } +} diff --git a/views/models/SettingsTemplate.php b/views/models/SettingsTemplate.php new file mode 100644 index 0000000..6005373 --- /dev/null +++ b/views/models/SettingsTemplate.php @@ -0,0 +1,19 @@ +proxy_elements = Settings::PROXY; + $this->following = Following::get(); + } +} diff --git a/views/music.latte b/views/music.latte index cd9ec73..99cea03 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->music->title}

+

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

+

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

{/block} {block content} diff --git a/views/settings.latte b/views/settings.latte index 87ad311..b33e511 100644 --- a/views/settings.latte +++ b/views/settings.latte @@ -1,15 +1,15 @@ {layout '../layouts/default.latte'} {block header} -

Settings

+

Settings

{/block} {block content} - -

Proxy

-{include '../components/settings/proxy.latte'} -
- -

Following

-{include '../components/settings/following.latte'} + +

Proxy

+ {include '../components/settings/proxy.latte'} +
+ +

Following

+ {include '../components/settings/following.latte'} {/block} diff --git a/views/tag.latte b/views/tag.latte index b954450..43aea6e 100644 --- a/views/tag.latte +++ b/views/tag.latte @@ -1,9 +1,9 @@ {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)}

+

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

+

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

+

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

{/block} {block content} diff --git a/views/trending.latte b/views/trending.latte index 1f89f63..bc367bd 100644 --- a/views/trending.latte +++ b/views/trending.latte @@ -1,7 +1,7 @@ {layout '../layouts/default.latte'} {block header} -

Trending

+

Trending

{/block} {block content} diff --git a/views/user.latte b/views/user.latte index a84061d..7aa8c2f 100644 --- a/views/user.latte +++ b/views/user.latte @@ -1,13 +1,13 @@ {layout '../layouts/default.latte'} {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}

+
+ +
+

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

{/block} {block content} diff --git a/views/video.latte b/views/video.latte index e11488f..7192eff 100644 --- a/views/video.latte +++ b/views/video.latte @@ -1,21 +1,21 @@ {layout '../layouts/hero.latte'} {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

-
- Download video -

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

- +
+
+ +
+
+
+

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

+
+ Download video +

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

+ +
-
{/block}