diff --git a/.env.example b/.env.example index 461a337..bf81d0f 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,5 @@ # APP_SUBDIR=/ # Subpath your app is running, defaults to / -# APP_CACHE=json # Cache engine for TikTok Api, defaults to json (more info on README) +# APP_CACHE=redis # Cache engine for TikTok Api, (more info on README) # Redis, used on Helpers\CahceEngines\RedisCache (CHOOSE ONE) diff --git a/README.md b/README.md index ae2e4d7..18b7ec2 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ php -S localhost:8080 ### .env Move the .env.example file to .env and modify it. -### Cache engine +### Cache engines Available cache engines: * redis: Writes response to Redis (check .env.example for config!) * json: Writes response to JSON file @@ -58,7 +58,7 @@ location /tiktok-viewer/.env { ## Credits * [TikTok-API-PHP](https://github.com/ssovit/TikTok-API-PHP) -* [steampixel/simple-php-router](https://github.com/steampixel/simple-php-router) +* [steampixel/simplePHPRouter](https://github.com/steampixel/simplePHPRouter) * [PHP dotenv](https://github.com/vlucas/phpdotenv) * [Bulma](https://github.com/jgthms/bulma) * [Bulmaswatch](https://github.com/jenil/bulmaswatch) diff --git a/composer.json b/composer.json index f198ad6..a0bfb86 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "ext-redis": "^5.3.4", + "ext-redis": "^5.3.2", "ssovit/tiktok-api": "dev-rework", "steampixel/simple-php-router": "^0.7.0", "latte/latte": "^2.10", diff --git a/composer.lock b/composer.lock index e998b7f..ca04850 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "d1cc0362ae4c8fb48a29dbb6fc07a5cf", + "content-hash": "2c6e8300b7f0672000799c44a5fd19a3", "packages": [ { "name": "graham-campbell/result-type", @@ -310,12 +310,12 @@ "source": { "type": "git", "url": "https://github.com/pablouser1/TikTok-API-PHP.git", - "reference": "71d758f069cfc5348f5d311b177caa2232488d96" + "reference": "1fda12510448b646cfa29a372a0f93bc12342053" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pablouser1/TikTok-API-PHP/zipball/71d758f069cfc5348f5d311b177caa2232488d96", - "reference": "71d758f069cfc5348f5d311b177caa2232488d96", + "url": "https://api.github.com/repos/pablouser1/TikTok-API-PHP/zipball/1fda12510448b646cfa29a372a0f93bc12342053", + "reference": "1fda12510448b646cfa29a372a0f93bc12342053", "shasum": "" }, "require": { @@ -357,7 +357,7 @@ "issues": "https://github.com/ssovit/TikTok-API-PHP/issues", "email": "sovit.tamrakar@gmail.com" }, - "time": "2022-01-14T16:59:51+00:00" + "time": "2022-01-17T20:03:34+00:00" }, { "name": "steampixel/simple-php-router", @@ -740,7 +740,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "ext-redis": "^5.3.4" + "ext-redis": "^5.3.2" }, "platform-dev": [], "plugin-api-version": "2.2.0" diff --git a/helpers/CacheEngines/RedisCache.php b/helpers/CacheEngines/RedisCache.php index eaaf97b..606f1b1 100644 --- a/helpers/CacheEngines/RedisCache.php +++ b/helpers/CacheEngines/RedisCache.php @@ -5,9 +5,13 @@ class RedisCache { private \Redis $client; function __construct(string $host, int $port, ?string $password) { $this->client = new \Redis(); - $this->client->connect($host, $port); + if (!$this->client->connect($host, $port)) { + throw new \Exception('REDIS: Could not connnect to server'); + } if ($password) { - $this->client->auth($password); + if (!$this->client->auth($password)) { + throw new \Exception('REDIS: Could not authenticate'); + } } } @@ -15,12 +19,12 @@ class RedisCache { $this->client->close(); } - public function get(string $cache_key): object|false { - if ($this->client->exists($cache_key)) { - $data_string = $this->client->get($cache_key); - return json_decode($data_string); + public function get(string $cache_key): ?object { + $data = $this->client->get($cache_key); + if ($data) { + return json_decode($data); } - return false; + return null; } public function set(string $cache_key, mixed $data, $timeout = 3600) { diff --git a/routes/index.php b/routes/index.php index d3aaf46..ab70b02 100644 --- a/routes/index.php +++ b/routes/index.php @@ -61,6 +61,22 @@ Route::add('/video/([^/]+)', function (string $video_id) { } }); +Route::add('/music/([^/]+)', function (string $music_id) { + $cursor = 0; + if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { + $cursor = (int) $_GET['cursor']; + } + + $api = Misc::api(); + $feed = $api->getMusicFeed($music_id, $cursor); + if ($feed->meta->success) { + $latte = Misc::latte(); + $latte->render(Misc::getView('music'), ['feed' => $feed]); + } else { + Error::show($feed->meta); + } +}); + Route::add('/tag/(\w+)', function (string $name) { $cursor = 0; if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) { diff --git a/scripts/home.js b/scripts/home.js index 5968a23..e2a7e19 100644 --- a/scripts/home.js +++ b/scripts/home.js @@ -19,6 +19,14 @@ const goToVideo = e => { window.location.href = `./video/${video_id}` } +const goToMusic = e => { + e.preventDefault() + const formData = new FormData(e.target) + const video_id = formData.get('music_id') + window.location.href = `./music/${video_id}` +} + document.getElementById('username_form').addEventListener('submit', goToUser, false) document.getElementById('tag_form').addEventListener('submit', goToTag, false) document.getElementById('video_form').addEventListener('submit', goToVideo, false) +document.getElementById('music_form').addEventListener('submit', goToMusic, false) diff --git a/views/home.latte b/views/home.latte index e19b483..d2eb4f4 100644 --- a/views/home.latte +++ b/views/home.latte @@ -48,6 +48,18 @@
Search music videos by id:
+ +Trending:
Go diff --git a/views/music.latte b/views/music.latte new file mode 100644 index 0000000..6aebd9d --- /dev/null +++ b/views/music.latte @@ -0,0 +1,20 @@ + + + +{include 'components/head.latte', title: 'Music'} + + + {include 'components/navbar.latte'} +{$feed->info->detail->music->title}
+{$feed->info->detail->music->desc}
+Videos: {number($feed->info->detail->stats->videoCount)}
+