Wrapper update + Search by music id

This commit is contained in:
Pablo Ferreiro 2022-01-17 21:11:40 +01:00
parent fe5c2dc459
commit 4f35d68049
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
9 changed files with 77 additions and 17 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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",

12
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": "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"

View file

@ -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) {

View file

@ -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'])) {

View file

@ -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)

View file

@ -48,6 +48,18 @@
</div>
</form>
<hr />
<p>Search music videos by id:</p>
<form id="music_form">
<div class="field has-addons has-addons-centered">
<div class="control">
<input name="music_id" class="input" type="text" placeholder="Type music id" required />
</div>
<div class="control">
<button class="button is-success" type="submit">Go</button>
</div>
</div>
</form>
<hr />
<p>Trending:</p>
<a class="button is-success" href="./trending">Go</a>
</div>

20
views/music.latte Normal file
View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
{include 'components/head.latte', title: 'Music'}
<body>
{include 'components/navbar.latte'}
<section class="hero is-primary">
<div class="hero-body">
<div class="container has-text-centered">
<p class="title">{$feed->info->detail->music->title}</p>
<p class="subtitle">{$feed->info->detail->music->desc}</p>
<p>Videos: {number($feed->info->detail->stats->videoCount)}</p>
</div>
</div>
</section>
{include 'components/feed.latte'}
{include 'components/footer.latte'}
</body>
</html>