Code cleanup, following and gif support
This commit is contained in:
parent
493f56a052
commit
30954f3d3a
21 changed files with 318 additions and 123 deletions
|
|
@ -1,104 +1,59 @@
|
|||
<?php
|
||||
require __DIR__ . '/assets.php';
|
||||
require __DIR__ . '/settings.php';
|
||||
require __DIR__ . "/../helpers/settings_elements.php";
|
||||
require __DIR__ . '/following.php';
|
||||
use Steampixel\Route;
|
||||
|
||||
// -- ROUTING HELPERS -- //
|
||||
function getApi(array $proxy_elements): \Sovit\TikTok\Api {
|
||||
$options = [];
|
||||
// Proxy config
|
||||
if (in_array($proxy_elements, $_COOKIE)) {
|
||||
foreach ($proxy_elements as $proxy_element) {
|
||||
$options[$proxy_element] = $_COOKIE[$proxy_element];
|
||||
}
|
||||
}
|
||||
$api = new \Sovit\TikTok\Api($options);
|
||||
return $api;
|
||||
}
|
||||
|
||||
function getLatte(): \Latte\Engine {
|
||||
$subdir = getSubdir();
|
||||
$latte = new Latte\Engine;
|
||||
$latte->setTempDirectory(__DIR__ . '/../cache/views');
|
||||
$latte->addFunction('assets', function (string $name, string $type) use ($subdir) {
|
||||
$path = "{$subdir}/{$type}/{$name}";
|
||||
return $path;
|
||||
});
|
||||
$latte->addFunction('path', function (string $name) use ($subdir) {
|
||||
$path = "{$subdir}/{$name}";
|
||||
return $path;
|
||||
});
|
||||
// https://stackoverflow.com/a/36365553
|
||||
$latte->addFunction('number', function (int $x) {
|
||||
if($x > 1000) {
|
||||
$x_number_format = number_format($x);
|
||||
$x_array = explode(',', $x_number_format);
|
||||
$x_parts = array('K', 'M', 'B', 'T');
|
||||
$x_count_parts = count($x_array) - 1;
|
||||
$x_display = $x;
|
||||
$x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
|
||||
$x_display .= $x_parts[$x_count_parts - 1];
|
||||
return $x_display;
|
||||
}
|
||||
return $x;
|
||||
});
|
||||
return $latte;
|
||||
}
|
||||
|
||||
function getView(string $template): string {
|
||||
return __DIR__ . "/../views/{$template}.latte";
|
||||
}
|
||||
use Helpers\Misc;
|
||||
|
||||
Route::add('/', function () {
|
||||
$latte = getLatte();
|
||||
$latte->render(getView('home'));
|
||||
$latte = Misc::latte();
|
||||
$latte->render(Misc::getView('home'));
|
||||
});
|
||||
|
||||
Route::add("/trending", function () use ($proxy_elements) {
|
||||
Route::add("/trending", function () {
|
||||
$cursor = 0;
|
||||
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
|
||||
$cursor = (int) $_GET['cursor'];
|
||||
}
|
||||
$latte = getLatte();
|
||||
$api = getApi($proxy_elements);
|
||||
$latte = Misc::latte();
|
||||
$api = Misc::api();
|
||||
$feed = $api->getTrendingFeed($cursor);
|
||||
if ($feed) {
|
||||
$latte->render(getView('trending'), ['feed' => $feed]);
|
||||
$latte->render(Misc::getView('trending'), ['feed' => $feed]);
|
||||
} else {
|
||||
return 'ERROR!';
|
||||
}
|
||||
});
|
||||
|
||||
Route::add("/@([^/]+)", function (string $username) use ($proxy_elements) {
|
||||
Route::add("/@([^/]+)", function (string $username) {
|
||||
$cursor = 0;
|
||||
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
|
||||
$cursor = (int) $_GET['cursor'];
|
||||
}
|
||||
$latte = getLatte();
|
||||
$api = getApi($proxy_elements);
|
||||
$latte = Misc::latte();
|
||||
$api = Misc::api();
|
||||
$feed = $api->getUserFeed($username, $cursor);
|
||||
if ($feed) {
|
||||
if ($feed->info->detail->user->privateAccount) {
|
||||
http_response_code(400);
|
||||
return 'Private account detected! Not supported';
|
||||
}
|
||||
$latte->render(getView('user'), ['feed' => $feed]);
|
||||
$latte->render(Misc::getView('user'), ['feed' => $feed]);
|
||||
} else {
|
||||
return 'ERROR!';
|
||||
}
|
||||
});
|
||||
|
||||
Route::add('/tag/(\w+)', function (string $name) use ($proxy_elements) {
|
||||
Route::add('/tag/(\w+)', function (string $name) {
|
||||
$cursor = 0;
|
||||
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
|
||||
$cursor = (int) $_GET['cursor'];
|
||||
}
|
||||
$latte = getLatte();
|
||||
$api = getApi($proxy_elements);
|
||||
$latte = Misc::latte();
|
||||
$api = Misc::api();
|
||||
$feed = $api->getChallengeFeed($name, $cursor);
|
||||
if ($feed) {
|
||||
$latte->render(getView('tag'), ['feed' => $feed]);
|
||||
$latte->render(Misc::getView('tag'), ['feed' => $feed]);
|
||||
} else {
|
||||
return 'ERROR!';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue