Download services, scraper bump

This commit is contained in:
Pablo Ferreiro 2022-09-25 19:53:00 +02:00
parent c076ba65a6
commit 7aa869f567
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
27 changed files with 191 additions and 73 deletions

View file

@ -1,7 +1,7 @@
<?php
namespace App\Cache;
use TikScraper\CacheInterface;
use TikScraper\Interfaces\CacheInterface;
class JSONCache implements CacheInterface {
private string $cache_path = __DIR__ . '/../../cache/api';

View file

@ -1,7 +1,7 @@
<?php
namespace App\Cache;
use TikScraper\CacheInterface;
use TikScraper\Interfaces\CacheInterface;
class RedisCache implements CacheInterface {
private \Redis $client;

7
app/Constants/Themes.php Normal file
View file

@ -0,0 +1,7 @@
<?php
namespace App\Constants;
abstract class Themes {
const DEFAULT = "default";
const CARD = "card";
}

View file

@ -1,6 +1,9 @@
<?php
namespace App\Controllers;
use App\Helpers\Cookies;
use TikScraper\Helpers\Converter;
class ProxyController {
const VALID_TIKTOK_DOMAINS = [
"tiktokcdn.com", "tiktokcdn-us.com", "tiktok.com"
@ -30,11 +33,9 @@ class ProxyController {
}
static private function getFileName(): string {
$filename = 'tiktok-video';
if (isset($_GET['user'])) {
$filename .= '-' . $_GET['user'] . '-' . $_GET['id'];
}
static private function getFilename(string $url, string $user): string {
$id = Converter::urlToId($url);
$filename = 'tiktok-video-' . $id . '-' . $user;
return $filename;
}
@ -46,18 +47,17 @@ class ProxyController {
}
static public function download() {
$downloader = new \TikScraper\Download();
self::checkUrl();
$method = Cookies::downloader();
$downloader = new \TikScraper\Download($method);
// Params
$watermark = isset($_GET['watermark']);
if ($watermark) {
self::checkUrl();
$filename = self::getFileName();
$downloader->url($_GET['url'], $filename, true);
} else {
if (!isset($_GET['id'])) {
die('You need to send an ID!');
}
$filename = self::getFileName();
$downloader->url($_GET['id'], $filename, false);
}
$url = $_GET['url'];
$user = $_GET['user'] ?? '';
// Filename
$filename = self::getFilename($url, $user);
// Running
$downloader->url($_GET['url'], $filename, $watermark);
}
}

View file

@ -10,7 +10,7 @@ class RedirectController {
static public function redirect() {
$endpoint = '/';
if (isset($_GET['type'], $_GET['term'])) {
$term = trim($_GET['term']);
$term = urlencode(trim($_GET['term']));
switch ($_GET['type']) {
case 'url':
$endpoint = self::to_endpoint($term);

View file

@ -4,12 +4,12 @@ namespace App\Controllers;
use App\Helpers\Misc;
use App\Helpers\Cookies;
use App\Helpers\Wrappers;
use App\Models\BaseTemplate;
use App\Models\SettingsTemplate;
class SettingsController {
static public function index() {
$latte = Wrappers::latte();
$latte->render(Misc::getView('settings'), new BaseTemplate('Settings'));
$latte->render(Misc::getView('settings'), new SettingsTemplate());
}
static public function general() {
@ -25,6 +25,11 @@ class SettingsController {
$test_endpoints = $_POST['api-test_endpoints'];
Cookies::set('api-test_endpoints', $test_endpoints);
}
if (isset($_POST['api-downloader'])) {
$downloader = $_POST['api-downloader'];
Cookies::set("api-downloader", $downloader);
}
self::redirect();
}

View file

@ -16,7 +16,7 @@ class TagController {
if ($hashtag->ok()) {
$data = $hashtag->getFull();
$latte = Wrappers::latte();
$latte->render(Misc::getView('tag'), new FullTemplate('Tag', $data));
$latte->render(Misc::getView('tag'), new FullTemplate($data->info->detail->title, $data));
} else {
ErrorHandler::showMeta($hashtag->error());
}

View file

@ -1,9 +1,9 @@
<?php
namespace App\Helpers;
class Cookies {
const ALLOWED_THEMES = ['default', 'card'];
use App\Constants\Themes;
class Cookies {
static public function get(string $name, string $default_value = ''): string {
if (isset($_COOKIE[$name]) && !empty($_COOKIE[$name])) {
return $_COOKIE[$name];
@ -13,12 +13,19 @@ class Cookies {
static public function theme(): string {
$theme = self::get('theme');
if ($theme && in_array($theme, self::ALLOWED_THEMES)) {
$ref = new \ReflectionClass(Themes::class);
$themes = $ref->getConstants();
if ($theme && in_array($theme, $themes)) {
return $theme;
}
return 'default';
}
static public function downloader(): string {
$downloader = self::get('api-downloader', 'default');
return $downloader;
}
static public function exists(string $name): bool {
return isset($_COOKIE[$name]);
}

View file

@ -0,0 +1,27 @@
<?php
namespace App\Helpers;
class UrlBuilder {
static public function stream(string $url): string {
return Misc::url('/stream?url=' . urlencode($url));
}
static public function download(string $url, string $username, bool $watermark): string {
// {path('/download?url=' . urlencode($playAddr) . '&id=' . $id . '&user=' . $uniqueId) . '&watermark=1'}
$down_url = Misc::url('/download?url=' . urlencode($url) . '&user=' . $username);
if ($watermark) $down_url .= '&watermark=1';
return $down_url;
}
static public function user(string $username): string {
return Misc::url('/@' . $username);
}
static public function video_internal(string $username, string $id): string {
return Misc::url('/@' . $username . "/video/" . $id);
}
static public function video_external(string $username, string $id): string {
return "https://www.tiktok.com/@" . $username . "/video/" . $id;
}
}

View file

@ -29,6 +29,23 @@ class Wrappers {
$latte->addFunction('theme', function(): string {
return Cookies::theme();
});
// UrlBuilder
$latte->addFunction('url_stream', function (string $url): string {
return UrlBuilder::stream($url);
});
$latte->addFunction('url_user', function (string $username): string {
return UrlBuilder::user($username);
});
$latte->addFunction('url_video_internal', function (string $username, string $id): string {
return UrlBuilder::video_internal($username, $id);
});
$latte->addFunction('url_video_external', function (string $username, string $id): string {
return UrlBuilder::video_external($username, $id);
});
$latte->addFunction('url_download', function (string $url, string $username, bool $watermark): string {
return UrlBuilder::download($url, $username, $watermark);
});
// https://stackoverflow.com/a/36365553
$latte->addFunction('number', function (float $x) {
if($x > 1000) {

View file

@ -0,0 +1,31 @@
<?php
namespace App\Models;
use App\Constants\Themes;
use App\Helpers\Cookies;
use TikScraper\Constants\DownloadMethods;
/**
* Base for templates with a feed
*/
class SettingsTemplate extends BaseTemplate {
public array $downloaders = [];
public array $themes = [];
public bool $isTestEndpoints = false;
public string $currentDownloader;
public string $currentTheme;
function __construct() {
parent::__construct("Settings");
// Downloaders list
$ref = new \ReflectionClass(DownloadMethods::class);
$this->downloaders = $ref->getConstants();
// Themes list
$ref = new \ReflectionClass(Themes::class);
$this->themes = $ref->getConstants();
// Cookies data
$this->isTestEndpoints = Cookies::check('api-test_endpoints', 'yes');
$this->currentDownloader = Cookies::downloader();
$this->currentTheme = Cookies::theme();
}
}