Files
torsearch/src/Torrentio/Result/ResultFactory.php

131 lines
3.7 KiB
PHP

<?php
namespace App\Torrentio\Result;
use App\Base\Util\CountryLanguages;
use Nihilarr\PTN;
class ResultFactory
{
public static $codecMap = [
'h264' => 'h264',
'h265' => 'h265',
'x264' => 'h264',
'x265' => 'h265',
'-' => '-'
];
public static function map(
string $url,
string $title,
string $bingeGroup = "-"
) {
$ptn = (object) (new PTN())->parse($title);
return new TorrentioResult(
self::trimTitle($title),
urldecode($url),
self::setFilename($url),
self::setSize($title),
self::setSeeders($title),
self::setProvider($title),
self::setEpisode($title),
$ptn->season ?? "-",
$bingeGroup,
$ptn->resolution ?? "-",
self::setCodec($ptn->codec ?? "-"),
$ptn->quality ?? "-",
$ptn,
substr(base64_encode($url), strlen($url) - 10),
$ptn->episode ?? "-",
self::setLanguages($title),
self::setLanguageFlags($title),
false,
uniqid()
);
}
public static function setFilename(string $url)
{
$file = explode("/", urldecode($url));
return end($file);
}
public static function setSize(string $title): string
{
$sizeMatch = [];
preg_match('/(\d+\.?\d+ )(GB|MB)/', $title, $sizeMatch);
return $sizeMatch[0] ?? "-";
}
private static function setSeeders(string $title): string
{
$emoji = \Emoji\detect_emoji($title);
return intval(
grapheme_substr($title, $emoji[0]['grapheme_offset'] + 1, $emoji[1]['grapheme_offset'] - $emoji[0]['grapheme_offset'])
);
}
private static function setProvider(string $title): string
{
$emoji = \Emoji\detect_emoji($title);
$provider = trim(
grapheme_substr($title, $emoji[2]['grapheme_offset'] + 1, strlen($title) - $emoji[1]['grapheme_offset'])
);
$providerParts = explode("\n", $provider);
return $providerParts[0];
}
private static function setLanguageFlags(string $title): string
{
$emoji = \Emoji\detect_emoji($title);
$provider = trim(
grapheme_substr($title, $emoji[2]['grapheme_offset'] + 1, strlen($title) - $emoji[1]['grapheme_offset'])
);
$providerParts = explode("\n", $provider);
if (array_key_exists(1, $providerParts)) {
return $providerParts[1];
} else {
return "&#x1f1fa;&#x1f1f8;";
}
}
public static function setLanguages(string $title): array
{
$emoji = \Emoji\detect_emoji($title);
$flags = array_filter($emoji, function ($emoji) {
return str_starts_with($emoji['short_name'], 'flag-');
});
$languages = array_map(function ($flag) {
return CountryLanguages::fromCountryCode(strtoupper(substr($flag['short_name'], strlen('flag-'))));
},
$flags);
if (count($languages) > 0) {
return array_values($languages);
} else {
return ["English (US)"];
}
}
public static function setCodec(string $codec): string
{
return self::$codecMap[strtolower($codec)] ?? $codec;
}
private static function setEpisode(string $title)
{
$value = [];
preg_match('/[sS]\d\d[eE]\d\d/', $title, $value);
return array_key_exists(0, $value) ? strtoupper($value[0]) : "n/a";
}
private static function trimTitle(string $title)
{
$emoji = \Emoji\detect_emoji($title);
return trim(grapheme_substr($title, 0, $emoji[0]['grapheme_offset']));
}
}