Accept TikTok URLs as search terms

On the home page, allow TikTok URLs to be entered as search terms for
the new category "TikTok URL", and redirect to a ProxiTok-compatible
URL when a valid one is entered.

Closes #58.
This commit is contained in:
Chris Novakovic 2022-07-27 00:49:14 +01:00
parent d0057e77e3
commit e7ee03e3cc
2 changed files with 38 additions and 0 deletions

View file

@ -1,5 +1,6 @@
<?php <?php
namespace App\Controllers; namespace App\Controllers;
use App\Helpers\ErrorHandler;
use App\Helpers\Misc; use App\Helpers\Misc;
/** /**
@ -11,6 +12,13 @@ class RedirectController {
if (isset($_GET['type'], $_GET['term'])) { if (isset($_GET['type'], $_GET['term'])) {
$term = trim($_GET['term']); $term = trim($_GET['term']);
switch ($_GET['type']) { switch ($_GET['type']) {
case 'url':
$endpoint = to_endpoint($term);
if (!$endpoint) {
ErrorHandler::show('Invalid or unknown TikTok URL format');
return;
}
break;
case 'user': case 'user':
// Remove @ if sent // Remove @ if sent
if ($term[0] === '@') { if ($term[0] === '@') {
@ -39,4 +47,33 @@ class RedirectController {
$url = Misc::url($endpoint); $url = Misc::url($endpoint);
header("Location: {$url}"); header("Location: {$url}");
} }
/**
* to_endpoint maps a TikTok URL into a ProxiTok-compatible endpoint URL.
*/
static private function to_endpoint($url) {
if (preg_match('%^https://vm\.tiktok\.com/([A-Za-z0-9]+)%', $url, $m)) {
// Short video URL
return '/@placeholder/video/' . $m[1];
} elseif (preg_match('%^https://www\.tiktok\.com/(.+)%', $url, $m)) {
// Username component (which may indicate a user profile URL or a video URL)
if (preg_match('%^(@[A-Za-z0-9_.]+)(?:/|$)(.*)%', $m[1], $u)) {
if ($u[2] == '') {
// User profile URL
return '/' . $u[1];
} elseif (preg_match('%^video/(\d+)%', $u[2], $v)) {
// Video URL
return '/' . $u[1] . '/video/' . $v[1];
}
} elseif (preg_match('%^tag/([^ ]+?)(?:/|$)%', $m[1], $t)) {
// Tag URL
return '/tag/' . $t[1];
} elseif (preg_match('%^music/([^ ]+?)(?:/|$)%', $m[1], $m)) {
// Music URL
return '/music/' . $m[1];
}
}
return '';
}
} }

View file

@ -12,6 +12,7 @@
<div class="control"> <div class="control">
<div class="select"> <div class="select">
<select name="type"> <select name="type">
<option value="url">TikTok URL</option>
<option value="user">Username</option> <option value="user">Username</option>
<option value="tag">Tag</option> <option value="tag">Tag</option>
<option value="music">Music ID</option> <option value="music">Music ID</option>