feat: media result page
This commit is contained in:
107
src/Torrentio/Result/ResultFactory.php
Normal file
107
src/Torrentio/Result/ResultFactory.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Torrentio\Result;
|
||||
|
||||
use App\Util\CountryCodes;
|
||||
use Nihilarr\PTN;
|
||||
|
||||
class ResultFactory
|
||||
{
|
||||
public static function map(
|
||||
string $url,
|
||||
string $title,
|
||||
string $bingeGroup = "-"
|
||||
) {
|
||||
$ptn = (object) (new PTN())->parse($title);
|
||||
return new TorrentioResult(
|
||||
self::trimTitle($title),
|
||||
$url,
|
||||
self::setSize($title),
|
||||
self::setSeeders($title),
|
||||
self::setProvider($title),
|
||||
self::setEpisode($title),
|
||||
$ptn->season ?? "-",
|
||||
$bingeGroup,
|
||||
$ptn->resolution ?? "-",
|
||||
$ptn->codec ?? "-",
|
||||
$ptn,
|
||||
substr(base64_encode($url), strlen($url) - 10),
|
||||
$ptn->episode ?? "-",
|
||||
self::setLanguages($title),
|
||||
self::setLanguageFlags($title),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
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 "🇺🇸";
|
||||
}
|
||||
}
|
||||
|
||||
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 CountryCodes::convertFromAbbr(strtoupper(substr($flag['short_name'], strlen('flag-'))));
|
||||
},
|
||||
$flags);
|
||||
if (count($languages) > 0) {
|
||||
return array_values($languages);
|
||||
} else {
|
||||
return ["English"];
|
||||
}
|
||||
}
|
||||
|
||||
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']));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user