Completed
Push — dev ( 8773ea...325b1b )
by Darko
12:42
created

TV   F

Complexity

Total Complexity 89

Size/Duplication

Total Lines 765
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 89
eloc 308
dl 0
loc 765
ccs 0
cts 292
cp 0
rs 2
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setVideoNotFound() 0 5 1
A parseInfo() 0 33 6
A countEpsByVideoID() 0 6 2
A update() 0 33 3
A parseName() 0 25 3
A addEpisode() 0 19 3
A setCoverFound() 0 3 1
A getBySeasonEp() 0 23 4
B add() 0 55 9
A getTvReleases() 0 25 5
A delete() 0 16 1
A getSiteByID() 0 11 4
A setVideoIdFound() 0 5 1
A __construct() 0 8 2
A cleanName() 0 21 1
A checkMatch() 0 9 2
A checkDate() 0 13 3
C checkRequiredAttr() 0 44 14
B parseCountry() 0 21 7
C parseSeasonEp() 0 82 17

How to fix   Complexity   

Complex Class

Complex classes like TV 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 TV, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Blacklight\processing\tv;
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 Blacklight\ColorCLI;
12
use Blacklight\processing\Videos;
13
use Blacklight\utility\Country;
14
use Illuminate\Support\Facades\DB;
15
16
/**
17
 * Class TV -- abstract extension of Videos
18
 * Contains functions suitable for re-use in all TV scrapers.
19
 */
20
abstract class TV extends Videos
21
{
22
    // Television Sources
23
    protected const SOURCE_NONE = 0;   // No Scrape source
24
    protected const SOURCE_TVDB = 1;   // Scrape source was TVDB
25
    protected const SOURCE_TVMAZE = 2;   // Scrape source was TVMAZE
26
    protected const SOURCE_TMDB = 3;   // Scrape source was TMDB
27
    protected const SOURCE_TRAKT = 4;   // Scrape source was Trakt
28
    protected const SOURCE_IMDB = 5;   // Scrape source was IMDB
29
30
    // Anime Sources
31
    protected const SOURCE_ANIDB = 10;   // Scrape source was AniDB
32
33
    // Processing signifiers
34
    protected const PROCESS_TVDB = 0;   // Process TVDB First
35
    protected const PROCESS_TVMAZE = -1;   // Process TVMaze Second
36
    protected const PROCESS_TMDB = -2;   // Process TMDB Third
37
    protected const PROCESS_TRAKT = -3;   // Process Trakt Fourth
38
    protected const PROCESS_IMDB = -4;   // Process IMDB Fifth
39
    protected const NO_MATCH_FOUND = -6;   // Failed All Methods
40
    protected const FAILED_PARSE = -100; // Failed Parsing
41
42
    /**
43
     * @var int
44
     */
45
    public $tvqty;
46
47
    /**
48
     * @string Path to Save Images
49
     */
50
    public $imgSavePath;
51
52
    /**
53
     * @var array Site ID columns for TV
54
     */
55
    public $siteColumns;
56
57
    /**
58
     * @var string The TV categories_id lookup SQL language
59
     */
60
    public $catWhere;
61
62
    /**
63
     * @var \Blacklight\ColorCLI
64
     */
65
    protected $colorCli;
66
67
    /**
68
     * TV constructor.
69
     *
70
     * @param array $options
71
     * @throws \Exception
72
     */
73
    public function __construct(array $options = [])
74
    {
75
        parent::__construct($options);
76
        $this->colorCli = new ColorCLI();
77
        $this->catWhere = 'categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER.' AND categories_id != '.Category::TV_ANIME;
78
        $this->tvqty = Settings::settingValue('..maxrageprocessed') !== '' ? (int) Settings::settingValue('..maxrageprocessed') : 75;
0 ignored issues
show
introduced by
The condition App\Models\Settings::set...xrageprocessed') !== '' is always true.
Loading history...
79
        $this->imgSavePath = NN_COVERS.'tvshows'.DS;
80
        $this->siteColumns = ['tvdb', 'trakt', 'tvrage', 'tvmaze', 'imdb', 'tmdb'];
81
    }
82
83
    /**
84
     * Retrieve banner image from site using its API.
85
     *
86
     * @param $videoID
87
     * @param $siteId
88
     *
89
     * @return mixed
90
     */
91
    abstract protected function getBanner($videoID, $siteId);
92
93
    /**
94
     * Retrieve info of TV episode from site using its API.
95
     *
96
     * @param int $siteId
97
     * @param int $series
98
     * @param int $episode
99
     *
100
     * @return array|false    False on failure, an array of information fields otherwise.
101
     */
102
    abstract protected function getEpisodeInfo($siteId, $series, $episode);
103
104
    /**
105
     * Retrieve poster image for TV episode from site using its API.
106
     *
107
     * @param int $videoId ID from videos table.
108
     *
109
     * @return int
110
     */
111
    abstract protected function getPoster($videoId): int;
112
113
    /**
114
     * Retrieve info of TV programme from site using it's API.
115
     *
116
     * @param string $name Title of programme to look up. Usually a cleaned up version from releases table.
117
     *
118
     * @return array|false    False on failure, an array of information fields otherwise.
119
     */
120
    abstract protected function getShowInfo($name);
121
122
    /**
123
     * Assigns API show response values to a formatted array for insertion
124
     * Returns the formatted array.
125
     *
126
     * @param $show
127
     *
128
     * @return array
129
     */
130
    abstract protected function formatShowInfo($show): array;
131
132
    /**
133
     * Assigns API episode response values to a formatted array for insertion
134
     * Returns the formatted array.
135
     *
136
     * @param $episode
137
     *
138
     * @return array
139
     */
140
    abstract protected function formatEpisodeInfo($episode): array;
141
142
    /**
143
     * Retrieve releases for TV processing.
144
     *
145
     *
146
     * @param string $groupID
147
     * @param string $guidChar
148
     * @param int $lookupSetting
149
     * @param int $status
150
     * @return \Illuminate\Database\Eloquent\Collection|int|static[]
151
     */
152
    public function getTvReleases($groupID = '', $guidChar = '', $lookupSetting = 1, $status = 0)
153
    {
154
        $ret = 0;
155
        if ($lookupSetting === 0) {
156
            return $ret;
157
        }
158
159
        $qry = Release::query()
160
            ->where(['nzbstatus' => 1, 'videos_id' => 0, 'tv_episodes_id' => $status])
161
            ->where('size', '>', 1048576)
162
            ->whereBetween('categories_id', [Category::TV_ROOT, Category::TV_OTHER])
163
            ->where('categories_id', '<>', Category::TV_ANIME)
164
            ->orderBy('postdate', 'desc')
165
            ->limit($this->tvqty);
166
        if ($groupID !== '') {
167
            $qry->where('groups_id', $groupID);
168
        }
169
        if ($guidChar !== '') {
170
            $qry->where('leftguid', $guidChar);
171
        }
172
        if ($lookupSetting === 2) {
173
            $qry->where('isrenamed', '=', 1);
174
        }
175
176
        return $qry->get();
177
    }
178
179
    /**
180
     * Updates the release when match for the current scraper is found.
181
     *
182
     * @param     $videoId
183
     * @param     $releaseId
184
     * @param int $episodeId
185
     */
186
    public function setVideoIdFound($videoId, $releaseId, $episodeId): void
187
    {
188
        Release::query()
189
            ->where('id', $releaseId)
190
            ->update(['videos_id' => $videoId, 'tv_episodes_id' => $episodeId]);
191
    }
192
193
    /**
194
     * Updates the release tv_episodes_id status when scraper match is not found.
195
     *
196
     * @param $status
197
     * @param $Id
198
     */
199
    public function setVideoNotFound($status, $Id): void
200
    {
201
        Release::query()
202
            ->where('id', $Id)
203
            ->update(['tv_episodes_id' => $status]);
204
    }
205
206
    /**
207
     * Inserts a new video ID into the database for TV shows
208
     * If a duplicate is found it is handle by calling update instead.
209
     *
210
     * @param array $show
211
     *
212
     * @return int
213
     */
214
    public function add(array $show = []): int
215
    {
216
        $videoId = false;
217
218
        // Check if the country is not a proper code and retrieve if not
219
        if ($show['country'] !== '' && \strlen($show['country']) > 2) {
220
            $show['country'] = Country::countryCode($show['country']);
221
        }
222
223
        // Check if video already exists based on site ID info
224
        // if that fails be sure we're not inserting duplicates by checking the title
225
        foreach ($this->siteColumns as $column) {
226
            if ((int) $show[$column] > 0) {
227
                $videoId = $this->getVideoIDFromSiteID($column, $show[$column]);
228
            }
229
            if ($videoId !== false) {
230
                break;
231
            }
232
        }
233
234
        if ($videoId === false) {
235
            $title = Video::query()->where('title', $show['title'])->first(['title']);
236
            if ($title === null) {
237
                // Insert the Show
238
                $videoId = Video::query()->insertGetId([
239
                        'type' => $show['type'],
240
                        'title' => $show['title'],
241
                        'countries_id' => $show['country'] ?? '',
242
                        'started' => $show['started'],
243
                        'source' => $show['source'],
244
                        'tvdb' => $show['tvdb'],
245
                        'trakt' => $show['trakt'],
246
                        'tvrage' => $show['tvrage'],
247
                        'tvmaze' => $show['tvmaze'],
248
                        'imdb' => $show['imdb'],
249
                        'tmdb' => $show['tmdb'],
250
                    ]);
251
                // Insert the supplementary show info
252
                TvInfo::query()->insert([
253
                            'videos_id' => $videoId,
254
                            'summary' => $show['summary'],
255
                            'publisher' => $show['publisher'],
256
                            'localzone' => $show['localzone'],
257
                        ]);
258
                // If we have AKAs\aliases, insert those as well
259
                if (! empty($show['aliases'])) {
260
                    $this->addAliases($videoId, $show['aliases']);
261
                }
262
            }
263
        } else {
264
            // If a local match was found, just update missing video info
265
            $this->update($videoId, $show);
266
        }
267
268
        return $videoId;
269
    }
270
271
    /**
272
     * Inserts a new TV episode into the tv_episodes table following a match to a Video ID.
273
     *
274
     * @param $videoId
275
     * @param array $episode
276
     * @return false|int
277
     */
278
    public function addEpisode($videoId, array $episode = [])
279
    {
280
        $episodeId = $this->getBySeasonEp($videoId, $episode['series'], $episode['episode'], $episode['firstaired']);
281
282
        if ($episodeId === false) {
283
            $episodeId = TvEpisode::query()->insert(
284
                [
285
                    'videos_id' => $videoId,
286
                    'series' => $episode['series'],
287
                    'episode' => $episode['episode'],
288
                    'se_complete' => $episode['se_complete'],
289
                    'title' => $episode['title'],
290
                    'firstaired' => $episode['firstaired'] !== '' ? $episode['firstaired'] : null,
291
                    'summary' => $episode['summary'],
292
                ]
293
            );
294
        }
295
296
        return $episodeId;
297
    }
298
299
    /**
300
     * Updates the show info with data from the supplied array
301
     * Only called when a duplicate show is found during insert.
302
     *
303
     * @param int   $videoId
304
     * @param array $show
305
     */
306
    public function update($videoId, array $show = []): void
307
    {
308
        if ($show['country'] !== '') {
309
            $show['country'] = Country::countryCode($show['country']);
310
        }
311
312
        $ifStringID = 'IF(%s = 0, %s, %s)';
313
        $ifStringInfo = "IF(%s = '', %s, %s)";
314
315
        DB::update(
316
            sprintf(
317
                    '
318
				UPDATE videos v
319
				LEFT JOIN tv_info tvi ON v.id = tvi.videos_id
320
				SET v.countries_id = %s, v.tvdb = %s, v.trakt = %s, v.tvrage = %s,
321
					v.tvmaze = %s, v.imdb = %s, v.tmdb = %s,
322
					tvi.summary = %s, tvi.publisher = %s, tvi.localzone = %s
323
				WHERE v.id = %d',
324
                    sprintf($ifStringInfo, 'v.countries_id', escapeString($show['country']), 'v.countries_id'),
325
                    sprintf($ifStringID, 'v.tvdb', $show['tvdb'], 'v.tvdb'),
326
                    sprintf($ifStringID, 'v.trakt', $show['trakt'], 'v.trakt'),
327
                    sprintf($ifStringID, 'v.tvrage', $show['tvrage'], 'v.tvrage'),
328
                    sprintf($ifStringID, 'v.tvmaze', $show['tvmaze'], 'v.tvmaze'),
329
                    sprintf($ifStringID, 'v.imdb', $show['imdb'], 'v.imdb'),
330
                    sprintf($ifStringID, 'v.tmdb', $show['tmdb'], 'v.tmdb'),
331
                    sprintf($ifStringInfo, 'tvi.summary', escapeString($show['summary']), 'tvi.summary'),
332
                    sprintf($ifStringInfo, 'tvi.publisher', escapeString($show['publisher']), 'tvi.publisher'),
333
                    sprintf($ifStringInfo, 'tvi.localzone', escapeString($show['localzone']), 'tvi.localzone'),
334
                    $videoId
335
                )
336
        );
337
        if (! empty($show['aliases'])) {
338
            $this->addAliases($videoId, $show['aliases']);
339
        }
340
    }
341
342
    /**
343
     * Deletes a TV show entirely from all child tables via the Video ID.
344
     *
345
     *
346
     * @param $id
347
     *
348
     * @return mixed
349
     * @throws \Throwable
350
     */
351
    public function delete($id)
352
    {
353
        return DB::transaction(function () use ($id) {
354
            DB::delete(
355
                sprintf(
356
                    '
357
				DELETE v, tvi, tve, va
358
				FROM videos v
359
				LEFT JOIN tv_info tvi ON v.id = tvi.videos_id
360
				LEFT JOIN tv_episodes tve ON v.id = tve.videos_id
361
				LEFT JOIN videos_aliases va ON v.id = va.videos_id
362
				WHERE v.id = %d',
363
                    $id
364
                )
365
            );
366
        }, 3);
367
    }
368
369
    /**
370
     * Sets the TV show's image column to found (1).
371
     *
372
     * @param $videoId
373
     */
374
    public function setCoverFound($videoId): void
375
    {
376
        TvInfo::query()->where('videos_id', $videoId)->update(['image' => 1]);
377
    }
378
379
    /**
380
     * Get site ID from a Video ID and the site's respective column.
381
     * Returns the ID value or false if none found.
382
     *
383
     *
384
     * @param $column
385
     * @param $id
386
     * @return bool|\Illuminate\Database\Eloquent\Model|mixed|null|static
387
     */
388
    public function getSiteByID($column, $id)
389
    {
390
        $return = false;
391
        $video = Video::query()->where('id', $id)->first([$column]);
392
        if ($column === '*') {
393
            $return = $video;
394
        } elseif ($column !== '*' && $video !== null) {
395
            $return = $video[$column];
396
        }
397
398
        return $return;
399
    }
400
401
    /**
402
     * Retrieves the Episode ID using the Video ID and either:
403
     * season/episode numbers OR the airdate.
404
     *
405
     * Returns the Episode ID or false if not found
406
     *
407
     * @param        $id
408
     * @param        $series
409
     * @param        $episode
410
     * @param string $airdate
411
     *
412
     * @return int|false
413
     */
414
    public function getBySeasonEp($id, $series, $episode, $airdate = '')
415
    {
416
        if ($series > 0 && $episode > 0) {
417
            $queryString = sprintf('tve.series = %d AND tve.episode = %d', $series, $episode);
418
        } elseif (! empty($airdate)) {
419
            $queryString = sprintf('DATE(tve.firstaired) = %s', escapeString(date('Y-m-d', strtotime($airdate))));
420
        } else {
421
            return false;
422
        }
423
424
        $episodeArr = DB::selectOne(
425
            sprintf(
426
                '
427
				SELECT tve.id
428
				FROM tv_episodes tve
429
				WHERE tve.videos_id = %d
430
				AND %s',
431
                $id,
432
                $queryString
433
            )
434
        );
435
436
        return $episodeArr->id ?? false;
437
    }
438
439
    /**
440
     * Returns (true) if episodes for a given Video ID exist or don't (false).
441
     *
442
     *
443
     * @param $videoId
444
     * @return bool
445
     */
446
    public function countEpsByVideoID($videoId): bool
447
    {
448
        $count = TvEpisode::query()
449
            ->where('videos_id', $videoId)->count(['id']);
450
451
        return $count !== null && $count > 0;
452
    }
453
454
    /**
455
     * Parses a release searchname for specific TV show data
456
     * Returns an array of show data.
457
     *
458
     * @param $relname
459
     *
460
     * @return array|false
461
     */
462
    public function parseInfo($relname)
463
    {
464
        $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...
465
466
        if (! empty($showInfo['name'])) {
467
468
            // Retrieve the country from the cleaned name
469
            $showInfo['country'] = $this->parseCountry($showInfo['name']);
470
471
            // Clean show name.
472
            $showInfo['cleanname'] = preg_replace('/ - \d+$/i', '', $this->cleanName($showInfo['name']));
473
474
            // Get the Season/Episode/Airdate
475
            $showInfo += $this->parseSeasonEp($relname);
476
477
            if (isset($showInfo['season'], $showInfo['episode'])) {
478
                if (! isset($showInfo['airdate'])) {
479
                    // If year is present in the release name, add it to the cleaned name for title search
480
                    if (preg_match('/[^a-z0-9](?P<year>(19|20)(\d{2}))[^a-z0-9]/i', $relname, $yearMatch)) {
481
                        $showInfo['cleanname'] .= ' ('.$yearMatch['year'].')';
482
                    }
483
                    // Check for multi episode release.
484
                    if (\is_array($showInfo['episode'])) {
485
                        $showInfo['episode'] = $showInfo['episode'][0];
486
                    }
487
                    $showInfo['airdate'] = '';
488
                }
489
490
                return $showInfo;
491
            }
492
        }
493
494
        return false;
495
    }
496
497
    /**
498
     * Parses the release searchname and returns a show title.
499
     *
500
     * @param string $relname
501
     *
502
     * @return string
503
     */
504
    private function parseName($relname)
505
    {
506
        $showName = '';
507
508
        $following = '[^a-z0-9](\d\d-\d\d|\d{1,3}x\d{2,3}|\(?(19|20)\d{2}\)?|(480|720|1080)[ip]|AAC2?|BD-?Rip|Blu-?Ray|D0?\d'.
509
                '|DD5|DiVX|DLMux|DTS|DVD(-?Rip)?|E\d{2,3}|[HX][\-_. ]?26[45]|ITA(-ENG)?|HEVC|[HPS]DTV|PROPER|REPACK|Season|Episode|'.
510
                'S\d+[^a-z0-9]?((E\d+)[abr]?)*|WEB[\-_. ]?(DL|Rip)|XViD)[^a-z0-9]?';
511
512
        // For names that don't start with the title.
513
        if (preg_match('/^([^a-z0-9]{2,}|(sample|proof|repost)-)(?P<name>[\w .-]*?)'.$following.'/i', $relname, $matches)) {
514
            $showName = $matches['name'];
515
        } elseif (preg_match('/^(?P<name>[a-z0-9][\w\' .-]*?)'.$following.'/i', $relname, $matches)) {
516
            // For names that start with the title.
517
            $showName = $matches['name'];
518
        }
519
        // If we still have any of the words in $following, remove them.
520
        $showName = preg_replace('/'.$following.'/i', ' ', $showName);
521
        // Remove leading date if present
522
        $showName = preg_replace('/^\d{6}/', '', $showName);
523
        // Remove periods, underscored, anything between parenthesis.
524
        $showName = preg_replace('/\(.*?\)|[._]/i', ' ', $showName);
525
        // Finally remove multiple spaces and trim leading spaces.
526
        $showName = trim(preg_replace('/\s{2,}/', ' ', $showName));
527
528
        return $showName;
529
    }
530
531
    /**
532
     * Parses the release searchname for the season/episode/airdate information.
533
     *
534
     * @param $relname
535
     *
536
     * @return array
537
     */
538
    private function parseSeasonEp($relname)
539
    {
540
        $episodeArr = [];
541
542
        // S01E01-E02 and S01E01-02
543
        if (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})(?:[e-])(\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
544
            $episodeArr['season'] = (int) $matches[2];
545
            $episodeArr['episode'] = [(int) $matches[3], (int) $matches[4]];
546
        }
547
        //S01E0102 and S01E01E02 - lame no delimit numbering, regex would collide if there was ever 1000 ep season.
548
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{2})[^a-z0-9]?e(\d{2})e?(\d{2})[^a-z0-9]/i', $relname, $matches)) {
549
            $episodeArr['season'] = (int) $matches[2];
550
            $episodeArr['episode'] = (int) $matches[3];
551
        }
552
        // S01E01 and S01.E01
553
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?e(\d{1,3})[abr]?[^a-z0-9]/i', $relname, $matches)) {
554
            $episodeArr['season'] = (int) $matches[2];
555
            $episodeArr['episode'] = (int) $matches[3];
556
        }
557
        // S01
558
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
559
            $episodeArr['season'] = (int) $matches[2];
560
            $episodeArr['episode'] = 'all';
561
        }
562
        // S01D1 and S1D1
563
        elseif (preg_match('/^(.*?)[^a-z0-9]s(\d{1,2})[^a-z0-9]?d\d{1}[^a-z0-9]/i', $relname, $matches)) {
564
            $episodeArr['season'] = (int) $matches[2];
565
            $episodeArr['episode'] = 'all';
566
        }
567
        // 1x01 and 101
568
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{1,2})x(\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
569
            $episodeArr['season'] = (int) $matches[2];
570
            $episodeArr['episode'] = (int) $matches[3];
571
        }
572
        // 2009.01.01 and 2009-01-01
573
        elseif (preg_match('/^(.*?)[^a-z0-9](?P<airdate>(19|20)(\d{2})[.\/-](\d{2})[.\/-](\d{2}))[^a-z0-9]/i', $relname, $matches)) {
574
            $episodeArr['season'] = $matches[4].$matches[5];
575
            $episodeArr['episode'] = $matches[5].'/'.$matches[6];
576
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $matches['airdate']))); //yyyy-mm-dd
577
        }
578
        // 01.01.2009
579
        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, $matches)) {
580
            $episodeArr['season'] = $matches[5].$matches[6];
581
            $episodeArr['episode'] = $matches[3].'/'.$matches[4];
582
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $matches['airdate']))); //yyyy-mm-dd
583
        }
584
        // 01.01.09
585
        elseif (preg_match('/^(.*?)[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9](\d{2})[^a-z0-9]/i', $relname, $matches)) {
586
            // Add extra logic to capture the proper YYYY year
587
            $episodeArr['season'] = $matches[4] = ($matches[4] <= 99 && $matches[4] > 15) ? '19'.$matches[4] : '20'.$matches[4];
588
            $episodeArr['episode'] = $matches[2].'/'.$matches[3];
589
            $tmpAirdate = $episodeArr['season'].'/'.$episodeArr['episode'];
590
            $episodeArr['airdate'] = date('Y-m-d', strtotime(preg_replace('/[^0-9]/i', '/', $tmpAirdate))); //yyyy-mm-dd
591
        }
592
        // 2009.E01
593
        elseif (preg_match('/^(.*?)[^a-z0-9]20(\d{2})[^a-z0-9](\d{1,3})[^a-z0-9]/i', $relname, $matches)) {
594
            $episodeArr['season'] = '20'.$matches[2];
595
            $episodeArr['episode'] = (int) $matches[3];
596
        }
597
        // 2009.Part1
598
        elseif (preg_match('/^(.*?)[^a-z0-9](19|20)(\d{2})[^a-z0-9]Part(\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
599
            $episodeArr['season'] = $matches[2].$matches[3];
600
            $episodeArr['episode'] = (int) $matches[4];
601
        }
602
        // Part1/Pt1
603
        elseif (preg_match('/^(.*?)[^a-z0-9](?:Part|Pt)[^a-z0-9](\d{1,2})[^a-z0-9]/i', $relname, $matches)) {
604
            $episodeArr['season'] = 1;
605
            $episodeArr['episode'] = (int) $matches[2];
606
        }
607
608
        // Band.Of.Brothers.EP06.Bastogne.DVDRiP.XviD-DEiTY
609
        elseif (preg_match('/^(.*?)[^a-z0-9]EP?[^a-z0-9]?(\d{1,3})/i', $relname, $matches)) {
610
            $episodeArr['season'] = 1;
611
            $episodeArr['episode'] = (int) $matches[2];
612
        }
613
        // Season.1
614
        elseif (preg_match('/^(.*?)[^a-z0-9]Seasons?[^a-z0-9]?(\d{1,2})/i', $relname, $matches)) {
615
            $episodeArr['season'] = (int) $matches[2];
616
            $episodeArr['episode'] = 'all';
617
        }
618
619
        return $episodeArr;
620
    }
621
622
    /**
623
     * Parses the cleaned release name to determine if it has a country appended.
624
     *
625
     * @param string $showName
626
     *
627
     * @return string
628
     */
629
    private function parseCountry($showName): string
630
    {
631
        // Country or origin matching.
632
        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)) {
633
            $currentCountry = strtolower($countryMatch[1]);
634
            if ($currentCountry === 'canada') {
635
                $country = 'CA';
636
            } elseif ($currentCountry === 'australia') {
637
                $country = 'AU';
638
            } elseif ($currentCountry === 'america' || $currentCountry === 'united states') {
639
                $country = 'US';
640
            } elseif ($currentCountry === 'united kingdom') {
641
                $country = 'UK';
642
            } else {
643
                $country = strtoupper($countryMatch[1]);
644
            }
645
        } else {
646
            $country = '';
647
        }
648
649
        return $country;
650
    }
651
652
    /**
653
     * Supplementary to parseInfo
654
     * Cleans a derived local 'showname' for better matching probability
655
     * Returns the cleaned string.
656
     *
657
     * @param $str
658
     *
659
     * @return string
660
     */
661
    public function cleanName($str): string
662
    {
663
        $str = str_replace(['.', '_'], ' ', $str);
664
665
        $str = str_replace(['à', 'á', 'â', 'ã', 'ä', 'æ', 'À', 'Á', 'Â', 'Ã', 'Ä'], 'a', $str);
666
        $str = str_replace(['ç', 'Ç'], 'c', $str);
667
        $str = str_replace(['Σ', 'è', 'é', 'ê', 'ë', 'È', 'É', 'Ê', 'Ë'], 'e', $str);
668
        $str = str_replace(['ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï'], 'i', $str);
669
        $str = str_replace(['ò', 'ó', 'ô', 'õ', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö'], 'o', $str);
670
        $str = str_replace(['ù', 'ú', 'û', 'ü', 'ū', 'Ú', 'Û', 'Ü', 'Ū'], 'u', $str);
671
        $str = str_replace('ß', 'ss', $str);
672
673
        $str = str_replace('&', 'and', $str);
674
        $str = preg_replace('/^(history|discovery) channel/i', '', $str);
675
        $str = str_replace(['\'', ':', '!', '"', '#', '*', '’', ',', '(', ')', '?'], '', $str);
676
        $str = str_replace('$', 's', $str);
677
        $str = preg_replace('/\s{2,}/', ' ', $str);
678
679
        $str = trim($str, '\"');
680
681
        return trim($str);
682
    }
683
684
    /**
685
     * Simple function that compares two strings of text
686
     * Returns percentage of similarity.
687
     *
688
     * @param $ourName
689
     * @param $scrapeName
690
     * @param $probability
691
     *
692
     * @return int|float
693
     */
694
    public function checkMatch($ourName, $scrapeName, $probability)
695
    {
696
        similar_text($ourName, $scrapeName, $matchpct);
697
698
        if ($matchpct >= $probability) {
699
            return $matchpct;
700
        }
701
702
        return 0;
703
    }
704
705
    //
706
707
    /**
708
     * Convert 2012-24-07 to 2012-07-24, there is probably a better way.
709
     *
710
     * This shouldn't ever happen as I've never heard of a date starting with year being followed by day value.
711
     * Could this be a mistake? i.e. trying to solve the mm-dd-yyyy/dd-mm-yyyy confusion into a yyyy-mm-dd?
712
     *
713
     * @param string|bool|null $date
714
     *
715
     * @return string
716
     */
717
    public function checkDate($date): string
718
    {
719
        if (! empty($date)) {
720
            $chk = explode(' ', $date);
721
            $chkd = explode('-', $chk[0]);
722
            if ($chkd[1] > 12) {
723
                $date = date('Y-m-d H:i:s', strtotime($chkd[1].' '.$chkd[2].' '.$chkd[0]));
724
            }
725
        } else {
726
            $date = null;
727
        }
728
729
        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...
730
    }
731
732
    /**
733
     * Checks API response returns have all REQUIRED attributes set
734
     * Returns true or false.
735
     *
736
     * @param $array
737
     * @param int $type
738
     *
739
     * @return bool
740
     */
741
    public function checkRequiredAttr($array, $type): bool
742
    {
743
        $required = ['failedToMatchType'];
744
745
        switch ($type) {
746
            case 'tvdbS':
747
                $required = ['id', 'seriesName', 'overview', 'firstAired'];
748
                break;
749
            case 'tvdbE':
750
                $required = ['episodeName', 'airedSeason', 'airedEpisode', 'firstAired', 'overview'];
751
                break;
752
            case 'tvmazeS':
753
                $required = ['id', 'name', 'summary', 'premiered', 'country'];
754
                break;
755
            case 'tvmazeE':
756
                $required = ['name', 'season', 'number', 'airdate', 'summary'];
757
                break;
758
            case 'tmdbS':
759
                $required = ['id', 'original_name', 'overview', 'first_air_date', 'origin_country'];
760
                break;
761
            case 'tmdbE':
762
                $required = ['name', 'season_number', 'episode_number', 'air_date', 'overview'];
763
                break;
764
            case 'traktS':
765
                $required = ['title', 'ids', 'overview', 'first_aired', 'airs', 'country'];
766
                break;
767
            case 'traktE':
768
                $required = ['title', 'season', 'episode', 'overview', 'first_aired'];
769
                break;
770
        }
771
772
        if (\is_array($required)) {
0 ignored issues
show
introduced by
The condition is_array($required) is always true.
Loading history...
773
            foreach ($required as $req) {
774
                if (! \in_array($type, ['tmdbS', 'tmdbE', 'traktS', 'traktE'], false)) {
775
                    if (! isset($array->$req)) {
776
                        return false;
777
                    }
778
                } elseif (! isset($array[$req])) {
779
                    return false;
780
                }
781
            }
782
        }
783
784
        return true;
785
    }
786
}
787