RSS initial support

This commit is contained in:
Pablo Ferreiro 2022-01-28 15:54:09 +01:00
parent ecdc8241e7
commit bd1642957c
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
21 changed files with 329 additions and 161 deletions

View file

@ -1,10 +1,10 @@
<?php
use Steampixel\Route;
const VALID_TIKTOK_DOMAINS = [
"tiktokcdn.com", "tiktokcdn-us.com", "tiktok.com"
];
/**@var Bramus\Router\Router $router */
/**
* Check if an url has a valid domain
* @param string $url URL you want to check
@ -16,7 +16,7 @@ function isValidDomain(string $url): bool {
return count($host_split) === 3 && in_array($host_split[1] . '.' . $host_split[2], VALID_TIKTOK_DOMAINS);
}
Route::add('/stream', function () {
$router->get('/stream', function () {
if (!isset($_GET['url'])) {
die('You need to send a url!');
}

View file

@ -1,33 +1,15 @@
<?php
/**@var Bramus\Router\Router $router */
use Helpers\Following;
use Helpers\Misc;
use Steampixel\Route;
use Views\Models\FollowingTemplate;
// Showing
Route::add('/following', function () {
$allowed_items_total = isset($_GET['max']) && is_numeric($_GET['max']) && $_GET['max'] <= 100 ? $_GET['max'] : 20;
$following = Following::get();
$items = [];
if (count($following) !== 0) {
$api = Misc::api();
$max_items_per_user = $allowed_items_total / count($following);
foreach ($following as $user) {
$user_feed = $api->getUserFeed($user);
if ($user_feed) {
$max = count($user_feed->items) > $max_items_per_user ? $max_items_per_user : count($user_feed->items);
for ($i = 0; $i < $max; $i++) {
$item = $user_feed->items[$i];
array_push($items, $item);
}
}
}
}
$feed = (object) [
'items' => $items,
'hasMore' => false
];
$router->get('/following', function () {
$users = Following::getUsers();
$feed = Following::getAll($users);
$latte = Misc::latte();
$latte->render(Misc::getView('following'), new FollowingTemplate($following, $feed));
$latte->render(Misc::getView('following'), new FollowingTemplate($users, $feed));
});

View file

@ -2,25 +2,27 @@
require __DIR__ . '/assets.php';
require __DIR__ . '/settings.php';
require __DIR__ . '/following.php';
require __DIR__ . '/rss.php';
/**@var Bramus\Router\Router $router */
use Steampixel\Route;
use Helpers\Misc;
use Helpers\Error;
use Helpers\ErrorHandler;
use Views\Models\BaseTemplate;
use Views\Models\FeedTemplate;
use Views\Models\ItemTemplate;
Route::add('/', function () {
$router->get('/', function () {
$latte = Misc::latte();
$latte->render(Misc::getView('home'), new BaseTemplate('Home'));
});
Route::add('/about', function () {
$router->get('/about', function () {
$latte = Misc::latte();
$latte->render(Misc::getView('about'), new BaseTemplate('About'));
});
Route::add("/trending", function () {
$router->get("/trending", function () {
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
@ -31,11 +33,11 @@ Route::add("/trending", function () {
$latte = Misc::latte();
$latte->render(Misc::getView('trending'), new FeedTemplate('Trending', $feed));
} else {
Error::show($feed->meta);
ErrorHandler::show($feed->meta);
}
});
Route::add("/@([^/]+)", function (string $username) {
$router->get("/@([^/]+)", function (string $username) {
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
@ -50,22 +52,22 @@ Route::add("/@([^/]+)", function (string $username) {
$latte = Misc::latte();
$latte->render(Misc::getView('user'), new FeedTemplate($feed->info->detail->user->nickname, $feed));
} else {
Error::show($feed->meta);
ErrorHandler::show($feed->meta);
}
});
Route::add('/video/([^/]+)', function (string $video_id) {
$router->get('/video/([^/]+)', function (string $video_id) {
$api = Misc::api();
$item = $api->getVideoByID($video_id);
if ($item->meta->success) {
$latte = Misc::latte();
$latte->render(Misc::getView('video'), new ItemTemplate($item->info->detail->user->nickname, $item));
} else {
Error::show($item->meta);
ErrorHandler::show($item->meta);
}
});
Route::add('/music/([^/]+)', function (string $music_id) {
$router->get('/music/([^/]+)', function (string $music_id) {
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
@ -77,11 +79,11 @@ Route::add('/music/([^/]+)', function (string $music_id) {
$latte = Misc::latte();
$latte->render(Misc::getView('music'), new FeedTemplate('Music', $feed));
} else {
Error::show($feed->meta);
ErrorHandler::show($feed->meta);
}
});
Route::add('/tag/(\w+)', function (string $name) {
$router->get('/tag/(\w+)', function (string $name) {
$cursor = 0;
if (isset($_GET['cursor']) && is_numeric($_GET['cursor'])) {
$cursor = (int) $_GET['cursor'];
@ -92,6 +94,6 @@ Route::add('/tag/(\w+)', function (string $name) {
$latte = Misc::latte();
$latte->render(Misc::getView('tag'), new FeedTemplate('Tag', $feed));
} else {
Error::show($feed->meta);
ErrorHandler::show($feed->meta);
}
});

46
routes/rss.php Normal file
View file

@ -0,0 +1,46 @@
<?php
/**@var Bramus\Router\Router $router */
use Helpers\Misc;
use Helpers\RSS;
use Helpers\ErrorHandler;
$router->all("/@([^/]+)/rss", function (string $username) {
$api = Misc::api();
$feed = $api->getUserFeed($username);
if ($feed->meta->success) {
$feed = RSS::build('/@'.$username, $feed->info->detail->user->nickname, $feed->info->detail->user->signature, $feed->items);
// Setup headers
RSS::setHeaders('user.rss');
echo $feed;
} else {
ErrorHandler::show($feed->meta);
}
});
$router->all("/trending/rss", function () {
$api = Misc::api();
$feed = $api->getTrendingFeed();
if ($feed->meta->success) {
$feed = RSS::build('/trending', 'Trending', 'Tiktok trending', $feed->items);
// Setup headers
RSS::setHeaders('trending.rss');
echo $feed;
} else {
ErrorHandler::show($feed->meta);
}
});
$router->all("/tag/(\w+)/rss", function (string $name) {
$api = Misc::api();
$feed = $api->getChallengeFeed($name);
if ($feed->meta->success) {
$feed = RSS::build("/tag/{$name}", "{$name} Tag", $feed->info->detail->challenge->desc, $feed->items);
// Setup headers
RSS::setHeaders('tag.rss');
echo $feed;
} else {
ErrorHandler::show($feed->meta);
}
});

View file

@ -1,52 +1,54 @@
<?php
/**@var Bramus\Router\Router $router */
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'), new SettingsTemplate());
});
$router->mount('/settings', function () use ($router) {
$router->get('/', function () {
$latte = Misc::latte();
$latte->render(Misc::getView('settings'), new SettingsTemplate());
});
Route::add("/settings/proxy", function () {
if (in_array(Settings::PROXY, $_POST)) {
foreach (Settings::PROXY as $proxy_element) {
Settings::set($proxy_element, $_POST[$proxy_element]);
}
}
http_response_code(302);
header('Location: ./home');
}, 'POST');
Route::add("/settings/following", function () {
$following = Following::get();
if (!isset($_POST['mode']) || empty($_POST['mode'])) {
die('You need to send a mode');
}
switch ($_POST['mode']) {
case 'add':
// Add following
array_push($following, $_POST['account']);
break;
case 'remove':
// Remove following
$index = array_search($_POST['account'], $following);
if ($index !== false) {
unset($following[$index]);
$router->post('/proxy', function () {
if (in_array(Settings::PROXY, $_POST)) {
foreach (Settings::PROXY as $proxy_element) {
Settings::set($proxy_element, $_POST[$proxy_element]);
}
break;
default:
// Invalid
die('Invalid mode');
}
}
http_response_code(302);
header('Location: ./home');
});
// Build string
$following_string = implode(',', $following);
Settings::set('following', $following_string);
header('Location: ../settings');
}, 'POST');
$router->post('/following', function () {
$following = Following::getUsers();
if (!isset($_POST['mode']) || empty($_POST['mode'])) {
die('You need to send a mode');
}
switch ($_POST['mode']) {
case 'add':
// Add following
array_push($following, $_POST['account']);
break;
case 'remove':
// Remove following
$index = array_search($_POST['account'], $following);
if ($index !== false) {
unset($following[$index]);
}
break;
default:
// Invalid
die('Invalid mode');
}
// Build string
$following_string = implode(',', $following);
Settings::set('following', $following_string);
header('Location: ../settings');
});
});