Passed
Push — master ( c84873...a36453 )
by Darko
19:01
created

AbstractTvProvider::normalizeShowTitle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 64
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 64
rs 8.9818
cc 2
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Services\TvProcessing\Providers;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use App\Models\Settings;
8
use App\Models\TvEpisode;
9
use App\Models\TvInfo;
10
use App\Models\Video;
11
use App\Services\Releases\ReleaseBrowseService;
12
use Blacklight\ColorCLI;
13
use Illuminate\Support\Facades\DB;
14
15
/**
16
 * Class AbstractTvProvider -- abstract extension of BaseVideoProvider
17
 * Contains functions suitable for re-use in all TV scrapers.
18
 */
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')
0 ignored issues
show
Bug introduced by
'postdate' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
            ->orderByDesc(/** @scrutinizer ignore-type */ 'postdate')
Loading history...
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
168
    {
169
        Release::query()
170
            ->where('id', $Id)
171
            ->update(['tv_episodes_id' => $status]);
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);
0 ignored issues
show
Bug introduced by
It seems like $videoId can also be of type true; however, parameter $videoId of App\Services\TvProcessin...actTvProvider::update() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

229
            $this->update(/** @scrutinizer ignore-type */ $videoId, $show);
Loading history...
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
371
    {
372
        $count = TvEpisode::query()
373
            ->where('videos_id', $videoId)->count(['id']);
374
375
        return $count !== null && $count > 0;
376
    }
377
378
    /**
379
     * @return array|false
380
     */
381
    public function parseInfo(string $relname): bool|array
382
    {
383
        $showInfo['name'] = $this->parseName($relname);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$showInfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $showInfo = array(); before regardless.
Loading history...
384
385
        if (! empty($showInfo['name'])) {
386
            // Retrieve the country from the cleaned name
387
            $showInfo['country'] = $this->parseCountry($showInfo['name']);
388
389
            // Clean show name.
390
            $showInfo['cleanname'] = preg_replace('/ - \d+$/i', '', $this->cleanName($showInfo['name']));
391
            $showInfo['cleanname'] = $this->normalizeShowTitle($showInfo['cleanname']);
392
393
            // Get the Season/Episode/Airdate
394
            $showInfo += $this->parseSeasonEp($relname);
395
396
            // --- Post-parse correction for daily talk shows misclassified as Season = Year ---
397
            if (isset($showInfo['season'], $showInfo['episode']) && ! isset($showInfo['airdate'])) {
398
                if (is_numeric($showInfo['season']) && (int) $showInfo['season'] >= 1900 && (int) $showInfo['season'] <= (int) date('Y') + 1) {
399
                    if (preg_match('/(?P<year>(19|20)\d{2})[.\-\/](?P<month>\d{2})[.\-\/](?P<day>\d{2})/i', $relname, $dateHits)) {
400
                        $year = (int) $dateHits['year'];
401
                        $month = (int) $dateHits['month'];
402
                        $day = (int) $dateHits['day'];
403
                        if ($month >= 1 && $month <= 12 && $day >= 1 && $day <= 31) {
404
                            $showInfo['airdate'] = sprintf('%04d-%02d-%02d', $year, $month, $day);
405
                            $showInfo['season'] = 0;
406
                            $showInfo['episode'] = 0;
407
                        }
408
                    }
409
                }
410
            }
411
412
            if (isset($showInfo['season'], $showInfo['episode'])) {
413
                if (! isset($showInfo['airdate'])) {
414
                    if (preg_match('/[^a-z0-9](?P<year>(19|20)(\d{2}))[^a-z0-9]/i', $relname, $yearMatch)) {
415
                        $showInfo['cleanname'] .= ' ('.$yearMatch['year'].')';
416
                    }
417
                    if (\is_array($showInfo['episode'])) {
418
                        $showInfo['episode'] = $showInfo['episode'][0];
419
                    }
420
                    $showInfo['airdate'] = '';
421
                }
422
423
                return $showInfo;
424
            }
425
        }
426
427
        return false;
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
     * Also includes German-to-English mappings for popular shows.
494
     */
495
    protected function normalizeShowTitle(string $cleanName): string
496
    {
497
        $normalized = strtolower(trim($cleanName));
498
        $aliases = [
499
            // English show aliases
500
            'grits' => 'Girls Raised in the South',
501
            'shield' => 'Agents of S.H.I.E.L.D.',
502
            'stephen colbert' => 'The Late Show with Stephen Colbert',
503
            'late show with stephen colbert' => 'The Late Show with Stephen Colbert',
504
            'late show stephen colbert' => 'The Late Show with Stephen Colbert',
505
            'the late show stephen colbert' => 'The Late Show with Stephen Colbert',
506
            'colbert' => 'The Late Show with Stephen Colbert',
507
            'daily show' => 'The Daily Show',
508
            'the daily show' => 'The Daily Show',
509
            'daily show with jon stewart' => 'The Daily Show with Jon Stewart',
510
            'the daily show with jon stewart' => 'The Daily Show with Jon Stewart',
511
            'daily show with trevor noah' => 'The Daily Show with Trevor Noah',
512
            'the daily show with trevor noah' => 'The Daily Show with Trevor Noah',
513
            'seth meyers' => 'Late Night with Seth Meyers',
514
            'late night with seth meyers' => 'Late Night with Seth Meyers',
515
            'late night seth meyers' => 'Late Night with Seth Meyers',
516
            'jimmy kimmel' => 'Jimmy Kimmel Live!',
517
            'jimmy kimmel live' => 'Jimmy Kimmel Live!',
518
            'the late late show with james corden' => 'The Late Late Show with James Corden',
519
            'late late show with james corden' => 'The Late Late Show with James Corden',
520
            'late show with james corden' => 'The Late Late Show with James Corden',
521
            'james corden' => 'The Late Late Show with James Corden',
522
            'late late show james corden' => 'The Late Late Show with James Corden',
523
            'jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
524
            'the tonight show starring jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
525
            'tonight show with jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
526
            'tonight show jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
527
528
            // German title to English title mappings
529
            'oak island fluch und legende' => 'The Curse of Oak Island',
530
            'oak island - fluch und legende' => 'The Curse of Oak Island',
531
            'tierische freundschaften' => 'Animal Odd Couples',
532
            'zwischen meer und maloche' => 'Zwischen Meer und Maloche',
533
            'die schatzsucher von oak island' => 'The Curse of Oak Island',
534
            'game of thrones das lied von eis und feuer' => 'Game of Thrones',
535
            'die simpsons' => 'The Simpsons',
536
            'die wilden siebziger' => 'That 70s Show',
537
            'how i met your mother' => 'How I Met Your Mother',
538
            'scrubs die anfanger' => 'Scrubs',
539
            'scrubs die anfaenger' => 'Scrubs',
540
            'der prinz von bel-air' => 'The Fresh Prince of Bel-Air',
541
            'prinz von bel-air' => 'The Fresh Prince of Bel-Air',
542
            'der prinz von bel air' => 'The Fresh Prince of Bel-Air',
543
            'alarm fur cobra 11' => 'Alarm for Cobra 11',
544
            'alarm fuer cobra 11' => 'Alarm for Cobra 11',
545
            'soko' => 'SOKO',
546
            'der bergdoktor' => 'Der Bergdoktor',
547
            'das boot' => 'Das Boot',
548
            'babylon berlin' => 'Babylon Berlin',
549
            'dark' => 'Dark',
550
            'tatort' => 'Tatort',
551
            'polizeiruf 110' => 'Polizeiruf 110',
552
        ];
553
554
        if (isset($aliases[$normalized])) {
555
            return $aliases[$normalized];
556
        }
557
558
        return $cleanName;
559
    }
560
561
    /**
562
     * Parses the release searchname for the season/episode/airdate information.
563
     */
564
    private function parseSeasonEp(string $relname): array
565
    {
566
        $episodeArr = [];
567
568
        // S01E01-E02 and S01E01-02
569
        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)) {
570
            $episodeArr['season'] = (int) $hits[2];
571
            $episodeArr['episode'] = [(int) $hits[3], (int) $hits[4]];
572
        }
573
        // S01E0102 and S01E01E02
574
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{2})[^a-z0-9]?e(\d{2})e?(\d{2})[^a-z0-9]?/i', $relname, $hits)) {
575
            $episodeArr['season'] = (int) $hits[2];
576
            $episodeArr['episode'] = (int) $hits[3];
577
        }
578
        // S01E01 and S01.E01
579
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})[abr]?[^a-z0-9]?/i', $relname, $hits)) {
580
            $episodeArr['season'] = (int) $hits[2];
581
            $episodeArr['episode'] = (int) $hits[3];
582
        }
583
        // S01
584
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
585
            $episodeArr['season'] = (int) $hits[2];
586
            $episodeArr['episode'] = 'all';
587
        }
588
        // S01D1 and S1D1
589
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?d\d{1}[^a-z0-9]?/i', $relname, $hits)) {
590
            $episodeArr['season'] = (int) $hits[2];
591
            $episodeArr['episode'] = 'all';
592
        }
593
        // 1x01 and 101
594
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{1,2})x(\d{1,3})[^a-z0-9]?/i', $relname, $hits)) {
595
            $episodeArr['season'] = (int) $hits[2];
596
            $episodeArr['episode'] = (int) $hits[3];
597
        }
598
        // 2009.01.01 and 2009-01-01
599
        elseif (preg_match('/^(.*?)[^a-z0-9](?P<airdate>(19|20)(\d{2})[.\/-](\d{2})[.\/-](\d{2}))[^a-z0-9]?/i', $relname, $hits)) {
600
            $episodeArr['season'] = 0;
601
            $episodeArr['episode'] = 0;
602
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate'])));
603
        }
604
        // 01.01.2009
605
        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)) {
606
            $episodeArr['season'] = 0;
607
            $episodeArr['episode'] = 0;
608
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate'])));
609
        }
610
        // 01.01.09
611
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]?/i', $relname, $hits)) {
612
            $year = ($hits[4] <= 99 && $hits[4] > 15) ? '19'.$hits[4] : '20'.$hits[4];
613
            $airdate = $year.'/'.$hits[2].'/'.$hits[3];
614
            $episodeArr['season'] = 0;
615
            $episodeArr['episode'] = 0;
616
            $episodeArr['airdate'] = date('Y-m-d', strtotime($airdate));
617
        }
618
        // 2009.E01
619
        elseif (preg_match('/^(.*?)[^a-z0-9]20(\d{2})[^a-z0-9](\d{1,3})[^a-z0-9]?/i', $relname, $hits)) {
620
            $episodeArr['season'] = '20'.$hits[2];
621
            $episodeArr['episode'] = (int) $hits[3];
622
        }
623
        // 2009.Part1
624
        elseif (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9]Part(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
625
            $episodeArr['season'] = $hits[2].$hits[3];
626
            $episodeArr['episode'] = (int) $hits[4];
627
        }
628
        // Part1/Pt1
629
        elseif (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9](\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
630
            $episodeArr['season'] = 1;
631
            $episodeArr['episode'] = (int) $hits[2];
632
        }
633
        // Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY
634
        elseif (preg_match('/^(.*?)[^a-z0-9]EP?[^a-z0-9]?(\d{1,3})/i', $relname, $hits)) {
635
            $episodeArr['season'] = 1;
636
            $episodeArr['episode'] = (int) $hits[2];
637
        }
638
        // Anime style: [Fansub]_Show_Name_-_06_[quality] or Show_-_Episode_Title_-_06_[
639
        elseif (preg_match('/[_\s][-_][_\s](\d{1,3})[_\s]*[\[(]/i', $relname, $hits)) {
640
            $episodeArr['season'] = 1;
641
            $episodeArr['episode'] = (int) $hits[1];
642
        }
643
        // Season.1
644
        elseif (preg_match('/^(.*?)[^a-z0-9]Seasons?[^a-z0-9]?(\d{1,2})/i', $relname, $hits)) {
645
            $episodeArr['season'] = (int) $hits[2];
646
            $episodeArr['episode'] = 'all';
647
        }
648
649
        return $episodeArr;
650
    }
651
652
    /**
653
     * Parses the cleaned release name to determine if it has a country appended.
654
     */
655
    private function parseCountry(string $showName): string
656
    {
657
        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)) {
658
            $currentCountry = strtolower($countryMatch[1]);
659
            if ($currentCountry === 'canada') {
660
                $country = 'CA';
661
            } elseif ($currentCountry === 'australia') {
662
                $country = 'AU';
663
            } elseif ($currentCountry === 'america' || $currentCountry === 'united states') {
664
                $country = 'US';
665
            } elseif ($currentCountry === 'united kingdom') {
666
                $country = 'UK';
667
            } else {
668
                $country = strtoupper($countryMatch[1]);
669
            }
670
        } else {
671
            $country = '';
672
        }
673
674
        return $country;
675
    }
676
677
    /**
678
     * Supplementary to parseInfo
679
     * Cleans a derived local 'showname' for better matching probability
680
     */
681
    public function cleanName(string $str): string
682
    {
683
        $str = str_replace(['.', '_'], ' ', $str);
684
685
        $str = str_replace(['à', 'á', 'â', 'ã', 'ä', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä'], 'a', $str);
686
        $str = str_replace(['ç', 'Ç'], 'c', $str);
687
        $str = str_replace(['Σ', 'è', 'é', 'ê', 'ë', 'È', 'É', 'Ê', 'Ë'], 'e', $str);
688
        $str = str_replace(['ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï'], 'i', $str);
689
        $str = str_replace(['ò', 'ó', 'ô', 'õ', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö'], 'o', $str);
690
        $str = str_replace(['ù', 'ú', 'û', 'ü', 'ū', 'Ú', 'Û', 'Ü', 'Ū'], 'u', $str);
691
        $str = str_replace('ß', 'ss', $str);
692
693
        $str = str_replace('&', 'and', $str);
694
        $str = preg_replace('/^(history|discovery) channel/i', '', $str);
695
        $str = str_replace(["'", ':', '!', '"', '#', '*', "'", ',', '(', ')', '?'], '', $str);
696
        $str = str_replace('$', 's', $str);
697
698
        // Remove language tags that are commonly found in release names
699
        $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);
700
701
        // Remove common subtitle/episode indicators that aren't part of the show title
702
        $str = preg_replace('/\s+-\s+.*(wo andere|where others|Arbeiten|Working).*$/i', '', $str);
703
704
        // Clean up trailing hyphens, episode numbers, and other artifacts
705
        $str = preg_replace('/\s*-\s*$/', '', $str);  // Trailing hyphen
706
        $str = preg_replace('/\s+\d{1,3}\s*$/', '', $str);  // Trailing episode number
707
        $str = preg_replace('/\s+-$/', '', $str);  // Another trailing hyphen cleanup
708
709
        $str = preg_replace('/\s{2,}/', ' ', $str);
710
711
        $str = trim($str, '"-');  // Trim quotes and hyphens from both ends
712
713
        return trim($str);
714
    }
715
716
    /**
717
     * Simple function that compares two strings of text
718
     */
719
    public function checkMatch($ourName, $scrapeName, $probability): float|int
720
    {
721
        similar_text($ourName, $scrapeName, $matchpct);
722
723
        if ($matchpct >= $probability) {
724
            return $matchpct;
725
        }
726
727
        return 0;
728
    }
729
730
    /**
731
     * Convert 2012-24-07 to 2012-07-24
732
     */
733
    public function checkDate(bool|string|null $date): string
734
    {
735
        if (! empty($date)) {
736
            $chk = explode(' ', $date);
0 ignored issues
show
Bug introduced by
It seems like $date can also be of type boolean; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

736
            $chk = explode(' ', /** @scrutinizer ignore-type */ $date);
Loading history...
737
            $chkd = explode('-', $chk[0]);
738
            if ($chkd[1] > 12) {
739
                $date = date('Y-m-d H:i:s', strtotime($chkd[1].' '.$chkd[2].' '.$chkd[0]));
740
            }
741
        } else {
742
            $date = null;
743
        }
744
745
        return $date;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $date could return the type boolean|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
746
    }
747
748
    /**
749
     * Checks API response returns have all REQUIRED attributes set
750
     */
751
    public function checkRequiredAttr($array, string $type): bool
752
    {
753
        $required = ['failedToMatchType'];
754
755
        switch ($type) {
756
            case 'tvdbS':
757
                $required = ['tvdb_id', 'name', 'overview', 'first_air_time'];
758
                break;
759
            case 'tvdbE':
760
                $required = ['name', 'seasonNumber', 'number', 'aired', 'overview'];
761
                break;
762
            case 'tvmazeS':
763
                $required = ['id', 'name', 'summary', 'premiered', 'country'];
764
                break;
765
            case 'tvmazeE':
766
                $required = ['name', 'season', 'number', 'airdate', 'summary'];
767
                break;
768
            case 'tmdbS':
769
                $required = ['id', 'name', 'overview', 'first_air_date', 'origin_country'];
770
                break;
771
            case 'tmdbE':
772
                $required = ['name', 'season_number', 'episode_number', 'air_date', 'overview'];
773
                break;
774
            case 'traktS':
775
                $required = ['title', 'ids', 'overview', 'first_aired', 'airs', 'country'];
776
                break;
777
            case 'traktE':
778
                $required = ['title', 'season', 'number', 'overview', 'first_aired'];
779
                break;
780
        }
781
782
        foreach ($required as $req) {
783
            if (! \in_array($type, ['tmdbS', 'tmdbE', 'traktS', 'traktE'], false)) {
784
                if (! isset($array->$req)) {
785
                    return false;
786
                }
787
            } elseif (! isset($array[$req])) {
788
                return false;
789
            }
790
        }
791
792
        return true;
793
    }
794
795
    /**
796
     * Truncates title for display.
797
     */
798
    protected function truncateTitle(string $title, int $maxLength = 45): string
799
    {
800
        if (mb_strlen($title) <= $maxLength) {
801
            return $title;
802
        }
803
804
        return mb_substr($title, 0, $maxLength - 3).'...';
805
    }
806
}
807
808