AbstractTvProvider::getBySeasonEp()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 23
rs 9.7333
cc 4
nc 3
nop 4
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 Illuminate\Support\Facades\DB;
13
14
/**
15
 * Class AbstractTvProvider -- abstract extension of BaseVideoProvider
16
 * Contains functions suitable for re-use in all TV scrapers.
17
 */
18
abstract class AbstractTvProvider extends BaseVideoProvider
19
{
20
    // Television Sources
21
    protected const SOURCE_NONE = 0;   // No Scrape source
22
23
    protected const SOURCE_TVDB = 1;   // Scrape source was TVDB
24
25
    protected const SOURCE_TVMAZE = 2;   // Scrape source was TVMAZE
26
27
    protected const SOURCE_TMDB = 3;   // Scrape source was TMDB
28
29
    protected const SOURCE_TRAKT = 4;   // Scrape source was Trakt
30
31
    protected const SOURCE_IMDB = 5;   // Scrape source was IMDB
32
33
    // Anime Sources
34
    protected const SOURCE_ANIDB = 10;   // Scrape source was AniDB
35
36
    // Processing signifiers
37
    protected const PROCESS_TVDB = 0;   // Process TVDB First
38
39
    protected const PROCESS_TVMAZE = -1;   // Process TVMaze Second
40
41
    protected const PROCESS_TMDB = -2;   // Process TMDB Third
42
43
    protected const PROCESS_TRAKT = -3;   // Process Trakt Fourth
44
45
    protected const PROCESS_IMDB = -4;   // Process IMDB Fifth
46
47
    protected const NO_MATCH_FOUND = -6;   // Failed All Methods
48
49
    protected const FAILED_PARSE = -100; // Failed Parsing
50
51
    public int $tvqty;
52
53
    /**
54
     * @string Path to Save Images
55
     */
56
    public string $imgSavePath;
57
58
    /**
59
     * @var array Site ID columns for TV
60
     */
61
    public array $siteColumns;
62
63
    /**
64
     * @var string The TV categories_id lookup SQL language
65
     */
66
    public string $catWhere;
67
68
    /**
69
     * AbstractTvProvider constructor.
70
     *
71
     * @throws \Exception
72
     */
73
    public function __construct()
74
    {
75
        parent::__construct();
76
        $this->catWhere = 'categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER.' AND categories_id != '.Category::TV_ANIME;
77
        $this->tvqty = Settings::settingValue('maxrageprocessed') !== '' ? (int) Settings::settingValue('maxrageprocessed') : 75;
78
        $this->imgSavePath = storage_path('covers/tvshows/');
79
        $this->siteColumns = ['tvdb', 'trakt', 'tvrage', 'tvmaze', 'imdb', 'tmdb'];
80
    }
81
82
    /**
83
     * Retrieve banner image from site using its API.
84
     */
85
    abstract public function getBanner(int $videoID, int $siteId): mixed;
86
87
    /**
88
     * Retrieve info of TV episode from site using its API.
89
     *
90
     * @return array|false False on failure, an array of information fields otherwise.
91
     */
92
    abstract public function getEpisodeInfo(int|string $siteId, int|string $series, int|string $episode): array|bool;
93
94
    /**
95
     * Retrieve poster image for TV episode from site using its API.
96
     *
97
     * @param  int  $videoId  ID from videos table.
98
     */
99
    abstract public function getPoster(int $videoId): int;
100
101
    /**
102
     * Retrieve info of TV programme from site using it's API.
103
     *
104
     * @param  string  $name  Title of programme to look up. Usually a cleaned up version from releases table.
105
     * @return array|false False on failure, an array of information fields otherwise.
106
     */
107
    abstract public function getShowInfo(string $name): bool|array;
108
109
    /**
110
     * Assigns API show response values to a formatted array for insertion
111
     * Returns the formatted array.
112
     */
113
    abstract public function formatShowInfo($show): array;
114
115
    /**
116
     * Assigns API episode response values to a formatted array for insertion
117
     * Returns the formatted array.
118
     */
119
    abstract public function formatEpisodeInfo($episode): array;
120
121
    /**
122
     * @return Release[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|int
123
     */
124
    public function getTvReleases(string $groupID = '', string $guidChar = '', int $lookupSetting = 1, int $status = 0): array|\Illuminate\Database\Eloquent\Collection|int|\Illuminate\Support\Collection
125
    {
126
        $ret = 0;
127
        if ($lookupSetting === 0) {
128
            return $ret;
129
        }
130
131
        $qry = Release::query()
132
            ->where(['videos_id' => 0, 'tv_episodes_id' => $status])
133
            ->where('size', '>', 1048576)
134
            ->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER])
135
            ->where('categories_id', '<>', Category::TV_ANIME)
136
            ->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

136
            ->orderByDesc(/** @scrutinizer ignore-type */ 'postdate')
Loading history...
137
            ->limit($this->tvqty);
138
        if ($groupID !== '') {
139
            $qry->where('groups_id', $groupID);
140
        }
141
        if ($guidChar !== '') {
142
            $qry->where('leftguid', $guidChar);
143
        }
144
        if ($lookupSetting === 2) {
145
            $qry->where('isrenamed', '=', 1);
146
        }
147
148
        return $qry->get();
149
    }
150
151
    public function setVideoIdFound(int $videoId, int $releaseId, int $episodeId): void
152
    {
153
        Release::query()
154
            ->where('id', $releaseId)
155
            ->update(['videos_id' => $videoId, 'tv_episodes_id' => $episodeId]);
156
157
        ReleaseBrowseService::bumpCacheVersion();
158
    }
159
160
    /**
161
     * Updates the release tv_episodes_id status when scraper match is not found.
162
     */
163
    public function setVideoNotFound($status, $Id): void
164
    {
165
        Release::query()
166
            ->where('id', $Id)
167
            ->update(['tv_episodes_id' => $status]);
168
    }
169
170
    /**
171
     * Inserts a new video ID into the database for TV shows
172
     * If a duplicate is found it is handle by calling update instead.
173
     */
174
    public function add(array $show = []): int
175
    {
176
        $videoId = false;
177
178
        // Check if the country is not a proper code and retrieve if not
179
        if ($show['country'] !== '' && \strlen($show['country']) > 2) {
180
            $show['country'] = countryCode($show['country']);
181
        }
182
183
        // Check if video already exists based on site ID info
184
        // if that fails be sure we're not inserting duplicates by checking the title
185
        foreach ($this->siteColumns as $column) {
186
            if ((int) $show[$column] > 0) {
187
                $videoId = $this->getVideoIDFromSiteID($column, $show[$column]);
188
            }
189
            if ($videoId !== false) {
190
                break;
191
            }
192
        }
193
194
        if ($videoId === false) {
195
            $title = Video::query()->where('title', $show['title'])->first(['title']);
196
            if ($title === null) {
197
                // Insert the Show
198
                $videoId = Video::query()->insertGetId([
199
                    'type' => $show['type'],
200
                    'title' => $show['title'],
201
                    'countries_id' => $show['country'] ?? '',
202
                    'started' => $show['started'],
203
                    'source' => $show['source'],
204
                    'tvdb' => $show['tvdb'],
205
                    'trakt' => $show['trakt'],
206
                    'tvrage' => $show['tvrage'],
207
                    'tvmaze' => $show['tvmaze'],
208
                    'imdb' => $show['imdb'],
209
                    'tmdb' => $show['tmdb'],
210
                ]);
211
                // Insert the supplementary show info
212
                TvInfo::query()->insertOrIgnore([
213
                    'videos_id' => $videoId,
214
                    'summary' => $show['summary'],
215
                    'publisher' => $show['publisher'],
216
                    'localzone' => $show['localzone'],
217
                ]);
218
                // If we have AKAs\aliases, insert those as well
219
                if (! empty($show['aliases'])) {
220
                    $this->addAliases($videoId, $show['aliases']);
221
                }
222
            }
223
        } else {
224
            // If a local match was found, just update missing video info
225
            $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

225
            $this->update(/** @scrutinizer ignore-type */ $videoId, $show);
Loading history...
226
        }
227
228
        return $videoId;
229
    }
230
231
    public function addEpisode(int $videoId, array $episode = []): bool|int
232
    {
233
        $episodeId = $this->getBySeasonEp($videoId, $episode['series'], $episode['episode'], $episode['firstaired']);
234
235
        if ($episodeId === false) {
236
            $episodeId = TvEpisode::query()->insertOrIgnore(
237
                [
238
                    'videos_id' => $videoId,
239
                    'series' => $episode['series'],
240
                    'episode' => $episode['episode'],
241
                    'se_complete' => $episode['se_complete'],
242
                    'title' => $episode['title'],
243
                    'firstaired' => $episode['firstaired'] !== '' ? $episode['firstaired'] : null,
244
                    'summary' => $episode['summary'],
245
                ]
246
            );
247
        }
248
249
        return $episodeId;
250
    }
251
252
    public function update(int $videoId, array $show = []): void
253
    {
254
        if ($show['country'] !== '') {
255
            $show['country'] = countryCode($show['country']);
256
        }
257
258
        $ifStringID = 'IF(%s = 0, %s, %s)';
259
        $ifStringInfo = "IF(%s = '', %s, %s)";
260
261
        DB::update(
262
            sprintf(
263
                '
264
				UPDATE videos v
265
				LEFT JOIN tv_info tvi ON v.id = tvi.videos_id
266
				SET v.countries_id = %s, v.tvdb = %s, v.trakt = %s, v.tvrage = %s,
267
					v.tvmaze = %s, v.imdb = %s, v.tmdb = %s,
268
					tvi.summary = %s, tvi.publisher = %s, tvi.localzone = %s
269
				WHERE v.id = %d',
270
                sprintf($ifStringInfo, 'v.countries_id', escapeString($show['country']), 'v.countries_id'),
271
                sprintf($ifStringID, 'v.tvdb', $show['tvdb'], 'v.tvdb'),
272
                sprintf($ifStringID, 'v.trakt', $show['trakt'], 'v.trakt'),
273
                sprintf($ifStringID, 'v.tvrage', $show['tvrage'], 'v.tvrage'),
274
                sprintf($ifStringID, 'v.tvmaze', $show['tvmaze'], 'v.tvmaze'),
275
                sprintf($ifStringID, 'v.imdb', $show['imdb'], 'v.imdb'),
276
                sprintf($ifStringID, 'v.tmdb', $show['tmdb'], 'v.tmdb'),
277
                sprintf($ifStringInfo, 'tvi.summary', escapeString($show['summary']), 'tvi.summary'),
278
                sprintf($ifStringInfo, 'tvi.publisher', escapeString($show['publisher']), 'tvi.publisher'),
279
                sprintf($ifStringInfo, 'tvi.localzone', escapeString($show['localzone']), 'tvi.localzone'),
280
                $videoId
281
            )
282
        );
283
        if (! empty($show['aliases'])) {
284
            $this->addAliases($videoId, $show['aliases']);
285
        }
286
    }
287
288
    /**
289
     * @throws \Throwable
290
     */
291
    public function delete(int $id): mixed
292
    {
293
        return DB::transaction(function () use ($id) {
294
            DB::delete(
295
                sprintf(
296
                    '
297
				DELETE v, tvi, tve, va
298
				FROM videos v
299
				LEFT JOIN tv_info tvi ON v.id = tvi.videos_id
300
				LEFT JOIN tv_episodes tve ON v.id = tve.videos_id
301
				LEFT JOIN videos_aliases va ON v.id = va.videos_id
302
				WHERE v.id = %d',
303
                    $id
304
                )
305
            );
306
        }, 3);
307
    }
308
309
    public function setCoverFound(int $videoId): void
310
    {
311
        TvInfo::query()->where('videos_id', $videoId)->update(['image' => 1]);
312
    }
313
314
    /**
315
     * @return Video|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
316
     */
317
    public function getSiteByID(string $column, int $id): \Illuminate\Database\Eloquent\Model|bool|\Illuminate\Database\Eloquent\Builder|Video
318
    {
319
        $return = false;
320
        $video = Video::query()->where('id', $id)->first([$column]);
321
        if ($column === '*') {
322
            $return = $video;
323
        } elseif ($column !== '*' && $video !== null) {
324
            $return = $video[$column];
325
        }
326
327
        return $return;
328
    }
329
330
    /**
331
     * Retrieves the Episode ID using the Video ID and either:
332
     * season/episode numbers OR the airdate.
333
     *
334
     * Returns the Episode ID or false if not found
335
     *
336
     * @return int|false
337
     */
338
    public function getBySeasonEp(int|string $id, int|string $series, int|string $episode, string $airdate = ''): bool|int
339
    {
340
        if ($series > 0 && $episode > 0) {
341
            $queryString = sprintf('tve.series = %d AND tve.episode = %d', $series, $episode);
342
        } elseif (! empty($airdate)) {
343
            $queryString = sprintf('DATE(tve.firstaired) = %s', escapeString(date('Y-m-d', strtotime($airdate))));
344
        } else {
345
            return false;
346
        }
347
348
        $episodeArr = DB::selectOne(
349
            sprintf(
350
                '
351
				SELECT tve.id
352
				FROM tv_episodes tve
353
				WHERE tve.videos_id = %d
354
				AND %s',
355
                $id,
356
                $queryString
357
            )
358
        );
359
360
        return $episodeArr->id ?? false;
361
    }
362
363
    /**
364
     * Returns (true) if episodes for a given Video ID exist or don't (false).
365
     */
366
    public function countEpsByVideoID(int $videoId): bool
367
    {
368
        $count = TvEpisode::query()
369
            ->where('videos_id', $videoId)->count(['id']);
370
371
        return $count !== null && $count > 0;
372
    }
373
374
    /**
375
     * @return array|false
376
     */
377
    public function parseInfo(string $relname): bool|array
378
    {
379
        $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...
380
381
        if (! empty($showInfo['name'])) {
382
            // Retrieve the country from the cleaned name
383
            $showInfo['country'] = $this->parseCountry($showInfo['name']);
384
385
            // Clean show name.
386
            $showInfo['cleanname'] = preg_replace('/ - \d+$/i', '', $this->cleanName($showInfo['name']));
387
            $showInfo['cleanname'] = $this->normalizeShowTitle($showInfo['cleanname']);
388
389
            // Get the Season/Episode/Airdate
390
            $showInfo += $this->parseSeasonEp($relname);
391
392
            // --- Post-parse correction for daily talk shows misclassified as Season = Year ---
393
            if (isset($showInfo['season'], $showInfo['episode']) && ! isset($showInfo['airdate'])) {
394
                if (is_numeric($showInfo['season']) && (int) $showInfo['season'] >= 1900 && (int) $showInfo['season'] <= (int) date('Y') + 1) {
395
                    if (preg_match('/(?P<year>(19|20)\d{2})[.\-\/](?P<month>\d{2})[.\-\/](?P<day>\d{2})/i', $relname, $dateHits)) {
396
                        $year = (int) $dateHits['year'];
397
                        $month = (int) $dateHits['month'];
398
                        $day = (int) $dateHits['day'];
399
                        if ($month >= 1 && $month <= 12 && $day >= 1 && $day <= 31) {
400
                            $showInfo['airdate'] = sprintf('%04d-%02d-%02d', $year, $month, $day);
401
                            $showInfo['season'] = 0;
402
                            $showInfo['episode'] = 0;
403
                        }
404
                    }
405
                }
406
            }
407
408
            if (isset($showInfo['season'], $showInfo['episode'])) {
409
                if (! isset($showInfo['airdate'])) {
410
                    if (preg_match('/[^a-z0-9](?P<year>(19|20)(\d{2}))[^a-z0-9]/i', $relname, $yearMatch)) {
411
                        $showInfo['cleanname'] .= ' ('.$yearMatch['year'].')';
412
                    }
413
                    if (\is_array($showInfo['episode'])) {
414
                        $showInfo['episode'] = $showInfo['episode'][0];
415
                    }
416
                    $showInfo['airdate'] = '';
417
                }
418
419
                return $showInfo;
420
            }
421
        }
422
423
        return false;
424
    }
425
426
    /**
427
     * Parses the release searchname and returns a show title.
428
     */
429
    private function parseName(string $relname): string
430
    {
431
        $showName = '';
432
433
        $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]?';
434
435
        // Handle fansub/release group prefixes like [SubsPlease], [Erai-raws], [ASW], etc.
436
        $cleanRelname = preg_replace('/^\[[^\]]+\][_\s]*/i', '', $relname);
437
438
        if (preg_match('/^([^a-z0-9]{2,}|(sample|proof|repost)-)(?P<name>[\w .\-!&]+?)'.$following.'/i', $cleanRelname, $hits)) {
439
            $showName = $hits['name'];
440
        } elseif (preg_match('/^(?P<name>[\w!&][\s\w\'._\-!&]*?)'.$following.'/i', $cleanRelname, $hits)) {
441
            $showName = $hits['name'];
442
        }
443
444
        // If still no match, try extracting name before common delimiters
445
        if (empty($showName)) {
446
            // Try to extract name before - SxxExx pattern
447
            if (preg_match('/^(?P<name>.+?)[_\s]*[-_\s]+S\d{1,2}E\d{1,3}/i', $cleanRelname, $hits)) {
448
                $showName = $hits['name'];
449
            }
450
            // Try to extract name before standalone episode number like "- 06 [" or "_09_["
451
            elseif (preg_match('/^(?P<name>.+?)[_\s]+[-_]+[_\s]*\d{1,3}[_\s]*[\[(]/i', $cleanRelname, $hits)) {
452
                $showName = $hits['name'];
453
            }
454
            // Handle anime-style "Show_Name_-_Episode_Title_-_XX_[quality]" format (extract before first _-_)
455
            elseif (preg_match('/^(?P<name>.+?)_-_.+?_-_\d{1,3}_[\[(]/i', $cleanRelname, $hits)) {
456
                $showName = $hits['name'];
457
            }
458
            // Simpler pattern: extract everything before "_-_" for anime releases
459
            elseif (preg_match('/^(?P<name>[^_]+(?:_[^_-]+)*)_-_/i', $cleanRelname, $hits)) {
460
                $showName = $hits['name'];
461
            }
462
        }
463
464
        $showName = preg_replace('/'.$following.'/i', ' ', $showName);
465
        $showName = preg_replace('/^\d{6}/', '', $showName);
466
        $showName = $this->convertAcronyms($showName);
467
        $showName = preg_replace('/\(.*?\)|[._]/i', ' ', $showName);
468
        $showName = trim(preg_replace('/\s{2,}/', ' ', $showName));
469
470
        return $showName;
471
    }
472
473
    /**
474
     * Convert acronyms with dots to condensed form.
475
     */
476
    private function convertAcronyms(string $str): string
477
    {
478
        return preg_replace_callback(
479
            '/\b((?:[A-Za-z]\.){2,}[A-Za-z]?\.?)\b/',
480
            function ($matches) {
481
                return str_replace('.', '', $matches[1]);
482
            },
483
            $str
484
        );
485
    }
486
487
    /**
488
     * Normalize well-known daily/talk show titles to their canonical names.
489
     * Also includes German-to-English mappings for popular shows.
490
     */
491
    protected function normalizeShowTitle(string $cleanName): string
492
    {
493
        $normalized = strtolower(trim($cleanName));
494
        $aliases = [
495
            // English show aliases
496
            'grits' => 'Girls Raised in the South',
497
            'shield' => 'Agents of S.H.I.E.L.D.',
498
            'stephen colbert' => 'The Late Show with Stephen Colbert',
499
            'late show with stephen colbert' => 'The Late Show with Stephen Colbert',
500
            'late show stephen colbert' => 'The Late Show with Stephen Colbert',
501
            'the late show stephen colbert' => 'The Late Show with Stephen Colbert',
502
            'colbert' => 'The Late Show with Stephen Colbert',
503
            'daily show' => 'The Daily Show',
504
            'the daily show' => 'The Daily Show',
505
            'daily show with jon stewart' => 'The Daily Show with Jon Stewart',
506
            'the daily show with jon stewart' => 'The Daily Show with Jon Stewart',
507
            'daily show with trevor noah' => 'The Daily Show with Trevor Noah',
508
            'the daily show with trevor noah' => 'The Daily Show with Trevor Noah',
509
            'seth meyers' => 'Late Night with Seth Meyers',
510
            'late night with seth meyers' => 'Late Night with Seth Meyers',
511
            'late night seth meyers' => 'Late Night with Seth Meyers',
512
            'jimmy kimmel' => 'Jimmy Kimmel Live!',
513
            'jimmy kimmel live' => 'Jimmy Kimmel Live!',
514
            'the late late show with james corden' => 'The Late Late Show with James Corden',
515
            'late late show with james corden' => 'The Late Late Show with James Corden',
516
            'late show with james corden' => 'The Late Late Show with James Corden',
517
            'james corden' => 'The Late Late Show with James Corden',
518
            'late late show james corden' => 'The Late Late Show with James Corden',
519
            'jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
520
            'the tonight show starring jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
521
            'tonight show with jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
522
            'tonight show jimmy fallon' => 'The Tonight Show Starring Jimmy Fallon',
523
524
            // German title to English title mappings
525
            'oak island fluch und legende' => 'The Curse of Oak Island',
526
            'oak island - fluch und legende' => 'The Curse of Oak Island',
527
            'tierische freundschaften' => 'Animal Odd Couples',
528
            'zwischen meer und maloche' => 'Zwischen Meer und Maloche',
529
            'die schatzsucher von oak island' => 'The Curse of Oak Island',
530
            'game of thrones das lied von eis und feuer' => 'Game of Thrones',
531
            'die simpsons' => 'The Simpsons',
532
            'die wilden siebziger' => 'That 70s Show',
533
            'how i met your mother' => 'How I Met Your Mother',
534
            'scrubs die anfanger' => 'Scrubs',
535
            'scrubs die anfaenger' => 'Scrubs',
536
            'der prinz von bel-air' => 'The Fresh Prince of Bel-Air',
537
            'prinz von bel-air' => 'The Fresh Prince of Bel-Air',
538
            'der prinz von bel air' => 'The Fresh Prince of Bel-Air',
539
            'alarm fur cobra 11' => 'Alarm for Cobra 11',
540
            'alarm fuer cobra 11' => 'Alarm for Cobra 11',
541
            'soko' => 'SOKO',
542
            'der bergdoktor' => 'Der Bergdoktor',
543
            'das boot' => 'Das Boot',
544
            'babylon berlin' => 'Babylon Berlin',
545
            'dark' => 'Dark',
546
            'tatort' => 'Tatort',
547
            'polizeiruf 110' => 'Polizeiruf 110',
548
        ];
549
550
        if (isset($aliases[$normalized])) {
551
            return $aliases[$normalized];
552
        }
553
554
        return $cleanName;
555
    }
556
557
    /**
558
     * Parses the release searchname for the season/episode/airdate information.
559
     */
560
    private function parseSeasonEp(string $relname): array
561
    {
562
        $episodeArr = [];
563
564
        // S01E01-E02 and S01E01-02
565
        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)) {
566
            $episodeArr['season'] = (int) $hits[2];
567
            $episodeArr['episode'] = [(int) $hits[3], (int) $hits[4]];
568
        }
569
        // S01E0102 and S01E01E02
570
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{2})[^a-z0-9]?e(\d{2})e?(\d{2})[^a-z0-9]?/i', $relname, $hits)) {
571
            $episodeArr['season'] = (int) $hits[2];
572
            $episodeArr['episode'] = (int) $hits[3];
573
        }
574
        // S01E01 and S01.E01
575
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})[abr]?[^a-z0-9]?/i', $relname, $hits)) {
576
            $episodeArr['season'] = (int) $hits[2];
577
            $episodeArr['episode'] = (int) $hits[3];
578
        }
579
        // S01
580
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
581
            $episodeArr['season'] = (int) $hits[2];
582
            $episodeArr['episode'] = 'all';
583
        }
584
        // S01D1 and S1D1
585
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?d\d{1}[^a-z0-9]?/i', $relname, $hits)) {
586
            $episodeArr['season'] = (int) $hits[2];
587
            $episodeArr['episode'] = 'all';
588
        }
589
        // 1x01 and 101
590
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{1,2})x(\d{1,3})[^a-z0-9]?/i', $relname, $hits)) {
591
            $episodeArr['season'] = (int) $hits[2];
592
            $episodeArr['episode'] = (int) $hits[3];
593
        }
594
        // 2009.01.01 and 2009-01-01
595
        elseif (preg_match('/^(.*?)[^a-z0-9](?P<airdate>(19|20)(\d{2})[.\/-](\d{2})[.\/-](\d{2}))[^a-z0-9]?/i', $relname, $hits)) {
596
            $episodeArr['season'] = 0;
597
            $episodeArr['episode'] = 0;
598
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate'])));
599
        }
600
        // 01.01.2009
601
        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)) {
602
            $episodeArr['season'] = 0;
603
            $episodeArr['episode'] = 0;
604
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $hits['airdate'])));
605
        }
606
        // 01.01.09
607
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]?/i', $relname, $hits)) {
608
            $year = ($hits[4] <= 99 && $hits[4] > 15) ? '19'.$hits[4] : '20'.$hits[4];
609
            $airdate = $year.'/'.$hits[2].'/'.$hits[3];
610
            $episodeArr['season'] = 0;
611
            $episodeArr['episode'] = 0;
612
            $episodeArr['airdate'] = date('Y-m-d', strtotime($airdate));
613
        }
614
        // 2009.E01
615
        elseif (preg_match('/^(.*?)[^a-z0-9]20(\d{2})[^a-z0-9](\d{1,3})[^a-z0-9]?/i', $relname, $hits)) {
616
            $episodeArr['season'] = '20'.$hits[2];
617
            $episodeArr['episode'] = (int) $hits[3];
618
        }
619
        // 2009.Part1
620
        elseif (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9]Part(\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
621
            $episodeArr['season'] = $hits[2].$hits[3];
622
            $episodeArr['episode'] = (int) $hits[4];
623
        }
624
        // Part1/Pt1
625
        elseif (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9](\d{1,2})[^a-z0-9]?/i', $relname, $hits)) {
626
            $episodeArr['season'] = 1;
627
            $episodeArr['episode'] = (int) $hits[2];
628
        }
629
        // Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY
630
        elseif (preg_match('/^(.*?)[^a-z0-9]EP?[^a-z0-9]?(\d{1,3})/i', $relname, $hits)) {
631
            $episodeArr['season'] = 1;
632
            $episodeArr['episode'] = (int) $hits[2];
633
        }
634
        // Anime style: [Fansub]_Show_Name_-_06_[quality] or Show_-_Episode_Title_-_06_[
635
        elseif (preg_match('/[_\s][-_][_\s](\d{1,3})[_\s]*[\[(]/i', $relname, $hits)) {
636
            $episodeArr['season'] = 1;
637
            $episodeArr['episode'] = (int) $hits[1];
638
        }
639
        // Season.1
640
        elseif (preg_match('/^(.*?)[^a-z0-9]Seasons?[^a-z0-9]?(\d{1,2})/i', $relname, $hits)) {
641
            $episodeArr['season'] = (int) $hits[2];
642
            $episodeArr['episode'] = 'all';
643
        }
644
645
        return $episodeArr;
646
    }
647
648
    /**
649
     * Parses the cleaned release name to determine if it has a country appended.
650
     */
651
    private function parseCountry(string $showName): string
652
    {
653
        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)) {
654
            $currentCountry = strtolower($countryMatch[1]);
655
            if ($currentCountry === 'canada') {
656
                $country = 'CA';
657
            } elseif ($currentCountry === 'australia') {
658
                $country = 'AU';
659
            } elseif ($currentCountry === 'america' || $currentCountry === 'united states') {
660
                $country = 'US';
661
            } elseif ($currentCountry === 'united kingdom') {
662
                $country = 'UK';
663
            } else {
664
                $country = strtoupper($countryMatch[1]);
665
            }
666
        } else {
667
            $country = '';
668
        }
669
670
        return $country;
671
    }
672
673
    /**
674
     * Supplementary to parseInfo
675
     * Cleans a derived local 'showname' for better matching probability
676
     */
677
    public function cleanName(string $str): string
678
    {
679
        $str = str_replace(['.', '_'], ' ', $str);
680
681
        $str = str_replace(['à', 'á', 'â', 'ã', 'ä', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä'], 'a', $str);
682
        $str = str_replace(['ç', 'Ç'], 'c', $str);
683
        $str = str_replace(['Σ', 'è', 'é', 'ê', 'ë', 'È', 'É', 'Ê', 'Ë'], 'e', $str);
684
        $str = str_replace(['ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï'], 'i', $str);
685
        $str = str_replace(['ò', 'ó', 'ô', 'õ', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö'], 'o', $str);
686
        $str = str_replace(['ù', 'ú', 'û', 'ü', 'ū', 'Ú', 'Û', 'Ü', 'Ū'], 'u', $str);
687
        $str = str_replace('ß', 'ss', $str);
688
689
        $str = str_replace('&', 'and', $str);
690
        $str = preg_replace('/^(history|discovery) channel/i', '', $str);
691
        $str = str_replace(["'", ':', '!', '"', '#', '*', "'", ',', '(', ')', '?'], '', $str);
692
        $str = str_replace('$', 's', $str);
693
694
        // Remove language tags that are commonly found in release names
695
        $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);
696
697
        // Remove common subtitle/episode indicators that aren't part of the show title
698
        $str = preg_replace('/\s+-\s+.*(wo andere|where others|Arbeiten|Working).*$/i', '', $str);
699
700
        // Clean up trailing hyphens, episode numbers, and other artifacts
701
        $str = preg_replace('/\s*-\s*$/', '', $str);  // Trailing hyphen
702
        $str = preg_replace('/\s+\d{1,3}\s*$/', '', $str);  // Trailing episode number
703
        $str = preg_replace('/\s+-$/', '', $str);  // Another trailing hyphen cleanup
704
705
        $str = preg_replace('/\s{2,}/', ' ', $str);
706
707
        $str = trim($str, '"-');  // Trim quotes and hyphens from both ends
708
709
        return trim($str);
710
    }
711
712
    /**
713
     * Simple function that compares two strings of text
714
     */
715
    public function checkMatch($ourName, $scrapeName, $probability): float|int
716
    {
717
        similar_text($ourName, $scrapeName, $matchpct);
718
719
        if ($matchpct >= $probability) {
720
            return $matchpct;
721
        }
722
723
        return 0;
724
    }
725
726
    /**
727
     * Convert 2012-24-07 to 2012-07-24
728
     */
729
    public function checkDate(bool|string|null $date): string
730
    {
731
        if (! empty($date)) {
732
            $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

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