| Total Complexity | 109 |
| Total Lines | 759 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractTvProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractTvProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class AbstractTvProvider extends BaseVideoProvider |
||
| 20 | { |
||
| 21 | // Television Sources |
||
| 22 | protected const SOURCE_NONE = 0; // No Scrape source |
||
| 23 | |||
| 24 | protected const SOURCE_TVDB = 1; // Scrape source was TVDB |
||
| 25 | |||
| 26 | protected const SOURCE_TVMAZE = 2; // Scrape source was TVMAZE |
||
| 27 | |||
| 28 | protected const SOURCE_TMDB = 3; // Scrape source was TMDB |
||
| 29 | |||
| 30 | protected const SOURCE_TRAKT = 4; // Scrape source was Trakt |
||
| 31 | |||
| 32 | protected const SOURCE_IMDB = 5; // Scrape source was IMDB |
||
| 33 | |||
| 34 | // Anime Sources |
||
| 35 | protected const SOURCE_ANIDB = 10; // Scrape source was AniDB |
||
| 36 | |||
| 37 | // Processing signifiers |
||
| 38 | protected const PROCESS_TVDB = 0; // Process TVDB First |
||
| 39 | |||
| 40 | protected const PROCESS_TVMAZE = -1; // Process TVMaze Second |
||
| 41 | |||
| 42 | protected const PROCESS_TMDB = -2; // Process TMDB Third |
||
| 43 | |||
| 44 | protected const PROCESS_TRAKT = -3; // Process Trakt Fourth |
||
| 45 | |||
| 46 | protected const PROCESS_IMDB = -4; // Process IMDB Fifth |
||
| 47 | |||
| 48 | protected const NO_MATCH_FOUND = -6; // Failed All Methods |
||
| 49 | |||
| 50 | protected const FAILED_PARSE = -100; // Failed Parsing |
||
| 51 | |||
| 52 | public int $tvqty; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @string Path to Save Images |
||
| 56 | */ |
||
| 57 | public string $imgSavePath; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array Site ID columns for TV |
||
| 61 | */ |
||
| 62 | public array $siteColumns; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string The TV categories_id lookup SQL language |
||
| 66 | */ |
||
| 67 | public string $catWhere; |
||
| 68 | |||
| 69 | protected ColorCLI $colorCli; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * AbstractTvProvider constructor. |
||
| 73 | * |
||
| 74 | * @throws \Exception |
||
| 75 | */ |
||
| 76 | public function __construct() |
||
| 77 | { |
||
| 78 | parent::__construct(); |
||
| 79 | $this->colorCli = new ColorCLI; |
||
| 80 | $this->catWhere = 'categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER.' AND categories_id != '.Category::TV_ANIME; |
||
| 81 | $this->tvqty = Settings::settingValue('maxrageprocessed') !== '' ? (int) Settings::settingValue('maxrageprocessed') : 75; |
||
| 82 | $this->imgSavePath = storage_path('covers/tvshows/'); |
||
| 83 | $this->siteColumns = ['tvdb', 'trakt', 'tvrage', 'tvmaze', 'imdb', 'tmdb']; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Retrieve banner image from site using its API. |
||
| 88 | */ |
||
| 89 | abstract public function getBanner(int $videoID, int $siteId): mixed; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Retrieve info of TV episode from site using its API. |
||
| 93 | * |
||
| 94 | * @return array|false False on failure, an array of information fields otherwise. |
||
| 95 | */ |
||
| 96 | abstract public function getEpisodeInfo(int|string $siteId, int|string $series, int|string $episode): array|bool; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve poster image for TV episode from site using its API. |
||
| 100 | * |
||
| 101 | * @param int $videoId ID from videos table. |
||
| 102 | */ |
||
| 103 | abstract public function getPoster(int $videoId): int; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Retrieve info of TV programme from site using it's API. |
||
| 107 | * |
||
| 108 | * @param string $name Title of programme to look up. Usually a cleaned up version from releases table. |
||
| 109 | * @return array|false False on failure, an array of information fields otherwise. |
||
| 110 | */ |
||
| 111 | abstract public function getShowInfo(string $name): bool|array; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Assigns API show response values to a formatted array for insertion |
||
| 115 | * Returns the formatted array. |
||
| 116 | */ |
||
| 117 | abstract public function formatShowInfo($show): array; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Assigns API episode response values to a formatted array for insertion |
||
| 121 | * Returns the formatted array. |
||
| 122 | */ |
||
| 123 | abstract public function formatEpisodeInfo($episode): array; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return Release[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|int |
||
| 127 | */ |
||
| 128 | public function getTvReleases(string $groupID = '', string $guidChar = '', int $lookupSetting = 1, int $status = 0): array|\Illuminate\Database\Eloquent\Collection|int|\Illuminate\Support\Collection |
||
| 129 | { |
||
| 130 | $ret = 0; |
||
| 131 | if ($lookupSetting === 0) { |
||
| 132 | return $ret; |
||
| 133 | } |
||
| 134 | |||
| 135 | $qry = Release::query() |
||
| 136 | ->where(['videos_id' => 0, 'tv_episodes_id' => $status]) |
||
| 137 | ->where('size', '>', 1048576) |
||
| 138 | ->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER]) |
||
| 139 | ->where('categories_id', '<>', Category::TV_ANIME) |
||
| 140 | ->orderByDesc('postdate') |
||
|
|
|||
| 141 | ->limit($this->tvqty); |
||
| 142 | if ($groupID !== '') { |
||
| 143 | $qry->where('groups_id', $groupID); |
||
| 144 | } |
||
| 145 | if ($guidChar !== '') { |
||
| 146 | $qry->where('leftguid', $guidChar); |
||
| 147 | } |
||
| 148 | if ($lookupSetting === 2) { |
||
| 149 | $qry->where('isrenamed', '=', 1); |
||
| 150 | } |
||
| 151 | |||
| 152 | return $qry->get(); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function setVideoIdFound(int $videoId, int $releaseId, int $episodeId): void |
||
| 156 | { |
||
| 157 | Release::query() |
||
| 158 | ->where('id', $releaseId) |
||
| 159 | ->update(['videos_id' => $videoId, 'tv_episodes_id' => $episodeId]); |
||
| 160 | |||
| 161 | ReleaseBrowseService::bumpCacheVersion(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Updates the release tv_episodes_id status when scraper match is not found. |
||
| 166 | */ |
||
| 167 | public function setVideoNotFound($status, $Id): void |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Inserts a new video ID into the database for TV shows |
||
| 176 | * If a duplicate is found it is handle by calling update instead. |
||
| 177 | */ |
||
| 178 | public function add(array $show = []): int |
||
| 179 | { |
||
| 180 | $videoId = false; |
||
| 181 | |||
| 182 | // Check if the country is not a proper code and retrieve if not |
||
| 183 | if ($show['country'] !== '' && \strlen($show['country']) > 2) { |
||
| 184 | $show['country'] = countryCode($show['country']); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Check if video already exists based on site ID info |
||
| 188 | // if that fails be sure we're not inserting duplicates by checking the title |
||
| 189 | foreach ($this->siteColumns as $column) { |
||
| 190 | if ((int) $show[$column] > 0) { |
||
| 191 | $videoId = $this->getVideoIDFromSiteID($column, $show[$column]); |
||
| 192 | } |
||
| 193 | if ($videoId !== false) { |
||
| 194 | break; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($videoId === false) { |
||
| 199 | $title = Video::query()->where('title', $show['title'])->first(['title']); |
||
| 200 | if ($title === null) { |
||
| 201 | // Insert the Show |
||
| 202 | $videoId = Video::query()->insertGetId([ |
||
| 203 | 'type' => $show['type'], |
||
| 204 | 'title' => $show['title'], |
||
| 205 | 'countries_id' => $show['country'] ?? '', |
||
| 206 | 'started' => $show['started'], |
||
| 207 | 'source' => $show['source'], |
||
| 208 | 'tvdb' => $show['tvdb'], |
||
| 209 | 'trakt' => $show['trakt'], |
||
| 210 | 'tvrage' => $show['tvrage'], |
||
| 211 | 'tvmaze' => $show['tvmaze'], |
||
| 212 | 'imdb' => $show['imdb'], |
||
| 213 | 'tmdb' => $show['tmdb'], |
||
| 214 | ]); |
||
| 215 | // Insert the supplementary show info |
||
| 216 | TvInfo::query()->insertOrIgnore([ |
||
| 217 | 'videos_id' => $videoId, |
||
| 218 | 'summary' => $show['summary'], |
||
| 219 | 'publisher' => $show['publisher'], |
||
| 220 | 'localzone' => $show['localzone'], |
||
| 221 | ]); |
||
| 222 | // If we have AKAs\aliases, insert those as well |
||
| 223 | if (! empty($show['aliases'])) { |
||
| 224 | $this->addAliases($videoId, $show['aliases']); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | // If a local match was found, just update missing video info |
||
| 229 | $this->update($videoId, $show); |
||
| 230 | } |
||
| 231 | |||
| 232 | return $videoId; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function addEpisode(int $videoId, array $episode = []): bool|int |
||
| 236 | { |
||
| 237 | $episodeId = $this->getBySeasonEp($videoId, $episode['series'], $episode['episode'], $episode['firstaired']); |
||
| 238 | |||
| 239 | if ($episodeId === false) { |
||
| 240 | $episodeId = TvEpisode::query()->insertOrIgnore( |
||
| 241 | [ |
||
| 242 | 'videos_id' => $videoId, |
||
| 243 | 'series' => $episode['series'], |
||
| 244 | 'episode' => $episode['episode'], |
||
| 245 | 'se_complete' => $episode['se_complete'], |
||
| 246 | 'title' => $episode['title'], |
||
| 247 | 'firstaired' => $episode['firstaired'] !== '' ? $episode['firstaired'] : null, |
||
| 248 | 'summary' => $episode['summary'], |
||
| 249 | ] |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $episodeId; |
||
| 254 | } |
||
| 255 | |||
| 256 | public function update(int $videoId, array $show = []): void |
||
| 257 | { |
||
| 258 | if ($show['country'] !== '') { |
||
| 259 | $show['country'] = countryCode($show['country']); |
||
| 260 | } |
||
| 261 | |||
| 262 | $ifStringID = 'IF(%s = 0, %s, %s)'; |
||
| 263 | $ifStringInfo = "IF(%s = '', %s, %s)"; |
||
| 264 | |||
| 265 | DB::update( |
||
| 266 | sprintf( |
||
| 267 | ' |
||
| 268 | UPDATE videos v |
||
| 269 | LEFT JOIN tv_info tvi ON v.id = tvi.videos_id |
||
| 270 | SET v.countries_id = %s, v.tvdb = %s, v.trakt = %s, v.tvrage = %s, |
||
| 271 | v.tvmaze = %s, v.imdb = %s, v.tmdb = %s, |
||
| 272 | tvi.summary = %s, tvi.publisher = %s, tvi.localzone = %s |
||
| 273 | WHERE v.id = %d', |
||
| 274 | sprintf($ifStringInfo, 'v.countries_id', escapeString($show['country']), 'v.countries_id'), |
||
| 275 | sprintf($ifStringID, 'v.tvdb', $show['tvdb'], 'v.tvdb'), |
||
| 276 | sprintf($ifStringID, 'v.trakt', $show['trakt'], 'v.trakt'), |
||
| 277 | sprintf($ifStringID, 'v.tvrage', $show['tvrage'], 'v.tvrage'), |
||
| 278 | sprintf($ifStringID, 'v.tvmaze', $show['tvmaze'], 'v.tvmaze'), |
||
| 279 | sprintf($ifStringID, 'v.imdb', $show['imdb'], 'v.imdb'), |
||
| 280 | sprintf($ifStringID, 'v.tmdb', $show['tmdb'], 'v.tmdb'), |
||
| 281 | sprintf($ifStringInfo, 'tvi.summary', escapeString($show['summary']), 'tvi.summary'), |
||
| 282 | sprintf($ifStringInfo, 'tvi.publisher', escapeString($show['publisher']), 'tvi.publisher'), |
||
| 283 | sprintf($ifStringInfo, 'tvi.localzone', escapeString($show['localzone']), 'tvi.localzone'), |
||
| 284 | $videoId |
||
| 285 | ) |
||
| 286 | ); |
||
| 287 | if (! empty($show['aliases'])) { |
||
| 288 | $this->addAliases($videoId, $show['aliases']); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @throws \Throwable |
||
| 294 | */ |
||
| 295 | public function delete(int $id): mixed |
||
| 296 | { |
||
| 297 | return DB::transaction(function () use ($id) { |
||
| 298 | DB::delete( |
||
| 299 | sprintf( |
||
| 300 | ' |
||
| 301 | DELETE v, tvi, tve, va |
||
| 302 | FROM videos v |
||
| 303 | LEFT JOIN tv_info tvi ON v.id = tvi.videos_id |
||
| 304 | LEFT JOIN tv_episodes tve ON v.id = tve.videos_id |
||
| 305 | LEFT JOIN videos_aliases va ON v.id = va.videos_id |
||
| 306 | WHERE v.id = %d', |
||
| 307 | $id |
||
| 308 | ) |
||
| 309 | ); |
||
| 310 | }, 3); |
||
| 311 | } |
||
| 312 | |||
| 313 | public function setCoverFound(int $videoId): void |
||
| 314 | { |
||
| 315 | TvInfo::query()->where('videos_id', $videoId)->update(['image' => 1]); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return Video|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
||
| 320 | */ |
||
| 321 | public function getSiteByID(string $column, int $id): \Illuminate\Database\Eloquent\Model|bool|\Illuminate\Database\Eloquent\Builder|Video |
||
| 322 | { |
||
| 323 | $return = false; |
||
| 324 | $video = Video::query()->where('id', $id)->first([$column]); |
||
| 325 | if ($column === '*') { |
||
| 326 | $return = $video; |
||
| 327 | } elseif ($column !== '*' && $video !== null) { |
||
| 328 | $return = $video[$column]; |
||
| 329 | } |
||
| 330 | |||
| 331 | return $return; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Retrieves the Episode ID using the Video ID and either: |
||
| 336 | * season/episode numbers OR the airdate. |
||
| 337 | * |
||
| 338 | * Returns the Episode ID or false if not found |
||
| 339 | * |
||
| 340 | * @return int|false |
||
| 341 | */ |
||
| 342 | public function getBySeasonEp(int|string $id, int|string $series, int|string $episode, string $airdate = ''): bool|int |
||
| 343 | { |
||
| 344 | if ($series > 0 && $episode > 0) { |
||
| 345 | $queryString = sprintf('tve.series = %d AND tve.episode = %d', $series, $episode); |
||
| 346 | } elseif (! empty($airdate)) { |
||
| 347 | $queryString = sprintf('DATE(tve.firstaired) = %s', escapeString(date('Y-m-d', strtotime($airdate)))); |
||
| 348 | } else { |
||
| 349 | return false; |
||
| 350 | } |
||
| 351 | |||
| 352 | $episodeArr = DB::selectOne( |
||
| 353 | sprintf( |
||
| 354 | ' |
||
| 355 | SELECT tve.id |
||
| 356 | FROM tv_episodes tve |
||
| 357 | WHERE tve.videos_id = %d |
||
| 358 | AND %s', |
||
| 359 | $id, |
||
| 360 | $queryString |
||
| 361 | ) |
||
| 362 | ); |
||
| 363 | |||
| 364 | return $episodeArr->id ?? false; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns (true) if episodes for a given Video ID exist or don't (false). |
||
| 369 | */ |
||
| 370 | public function countEpsByVideoID(int $videoId): bool |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return array|false |
||
| 380 | */ |
||
| 381 | public function parseInfo(string $relname): bool|array |
||
| 382 | { |
||
| 383 | $showInfo['name'] = $this->parseName($relname); |
||
| 384 | |||
| 385 | if (! empty($showInfo['name'])) { |
||
| 386 | // Retrieve the country from the cleaned name |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Parses the release searchname and returns a show title. |
||
| 432 | */ |
||
| 433 | private function parseName(string $relname): string |
||
| 434 | { |
||
| 435 | $showName = ''; |
||
| 436 | |||
| 437 | $following = '[^a-z0-9]([(|\[]\w+[)|\]]\s)*?(\d\d-\d\d|\d{1,3}x\d{2,3}|\(?(19|20)\d{2}\)?|(480|720|1080|2160)[ip]|AAC2?|BD-?Rip|Blu-?Ray|D0?\d|DD5|DiVX|DLMux|DTS|DVD(-?Rip)?|E\d{2,3}|[HX][\-_. ]?26[45]|ITA(-ENG)?|HEVC|[HPS]DTV|PROPER|REPACK|Season|Episode|S\d+[^a-z0-9]?((E\d+)[abr]?)*|WEB[\-_. ]?(DL|Rip)|XViD)[^a-z0-9]?'; |
||
| 438 | |||
| 439 | // Handle fansub/release group prefixes like [SubsPlease], [Erai-raws], [ASW], etc. |
||
| 440 | $cleanRelname = preg_replace('/^\[[^\]]+\][_\s]*/i', '', $relname); |
||
| 441 | |||
| 442 | if (preg_match('/^([^a-z0-9]{2,}|(sample|proof|repost)-)(?P<name>[\w .\-!&]+?)'.$following.'/i', $cleanRelname, $hits)) { |
||
| 443 | $showName = $hits['name']; |
||
| 444 | } elseif (preg_match('/^(?P<name>[\w!&][\s\w\'._\-!&]*?)'.$following.'/i', $cleanRelname, $hits)) { |
||
| 445 | $showName = $hits['name']; |
||
| 446 | } |
||
| 447 | |||
| 448 | // If still no match, try extracting name before common delimiters |
||
| 449 | if (empty($showName)) { |
||
| 450 | // Try to extract name before - SxxExx pattern |
||
| 451 | if (preg_match('/^(?P<name>.+?)[_\s]*[-_\s]+S\d{1,2}E\d{1,3}/i', $cleanRelname, $hits)) { |
||
| 452 | $showName = $hits['name']; |
||
| 453 | } |
||
| 454 | // Try to extract name before standalone episode number like "- 06 [" or "_09_[" |
||
| 455 | elseif (preg_match('/^(?P<name>.+?)[_\s]+[-_]+[_\s]*\d{1,3}[_\s]*[\[(]/i', $cleanRelname, $hits)) { |
||
| 456 | $showName = $hits['name']; |
||
| 457 | } |
||
| 458 | // Handle anime-style "Show_Name_-_Episode_Title_-_XX_[quality]" format (extract before first _-_) |
||
| 459 | elseif (preg_match('/^(?P<name>.+?)_-_.+?_-_\d{1,3}_[\[(]/i', $cleanRelname, $hits)) { |
||
| 460 | $showName = $hits['name']; |
||
| 461 | } |
||
| 462 | // Simpler pattern: extract everything before "_-_" for anime releases |
||
| 463 | elseif (preg_match('/^(?P<name>[^_]+(?:_[^_-]+)*)_-_/i', $cleanRelname, $hits)) { |
||
| 464 | $showName = $hits['name']; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | $showName = preg_replace('/'.$following.'/i', ' ', $showName); |
||
| 469 | $showName = preg_replace('/^\d{6}/', '', $showName); |
||
| 470 | $showName = $this->convertAcronyms($showName); |
||
| 471 | $showName = preg_replace('/\(.*?\)|[._]/i', ' ', $showName); |
||
| 472 | $showName = trim(preg_replace('/\s{2,}/', ' ', $showName)); |
||
| 473 | |||
| 474 | return $showName; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Convert acronyms with dots to condensed form. |
||
| 479 | */ |
||
| 480 | private function convertAcronyms(string $str): string |
||
| 481 | { |
||
| 482 | return preg_replace_callback( |
||
| 483 | '/\b((?:[A-Za-z]\.){2,}[A-Za-z]?\.?)\b/', |
||
| 484 | function ($matches) { |
||
| 485 | return str_replace('.', '', $matches[1]); |
||
| 486 | }, |
||
| 487 | $str |
||
| 488 | ); |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Normalize well-known daily/talk show titles to their canonical names. |
||
| 493 | */ |
||
| 494 | protected function normalizeShowTitle(string $cleanName): string |
||
| 495 | { |
||
| 496 | $normalized = strtolower(trim($cleanName)); |
||
| 497 | $aliases = [ |
||
| 498 | 'grits' => 'Girls Raised in the South', |
||
| 499 | 'shield' => 'Agents of S.H.I.E.L.D.', |
||
| 500 | 'stephen colbert' => 'The Late Show with Stephen Colbert', |
||
| 501 | 'late show with stephen colbert' => 'The Late Show with Stephen Colbert', |
||
| 502 | 'late show stephen colbert' => 'The Late Show with Stephen Colbert', |
||
| 503 | 'the late show stephen colbert' => 'The Late Show with Stephen Colbert', |
||
| 504 | 'colbert' => 'The Late Show with Stephen Colbert', |
||
| 505 | 'daily show' => 'The Daily Show', |
||
| 506 | 'the daily show' => 'The Daily Show', |
||
| 507 | 'daily show with jon stewart' => 'The Daily Show with Jon Stewart', |
||
| 508 | 'the daily show with jon stewart' => 'The Daily Show with Jon Stewart', |
||
| 509 | 'daily show with trevor noah' => 'The Daily Show with Trevor Noah', |
||
| 510 | 'the daily show with trevor noah' => 'The Daily Show with Trevor Noah', |
||
| 511 | 'seth meyers' => 'Late Night with Seth Meyers', |
||
| 512 | 'late night with seth meyers' => 'Late Night with Seth Meyers', |
||
| 513 | 'late night seth meyers' => 'Late Night with Seth Meyers', |
||
| 514 | 'jimmy kimmel' => 'Jimmy Kimmel Live!', |
||
| 515 | 'jimmy kimmel live' => 'Jimmy Kimmel Live!', |
||
| 516 | 'the late late show with james corden' => 'The Late Late Show with James Corden', |
||
| 517 | 'late late show with james corden' => 'The Late Late Show with James Corden', |
||
| 518 | 'late show with james corden' => 'The Late Late Show with James Corden', |
||
| 519 | 'james corden' => 'The Late Late Show with James Corden', |
||
| 520 | 'late late show james corden' => 'The Late Late Show with James Corden', |
||
| 521 | 'jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon', |
||
| 522 | 'the tonight show starring jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon', |
||
| 523 | 'tonight show with jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon', |
||
| 524 | 'tonight show jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon', |
||
| 525 | ]; |
||
| 526 | |||
| 527 | if (isset($aliases[$normalized])) { |
||
| 528 | return $aliases[$normalized]; |
||
| 529 | } |
||
| 530 | |||
| 531 | return $cleanName; |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Parses the release searchname for the season/episode/airdate information. |
||
| 536 | */ |
||
| 537 | private function parseSeasonEp(string $relname): array |
||
| 538 | { |
||
| 539 | $episodeArr = []; |
||
| 540 | |||
| 541 | // S01E01-E02 and S01E01-02 |
||
| 542 | 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, $hits)) { |
||
| 543 | $episodeArr['season'] = (int) $hits[2]; |
||
| 544 | $episodeArr['episode'] = [(int) $hits[3], (int) $hits[4]]; |
||
| 545 | } |
||
| 546 | // S01E0102 and S01E01E02 |
||
| 547 | elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{2})[^a-z0-9]?e(\d{2})e?(\d{2})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 548 | $episodeArr['season'] = (int) $hits[2]; |
||
| 549 | $episodeArr['episode'] = (int) $hits[3]; |
||
| 550 | } |
||
| 551 | // S01E01 and S01.E01 |
||
| 552 | elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})[abr]?[^a-z0-9]?/i', $relname, $hits)) { |
||
| 553 | $episodeArr['season'] = (int) $hits[2]; |
||
| 554 | $episodeArr['episode'] = (int) $hits[3]; |
||
| 555 | } |
||
| 556 | // S01 |
||
| 557 | elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 558 | $episodeArr['season'] = (int) $hits[2]; |
||
| 559 | $episodeArr['episode'] = 'all'; |
||
| 560 | } |
||
| 561 | // S01D1 and S1D1 |
||
| 562 | elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?d\d{1}[^a-z0-9]?/i', $relname, $hits)) { |
||
| 563 | $episodeArr['season'] = (int) $hits[2]; |
||
| 564 | $episodeArr['episode'] = 'all'; |
||
| 565 | } |
||
| 566 | // 1x01 and 101 |
||
| 567 | elseif (preg_match('/^(.*?)[^a-z0-9](\d{1,2})x(\d{1,3})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 568 | $episodeArr['season'] = (int) $hits[2]; |
||
| 569 | $episodeArr['episode'] = (int) $hits[3]; |
||
| 570 | } |
||
| 571 | // 2009.01.01 and 2009-01-01 |
||
| 572 | elseif (preg_match('/^(.*?)[^a-z0-9](?P<airdate>(19|20)(\d{2})[.\/-](\d{2})[.\/-](\d{2}))[^a-z0-9]?/i', $relname, $hits)) { |
||
| 573 | $episodeArr['season'] = 0; |
||
| 574 | $episodeArr['episode'] = 0; |
||
| 575 | $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate']))); |
||
| 576 | } |
||
| 577 | // 01.01.2009 |
||
| 578 | elseif (preg_match('/^(.*?)[^a-z0-9](?P<airdate>(\d{2})[^a-z0-9](\d{2})[^a-z0-9](19|20)(\d{2}))[^a-z0-9]?/i', $relname, $hits)) { |
||
| 579 | $episodeArr['season'] = 0; |
||
| 580 | $episodeArr['episode'] = 0; |
||
| 581 | $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate']))); |
||
| 582 | } |
||
| 583 | // 01.01.09 |
||
| 584 | elseif (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 585 | $year = ($hits[4] <= 99 && $hits[4] > 15) ? '19'.$hits[4] : '20'.$hits[4]; |
||
| 586 | $airdate = $year.'/'.$hits[2].'/'.$hits[3]; |
||
| 587 | $episodeArr['season'] = 0; |
||
| 588 | $episodeArr['episode'] = 0; |
||
| 589 | $episodeArr['airdate'] = date('Y-m-d', strtotime($airdate)); |
||
| 590 | } |
||
| 591 | // 2009.E01 |
||
| 592 | elseif (preg_match('/^(.*?)[^a-z0-9]20(\d{2})[^a-z0-9](\d{1,3})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 593 | $episodeArr['season'] = '20'.$hits[2]; |
||
| 594 | $episodeArr['episode'] = (int) $hits[3]; |
||
| 595 | } |
||
| 596 | // 2009.Part1 |
||
| 597 | elseif (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9]Part(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 598 | $episodeArr['season'] = $hits[2].$hits[3]; |
||
| 599 | $episodeArr['episode'] = (int) $hits[4]; |
||
| 600 | } |
||
| 601 | // Part1/Pt1 |
||
| 602 | elseif (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9](\d{1,2})[^a-z0-9]?/i', $relname, $hits)) { |
||
| 603 | $episodeArr['season'] = 1; |
||
| 604 | $episodeArr['episode'] = (int) $hits[2]; |
||
| 605 | } |
||
| 606 | // Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY |
||
| 607 | elseif (preg_match('/^(.*?)[^a-z0-9]EP?[^a-z0-9]?(\d{1,3})/i', $relname, $hits)) { |
||
| 608 | $episodeArr['season'] = 1; |
||
| 609 | $episodeArr['episode'] = (int) $hits[2]; |
||
| 610 | } |
||
| 611 | // Anime style: [Fansub]_Show_Name_-_06_[quality] or Show_-_Episode_Title_-_06_[ |
||
| 612 | elseif (preg_match('/[_\s][-_][_\s](\d{1,3})[_\s]*[\[(]/i', $relname, $hits)) { |
||
| 613 | $episodeArr['season'] = 1; |
||
| 614 | $episodeArr['episode'] = (int) $hits[1]; |
||
| 615 | } |
||
| 616 | // Season.1 |
||
| 617 | elseif (preg_match('/^(.*?)[^a-z0-9]Seasons?[^a-z0-9]?(\d{1,2})/i', $relname, $hits)) { |
||
| 618 | $episodeArr['season'] = (int) $hits[2]; |
||
| 619 | $episodeArr['episode'] = 'all'; |
||
| 620 | } |
||
| 621 | |||
| 622 | return $episodeArr; |
||
| 623 | } |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Parses the cleaned release name to determine if it has a country appended. |
||
| 627 | */ |
||
| 628 | private function parseCountry(string $showName): string |
||
| 629 | { |
||
| 630 | if (preg_match('/[^a-z0-9](US|UK|AU|NZ|CA|NL|Canada|Australia|America|United[^a-z0-9]States|United[^a-z0-9]Kingdom)/i', $showName, $countryMatch)) { |
||
| 631 | $currentCountry = strtolower($countryMatch[1]); |
||
| 632 | if ($currentCountry === 'canada') { |
||
| 633 | $country = 'CA'; |
||
| 634 | } elseif ($currentCountry === 'australia') { |
||
| 635 | $country = 'AU'; |
||
| 636 | } elseif ($currentCountry === 'america' || $currentCountry === 'united states') { |
||
| 637 | $country = 'US'; |
||
| 638 | } elseif ($currentCountry === 'united kingdom') { |
||
| 639 | $country = 'UK'; |
||
| 640 | } else { |
||
| 641 | $country = strtoupper($countryMatch[1]); |
||
| 642 | } |
||
| 643 | } else { |
||
| 644 | $country = ''; |
||
| 645 | } |
||
| 646 | |||
| 647 | return $country; |
||
| 648 | } |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Supplementary to parseInfo |
||
| 652 | * Cleans a derived local 'showname' for better matching probability |
||
| 653 | */ |
||
| 654 | public function cleanName(string $str): string |
||
| 655 | { |
||
| 656 | $str = str_replace(['.', '_'], ' ', $str); |
||
| 657 | |||
| 658 | $str = str_replace(['à', 'á', 'â', 'ã', 'ä', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä'], 'a', $str); |
||
| 659 | $str = str_replace(['ç', 'Ç'], 'c', $str); |
||
| 660 | $str = str_replace(['Σ', 'è', 'é', 'ê', 'ë', 'È', 'É', 'Ê', 'Ë'], 'e', $str); |
||
| 661 | $str = str_replace(['ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï'], 'i', $str); |
||
| 662 | $str = str_replace(['ò', 'ó', 'ô', 'õ', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö'], 'o', $str); |
||
| 663 | $str = str_replace(['ù', 'ú', 'û', 'ü', 'ū', 'Ú', 'Û', 'Ü', 'Ū'], 'u', $str); |
||
| 664 | $str = str_replace('ß', 'ss', $str); |
||
| 665 | |||
| 666 | $str = str_replace('&', 'and', $str); |
||
| 667 | $str = preg_replace('/^(history|discovery) channel/i', '', $str); |
||
| 668 | $str = str_replace(["'", ':', '!', '"', '#', '*', "'", ',', '(', ')', '?'], '', $str); |
||
| 669 | $str = str_replace('$', 's', $str); |
||
| 670 | |||
| 671 | // Remove language tags that are commonly found in release names |
||
| 672 | $str = preg_replace('/\b(German|French|Spanish|Italian|Dutch|Russian|Japanese|Korean|Chinese|Portuguese|Swedish|Norwegian|Danish|Finnish|Polish|Czech|Hungarian|Romanian|Greek|Turkish|Arabic|Hebrew|Hindi|Thai|Vietnamese|Indonesian|Malay|Tagalog|DL|Dubbed|Subbed|MULTI|MULTi)\b/i', '', $str); |
||
| 673 | |||
| 674 | // Remove common subtitle/episode indicators that aren't part of the show title |
||
| 675 | $str = preg_replace('/\s+-\s+.*(wo andere|where others|Arbeiten|Working).*$/i', '', $str); |
||
| 676 | |||
| 677 | // Clean up trailing hyphens, episode numbers, and other artifacts |
||
| 678 | $str = preg_replace('/\s*-\s*$/', '', $str); // Trailing hyphen |
||
| 679 | $str = preg_replace('/\s+\d{1,3}\s*$/', '', $str); // Trailing episode number |
||
| 680 | $str = preg_replace('/\s+-$/', '', $str); // Another trailing hyphen cleanup |
||
| 681 | |||
| 682 | $str = preg_replace('/\s{2,}/', ' ', $str); |
||
| 683 | |||
| 684 | $str = trim($str, '"-'); // Trim quotes and hyphens from both ends |
||
| 685 | |||
| 686 | return trim($str); |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Simple function that compares two strings of text |
||
| 691 | */ |
||
| 692 | public function checkMatch($ourName, $scrapeName, $probability): float|int |
||
| 693 | { |
||
| 694 | similar_text($ourName, $scrapeName, $matchpct); |
||
| 695 | |||
| 696 | if ($matchpct >= $probability) { |
||
| 697 | return $matchpct; |
||
| 698 | } |
||
| 699 | |||
| 700 | return 0; |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Convert 2012-24-07 to 2012-07-24 |
||
| 705 | */ |
||
| 706 | public function checkDate(bool|string|null $date): string |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Checks API response returns have all REQUIRED attributes set |
||
| 723 | */ |
||
| 724 | public function checkRequiredAttr($array, string $type): bool |
||
| 725 | { |
||
| 726 | $required = ['failedToMatchType']; |
||
| 727 | |||
| 728 | switch ($type) { |
||
| 729 | case 'tvdbS': |
||
| 730 | $required = ['tvdb_id', 'name', 'overview', 'first_air_time']; |
||
| 731 | break; |
||
| 732 | case 'tvdbE': |
||
| 733 | $required = ['name', 'seasonNumber', 'number', 'aired', 'overview']; |
||
| 734 | break; |
||
| 735 | case 'tvmazeS': |
||
| 736 | $required = ['id', 'name', 'summary', 'premiered', 'country']; |
||
| 737 | break; |
||
| 738 | case 'tvmazeE': |
||
| 739 | $required = ['name', 'season', 'number', 'airdate', 'summary']; |
||
| 740 | break; |
||
| 741 | case 'tmdbS': |
||
| 742 | $required = ['id', 'name', 'overview', 'first_air_date', 'origin_country']; |
||
| 743 | break; |
||
| 744 | case 'tmdbE': |
||
| 745 | $required = ['name', 'season_number', 'episode_number', 'air_date', 'overview']; |
||
| 746 | break; |
||
| 747 | case 'traktS': |
||
| 748 | $required = ['title', 'ids', 'overview', 'first_aired', 'airs', 'country']; |
||
| 749 | break; |
||
| 750 | case 'traktE': |
||
| 751 | $required = ['title', 'season', 'number', 'overview', 'first_aired']; |
||
| 752 | break; |
||
| 753 | } |
||
| 754 | |||
| 755 | foreach ($required as $req) { |
||
| 756 | if (! \in_array($type, ['tmdbS', 'tmdbE', 'traktS', 'traktE'], false)) { |
||
| 757 | if (! isset($array->$req)) { |
||
| 758 | return false; |
||
| 759 | } |
||
| 760 | } elseif (! isset($array[$req])) { |
||
| 761 | return false; |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | return true; |
||
| 766 | } |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Truncates title for display. |
||
| 770 | */ |
||
| 771 | protected function truncateTitle(string $title, int $maxLength = 45): string |
||
| 778 | } |
||
| 779 | } |
||
| 780 | |||
| 781 |