mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 01:08:56 +00:00
315 lines
11 KiB
PHP
315 lines
11 KiB
PHP
<?php
|
||
|
||
use newznab\db\Settings;
|
||
|
||
/**
|
||
* Class TV
|
||
*/
|
||
class TV
|
||
{
|
||
const MATCH_PROBABILITY = 75;
|
||
/**
|
||
* @var \newznab\db\Settings
|
||
*/
|
||
public $pdo;
|
||
|
||
/**
|
||
* @var array|bool|int|string
|
||
*/
|
||
public $catWhere;
|
||
|
||
/**
|
||
* @var bool
|
||
*/
|
||
public $echooutput;
|
||
|
||
/**
|
||
* @param array $options Class instances / Echo to CLI.
|
||
*/
|
||
public function __construct(array $options = [])
|
||
{
|
||
$defaults = [
|
||
'Echo' => false,
|
||
'Settings' => null,
|
||
];
|
||
$options += $defaults;
|
||
$this->pdo = ($options['Settings'] instanceof Settings ? $options['Settings'] : new Settings());
|
||
$this->echooutput = ($options['Echo'] && NN_ECHOCLI);
|
||
$this->catWhere = 'categoryid BETWEEN 5000 AND 5999';
|
||
}
|
||
|
||
public function cleanName($str)
|
||
{
|
||
$str = str_replace(['.', '_'], ' ', $str);
|
||
|
||
$str = str_replace(['à', 'á', 'â', 'ã', 'ä', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä'], 'a', $str);
|
||
$str = str_replace(['ç', 'Ç'], 'c', $str);
|
||
$str = str_replace(['Σ', 'è', 'é', 'ê', 'ë', 'È', 'É', 'Ê', 'Ë'], 'e', $str);
|
||
$str = str_replace(['ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï'], 'i', $str);
|
||
$str = str_replace(['ò', 'ó', 'ô', 'õ', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö'], 'o', $str);
|
||
$str = str_replace(['ù', 'ú', 'û', 'ü', 'ū', 'Ú', 'Û', 'Ü', 'Ū'], 'u', $str);
|
||
$str = str_replace('ß', 'ss', $str);
|
||
|
||
$str = str_replace('&', 'and', $str);
|
||
$str = preg_replace('/^(history|discovery) channel/i', '', $str);
|
||
$str = str_replace(['\'', ':', '!', '"', '#', '*', '’', ',', '(', ')', '?'], '', $str);
|
||
$str = str_replace('$', 's', $str);
|
||
$str = preg_replace('/\s{2,}/', ' ', $str);
|
||
|
||
$str = trim($str, '\"');
|
||
return trim($str);
|
||
}
|
||
|
||
public function parseNameEpSeason($relname)
|
||
{
|
||
$showInfo = ['name' => '', 'season' => '', 'episode' => '', 'seriesfull' => '', 'airdate' => '', 'country' => '', 'year' => '', 'cleanname' => ''];
|
||
$matches = '';
|
||
|
||
$following = '[^a-z0-9](\d\d-\d\d|\d{1,2}x\d{2,3}|(19|20)\d\d|(480|720|1080)[ip]|AAC2?|BDRip|BluRay|D0?\d' .
|
||
'|DD5|DiVX|DLMux|DTS|DVD(Rip)?|E\d{2,3}|[HX][-_. ]?264|ITA(-ENG)?|[HPS]DTV|PROPER|REPACK|' .
|
||
'S\d+[^a-z0-9]?(E\d+)?|WEB[-_. ]?(DL|Rip)|XViD)[^a-z0-9]';
|
||
|
||
// For names that don't start with the title.
|
||
if (preg_match('/[^a-z0-9]{2,}(?P<name>[\w .-]*?)' . $following . '/i', $relname, $matches)) {
|
||
$showInfo['name'] = $matches[1];
|
||
} else if (preg_match('/^(?P<name>[a-z0-9][\w .-]*?)' . $following . '/i', $relname, $matches)) {
|
||
// For names that start with the title.
|
||
$showInfo['name'] = $matches[1];
|
||
}
|
||
|
||
if (!empty($showInfo['name'])) {
|
||
// S01E01-E02 and S01E01-02
|
||
if (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})(?:[e-])(\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = [intval($matches[3]), intval($matches[4])];
|
||
}
|
||
//S01E0102 - lame no delimit numbering, regex would collide if there was ever 1000 ep season.
|
||
else if (preg_match('/^(.*?)[^a-z0-9]s(\d{2})[^a-z0-9]?e(\d{2})(\d{2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = [intval($matches[3]), intval($matches[4])];
|
||
}
|
||
// S01E01 and S01.E01
|
||
else if (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = intval($matches[3]);
|
||
}
|
||
// S01
|
||
else if (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = 'all';
|
||
}
|
||
// S01D1 and S1D1
|
||
else if (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?d\d{1}[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = 'all';
|
||
}
|
||
// 1x01
|
||
else if (preg_match('/^(.*?)[^a-z0-9](\d{1,2})x(\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = intval($matches[3]);
|
||
}
|
||
// 2009.01.01 and 2009-01-01
|
||
else if (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = $matches[2] . $matches[3];
|
||
$showInfo['episode'] = $matches[4] . '/' . $matches[5];
|
||
$showInfo['airdate'] = $matches[2] . $matches[3] . '-' . $matches[4] . '-' . $matches[5]; //yy-m-d
|
||
}
|
||
// 01.01.2009
|
||
else if (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](19|20)(\d{2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = $matches[4] . $matches[5];
|
||
$showInfo['episode'] = $matches[2] . '/' . $matches[3];
|
||
$showInfo['airdate'] = $matches[4] . $matches[5] . '-' . $matches[2] . '-' . $matches[3]; //yy-m-d
|
||
}
|
||
// 01.01.09
|
||
else if (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = ($matches[4] <= 99 && $matches[4] > 15) ? '19' . $matches[4] : '20' . $matches[4];
|
||
$showInfo['episode'] = $matches[2] . '/' . $matches[3];
|
||
$showInfo['airdate'] = $showInfo['season'] . '-' . $matches[2] . '-' . $matches[3]; //yy-m-d
|
||
}
|
||
// 2009.E01
|
||
else if (preg_match('/^(.*?)[^a-z0-9]20(\d{2})[^a-z0-9](\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = '20' . $matches[2];
|
||
$showInfo['episode'] = intval($matches[3]);
|
||
}
|
||
// 2009.Part1
|
||
else if (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9]Part(\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = $matches[2] . $matches[3];
|
||
$showInfo['episode'] = intval($matches[4]);
|
||
}
|
||
// Part1/Pt1
|
||
else if (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9](\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
|
||
$showInfo['season'] = 1;
|
||
$showInfo['episode'] = intval($matches[2]);
|
||
}
|
||
//The.Pacific.Pt.VI.HDTV.XviD-XII / Part.IV
|
||
else if (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9]([ivx]+)/i', $relname, $matches)) {
|
||
$showInfo['season'] = 1;
|
||
$epLow = strtolower($matches[2]);
|
||
switch ($epLow) {
|
||
case 'i': $e = 1;
|
||
break;
|
||
case 'ii': $e = 2;
|
||
break;
|
||
case 'iii': $e = 3;
|
||
break;
|
||
case 'iv': $e = 4;
|
||
break;
|
||
case 'v': $e = 5;
|
||
break;
|
||
case 'vi': $e = 6;
|
||
break;
|
||
case 'vii': $e = 7;
|
||
break;
|
||
case 'viii': $e = 8;
|
||
break;
|
||
case 'ix': $e = 9;
|
||
break;
|
||
case 'x': $e = 10;
|
||
break;
|
||
case 'xi': $e = 11;
|
||
break;
|
||
case 'xii': $e = 12;
|
||
break;
|
||
case 'xiii': $e = 13;
|
||
break;
|
||
case 'xiv': $e = 14;
|
||
break;
|
||
case 'xv': $e = 15;
|
||
break;
|
||
case 'xvi': $e = 16;
|
||
break;
|
||
case 'xvii': $e = 17;
|
||
break;
|
||
case 'xviii': $e = 18;
|
||
break;
|
||
case 'xix': $e = 19;
|
||
break;
|
||
case 'xx': $e = 20;
|
||
break;
|
||
default:
|
||
$e = 0;
|
||
}
|
||
$showInfo['episode'] = $e;
|
||
}
|
||
// Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY
|
||
else if (preg_match('/^(.*?)[^a-z0-9]EP?[^a-z0-9]?(\d{1,3})/i', $relname, $matches)) {
|
||
$showInfo['season'] = 1;
|
||
$showInfo['episode'] = intval($matches[2]);
|
||
}
|
||
// Season.1
|
||
else if (preg_match('/^(.*?)[^a-z0-9]Seasons?[^a-z0-9]?(\d{1,2})/i', $relname, $matches)) {
|
||
$showInfo['season'] = intval($matches[2]);
|
||
$showInfo['episode'] = 'all';
|
||
}
|
||
|
||
$countryMatch = $yearMatch = '';
|
||
// Country or origin matching.
|
||
if (preg_match('/\W(US|UK|AU|NZ|CA|NL|Canada|Australia|America|United[^a-z0-9]States|United[^a-z0-9]Kingdom)\W/', $showInfo['name'], $countryMatch)) {
|
||
$currentCountry = strtolower($countryMatch[1]);
|
||
if ($currentCountry == 'canada') {
|
||
$showInfo['country'] = 'CA';
|
||
} else if ($currentCountry == 'australia') {
|
||
$showInfo['country'] = 'AU';
|
||
} else if ($currentCountry == 'america' || $currentCountry == 'united states') {
|
||
$showInfo['country'] = 'US';
|
||
} else if ($currentCountry == 'united kingdom') {
|
||
$showInfo['country'] = 'UK';
|
||
} else {
|
||
$showInfo['country'] = strtoupper($countryMatch[1]);
|
||
}
|
||
}
|
||
|
||
// Clean show name.
|
||
$showInfo['cleanname'] = preg_replace('/ - \d{1,}$/i', '', $this->cleanName($showInfo['name']));
|
||
|
||
// Check for dates instead of seasons.
|
||
if (strlen($showInfo['season']) == 4) {
|
||
$showInfo['seriesfull'] = $showInfo['season'] . "/" . $showInfo['episode'];
|
||
} else {
|
||
// Get year if present (not for releases with dates as seasons).
|
||
if (preg_match('/[^a-z0-9](19|20)(\d{2})/i', $relname, $yearMatch)) {
|
||
$showInfo['year'] = $yearMatch[1] . $yearMatch[2];
|
||
}
|
||
|
||
$showInfo['season'] = sprintf('S%02d', $showInfo['season']);
|
||
// Check for multi episode release.
|
||
if (is_array($showInfo['episode'])) {
|
||
$tmpArr = [];
|
||
foreach ($showInfo['episode'] as $ep) {
|
||
$tmpArr[] = sprintf('E%02d', $ep);
|
||
}
|
||
$showInfo['episode'] = implode('', $tmpArr);
|
||
} else {
|
||
$showInfo['episode'] = sprintf('E%02d', $showInfo['episode']);
|
||
}
|
||
|
||
$showInfo['seriesfull'] = $showInfo['season'] . $showInfo['episode'];
|
||
}
|
||
$showInfo['airdate'] = (!empty($showInfo['airdate'])) ? $showInfo['airdate'] . ' 00:00:00' : '';
|
||
return $showInfo;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public function checkMatch($ourName, $tvrName)
|
||
{
|
||
// Clean up name ($ourName is already clean).
|
||
$tvrName = $this->cleanName($tvrName);
|
||
$tvrName = preg_replace('/ of /i', '', $tvrName);
|
||
$ourName = preg_replace('/ of /i', '', $ourName);
|
||
|
||
// Create our arrays.
|
||
$ourArr = explode(' ', $ourName);
|
||
$tvrArr = explode(' ', $tvrName);
|
||
|
||
// Set our match counts.
|
||
$numMatches = 0;
|
||
$totalMatches = sizeof($ourArr) + sizeof($tvrArr);
|
||
|
||
// Loop through each array matching again the opposite value, if they match increment!
|
||
foreach ($ourArr as $oname) {
|
||
if (preg_match('/ ' . preg_quote($oname, '/') . ' /i', ' ' . $tvrName . ' ')) {
|
||
$numMatches++;
|
||
}
|
||
}
|
||
foreach ($tvrArr as $tname) {
|
||
if (preg_match('/ ' . preg_quote($tname, '/') . ' /i', ' ' . $ourName . ' ')) {
|
||
$numMatches++;
|
||
}
|
||
}
|
||
|
||
// Check what we're left with.
|
||
if ($numMatches <= 0) {
|
||
return false;
|
||
} else {
|
||
$matchpct = ($numMatches / $totalMatches) * 100;
|
||
}
|
||
|
||
if ($matchpct >= TV::MATCH_PROBABILITY) {
|
||
return $matchpct;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public function getGenres()
|
||
{
|
||
return [
|
||
'Action', 'Adult/Porn', 'Adventure', 'Anthology',
|
||
'Arts & Crafts', 'Automobiles', 'Buy, Sell & Trade',
|
||
'Celebrities', 'Children', 'Cinema/Theatre', 'Comedy',
|
||
'Cooking/Food', 'Crime', 'Current Events', 'Dance',
|
||
'Debate', 'Design/Decorating', 'Discovery/Science', 'Drama',
|
||
'Educational', 'Family', 'Fantasy', 'Fashion/Make-up',
|
||
'Financial/Business', 'Fitness', 'Garden/Landscape', 'History',
|
||
'Horror/Supernatural', 'Housing/Building', 'How To/Do It Yourself', 'Interview',
|
||
'Lifestyle', 'Literature', 'Medical', 'Military/War',
|
||
'Music', 'Mystery', 'Pets/Animals', 'Politics',
|
||
'Puppets', 'Religion', 'Romance/Dating', 'Sci-Fi',
|
||
'Sketch/Improv', 'Soaps', 'Sports', 'Super Heroes',
|
||
'Talent', 'Tech/Gaming', 'Teens', 'Thriller',
|
||
'Travel', 'Western', 'Wildlife'
|
||
];
|
||
}
|
||
}
|