Passed
Push — master ( 0af2a4...e70dbc )
by Darko
07:18
created

Movie::getMovieRange()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 81
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 5 Features 0
Metric Value
eloc 43
c 10
b 5
f 0
dl 0
loc 81
rs 9.232
cc 4
nc 4
nop 7

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 Blacklight;
4
5
use aharen\OMDbAPI;
6
use App\Models\Category;
7
use App\Models\MovieInfo;
8
use App\Models\Release;
9
use App\Models\Settings;
10
use Blacklight\libraries\FanartTV;
11
use Blacklight\processing\tv\TraktTv;
12
use Blacklight\utility\Utility;
13
use DariusIII\ItunesApi\Exceptions\InvalidProviderException;
14
use DariusIII\ItunesApi\Exceptions\MovieNotFoundException;
15
use DariusIII\ItunesApi\Exceptions\SearchNoResultsException;
16
use DariusIII\ItunesApi\iTunes;
17
use GuzzleHttp\Client;
18
use GuzzleHttp\Exception\GuzzleException;
19
use Illuminate\Database\Eloquent\Builder;
20
use Illuminate\Database\Eloquent\Model;
21
use Illuminate\Support\Arr;
22
use Illuminate\Support\Carbon;
23
use Illuminate\Support\Facades\Cache;
24
use Illuminate\Support\Facades\DB;
25
use Illuminate\Support\Facades\File;
26
use Illuminate\Support\Facades\Log;
27
use Illuminate\Support\Str;
28
use Imdb\Config;
29
use Imdb\Title;
30
use Imdb\TitleSearch;
31
use Tmdb\Exception\TmdbApiException;
32
use Tmdb\Laravel\Facades\Tmdb;
33
34
/**
35
 * Class Movie.
36
 */
37
class Movie
38
{
39
    /**
40
     * @var int
41
     */
42
    protected const MATCH_PERCENT = 75;
43
44
    /**
45
     * @var int
46
     */
47
    protected const YEAR_MATCH_PERCENT = 80;
48
49
    /**
50
     * Current title being passed through various sites/api's.
51
     */
52
    protected string $currentTitle = '';
53
54
    /**
55
     * Current year of parsed search name.
56
     */
57
    protected string $currentYear = '';
58
59
    /**
60
     * Current release id of parsed search name.
61
     */
62
    protected string $currentRelID = '';
63
64
    protected string $showPasswords;
65
66
    protected ReleaseImage $releaseImage;
67
68
    protected Client $client;
69
70
    /**
71
     * Language to fetch from IMDB.
72
     */
73
    protected string $lookuplanguage;
74
75
    public FanartTV $fanart;
76
77
    /**
78
     * @var null|string
79
     */
80
    public mixed $fanartapikey;
81
82
    /**
83
     * @var null|string
84
     */
85
    public mixed $omdbapikey;
86
87
    /**
88
     * @var bool
89
     */
90
    public $imdburl;
91
92
    public int $movieqty;
93
94
    public bool $echooutput;
95
96
    public string $imgSavePath;
97
98
    public string $service;
99
100
    public TraktTv $traktTv;
101
102
    public ?OMDbAPI $omdbApi;
103
104
    private Config $config;
105
106
    /**
107
     * @var null|string
108
     */
109
    protected mixed $traktcheck;
110
111
    protected ColorCLI $colorCli;
112
113
    /**
114
     * @throws \Exception
115
     */
116
    public function __construct()
117
    {
118
119
        $this->releaseImage = new ReleaseImage;
120
        $this->colorCli = new ColorCLI;
121
        $this->traktcheck = config('nntmux_api.trakttv_api_key');
122
        if ($this->traktcheck !== null) {
123
            $this->traktTv = new TraktTv(['Settings' => null]);
0 ignored issues
show
Unused Code introduced by
The call to Blacklight\processing\tv\TraktTv::__construct() has too many arguments starting with array('Settings' => null). ( Ignorable by Annotation )

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

123
            $this->traktTv = /** @scrutinizer ignore-call */ new TraktTv(['Settings' => null]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
124
        }
125
        $this->client = new Client;
126
        $this->fanartapikey = config('nntmux_api.fanarttv_api_key');
127
        if ($this->fanartapikey !== null) {
128
            $this->fanart = new FanartTV($this->fanartapikey);
129
        }
130
        $this->omdbapikey = config('nntmux_api.omdb_api_key');
131
        if ($this->omdbapikey !== null) {
132
            $this->omdbApi = new OMDbAPI($this->omdbapikey);
133
        }
134
135
        $this->lookuplanguage = Settings::settingValue('indexer.categorise.imdblanguage') !== '' ? (string) Settings::settingValue('indexer.categorise.imdblanguage') : 'en';
0 ignored issues
show
introduced by
The condition App\Models\Settings::set...e.imdblanguage') !== '' is always true.
Loading history...
136
        $this->config = new Config;
137
        $this->config->language = $this->lookuplanguage;
138
        $this->config->throwHttpExceptions = false;
139
        $cacheDir = storage_path('framework/cache/imdb_cache');
140
        if (! File::isDirectory($cacheDir)) {
141
            File::makeDirectory($cacheDir, 0777, false, true);
142
        }
143
        $this->config->cachedir = $cacheDir;
144
145
        $this->imdburl = (int) Settings::settingValue('indexer.categorise.imdburl') !== 0;
146
        $this->movieqty = Settings::settingValue('..maximdbprocessed') !== '' ? (int) Settings::settingValue('..maximdbprocessed') : 100;
0 ignored issues
show
introduced by
The condition App\Models\Settings::set...ximdbprocessed') !== '' is always true.
Loading history...
147
        $this->showPasswords = (new Releases)->showPasswords();
148
149
        $this->echooutput = config('nntmux.echocli');
150
        $this->imgSavePath = storage_path('covers/movies/');
151
        $this->service = '';
152
    }
153
154
    /**
155
     * @return Builder|Model|null|object
156
     */
157
    public function getMovieInfo($imdbId)
158
    {
159
        return MovieInfo::query()->where('imdbid', $imdbId)->first();
160
    }
161
162
    /**
163
     * Get movie releases with covers for movie browse page.
164
     *
165
     *
166
     * @return array|mixed
167
     */
168
    public function getMovieRange($page, $cat, $start, $num, $orderBy, int $maxAge = -1, array $excludedCats = []): mixed
169
    {
170
        $categorySearch = $this->buildCategorySearch($cat);
171
172
        $order = $this->getMovieOrder($orderBy);
173
        $cacheKey = md5(json_encode([$cat, $start, $num, $orderBy, $maxAge, $excludedCats, $page]));
174
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
0 ignored issues
show
Unused Code introduced by
The assignment to $expiresAt is dead and can be removed.
Loading history...
175
176
        // Try to get from cache
177
        $cachedMovies = Cache::get($cacheKey);
178
        if ($cachedMovies) {
179
            return $cachedMovies;
180
        }
181
182
        $moviesQuery = MovieInfo::query()
183
            ->selectRaw('movieinfo.imdbid, GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_id')
184
            ->join('releases as r', 'movieinfo.imdbid', '=', 'r.imdbid')
185
            ->where('r.nzbstatus', 1)
186
            ->where('movieinfo.title', '!=', '')
187
            ->where('movieinfo.imdbid', '!=', '0000000')
188
            ->when($maxAge > 0, fn ($query) => $query->whereRaw('r.postdate > NOW() - INTERVAL ? DAY', [$maxAge]))
189
            ->when(! empty($excludedCats), fn ($query) => $query->whereNotIn('r.categories_id', $excludedCats))
190
            ->when(! empty($categorySearch), fn ($query) => $query->whereRaw($categorySearch))
191
            ->groupBy('movieinfo.imdbid')
192
            ->orderBy($order[0], $order[1])
193
            ->offset($start)
194
            ->limit($num);
195
196
        $movies = $moviesQuery->get();
197
        $total = DB::select('SELECT FOUND_ROWS() AS total');
198
199
        $movieIDs = $movies->pluck('imdbid')->toArray();
200
        $releaseIDs = $movies->pluck('grp_release_id')->toArray();
201
202
        if (! empty($movieIDs)) {
203
            $moviesDetailQuery = Release::query()
204
                ->selectRaw('
205
                GROUP_CONCAT(r.id ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_id,
206
                GROUP_CONCAT(r.rarinnerfilecount ORDER BY r.postdate DESC SEPARATOR ",") AS grp_rarinnerfilecount,
207
                GROUP_CONCAT(r.haspreview ORDER BY r.postdate DESC SEPARATOR ",") AS grp_haspreview,
208
                GROUP_CONCAT(r.passwordstatus ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_password,
209
                GROUP_CONCAT(r.guid ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_guid,
210
                GROUP_CONCAT(rn.releases_id ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_nfoid,
211
                GROUP_CONCAT(g.name ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_grpname,
212
                GROUP_CONCAT(r.searchname ORDER BY r.postdate DESC SEPARATOR "#") AS grp_release_name,
213
                GROUP_CONCAT(r.postdate ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_postdate,
214
                GROUP_CONCAT(r.size ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_size,
215
                GROUP_CONCAT(r.totalpart ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_totalparts,
216
                GROUP_CONCAT(r.comments ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_comments,
217
                GROUP_CONCAT(r.grabs ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_grabs,
218
                GROUP_CONCAT(df.failed ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_failed,
219
                GROUP_CONCAT(CONCAT(cp.title, " > ", c.title) ORDER BY r.postdate DESC SEPARATOR ",") AS grp_release_catname,
220
                m.*,
221
                g.name AS group_name,
222
                rn.releases_id AS nfoid
223
            ')
224
                ->leftJoin('usenet_groups as g', 'g.id', '=', 'r.groups_id')
225
                ->leftJoin('release_nfos as rn', 'rn.releases_id', '=', 'r.id')
226
                ->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'r.id')
227
                ->leftJoin('categories as c', 'c.id', '=', 'r.categories_id')
228
                ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
229
                ->join('movieinfo as m', 'm.imdbid', '=', 'r.imdbid')
230
                ->whereIn('m.imdbid', $movieIDs)
231
                ->whereIn('r.id', $releaseIDs)
232
                ->when(! empty($categorySearch), fn ($query) => $query->whereRaw($categorySearch))
233
                ->groupBy('m.imdbid')
234
                ->orderBy($order[0], $order[1])
235
                ->get();
236
237
            // Add total count to the first record if available
238
            if ($moviesDetailQuery->count() > 0) {
239
                $moviesDetailQuery[0]->_totalcount = $total[0]->total ?? 0;
240
            }
241
242
            // Cache the result
243
            //Cache::put($cacheKey, $moviesDetailQuery, $expiresAt);
244
245
            return $moviesDetailQuery;
246
        }
247
248
        return collect();
249
    }
250
251
    protected function buildCategorySearch($cat): string
252
    {
253
        if (\count($cat) > 0 && $cat[0] !== -1) {
254
            return Category::getCategorySearch($cat);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Catego...getCategorySearch($cat) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
255
        }
256
257
        return '';
258
    }
259
260
    /**
261
     * Get the order type the user requested on the movies page.
262
     */
263
    protected function getMovieOrder($orderBy): array
264
    {
265
        $orderArr = explode('_', (($orderBy === '') ? 'MAX(r.postdate)' : $orderBy));
266
        $orderField = match ($orderArr[0]) {
267
            'title' => 'm.title',
268
            'year' => 'm.year',
269
            'rating' => 'm.rating',
270
            default => DB::raw('MAX(r.postdate)'),
271
        };
272
273
        return [$orderField, isset($orderArr[1]) && preg_match('/^asc|desc$/i', $orderArr[1]) ? $orderArr[1] : 'desc'];
274
    }
275
276
    /**
277
     * Order types for movies page.
278
     */
279
    public function getMovieOrdering(): array
280
    {
281
        return ['title_asc', 'title_desc', 'year_asc', 'year_desc', 'rating_asc', 'rating_desc'];
282
    }
283
284
    protected function getBrowseBy(): string
285
    {
286
        $browseBy = ' ';
287
        $browseByArr = ['title', 'director', 'actors', 'genre', 'rating', 'year', 'imdb'];
288
        foreach ($browseByArr as $bb) {
289
            if (request()->has($bb) && ! empty(request()->input($bb))) {
290
                $bbv = stripslashes(request()->input($bb));
291
                if ($bb === 'rating') {
292
                    $bbv .= '.';
293
                }
294
                if ($bb === 'imdb') {
295
                    $browseBy .= sprintf(' AND m.imdbid = %d', $bbv);
296
                } else {
297
                    $browseBy .= ' AND m.'.$bb.' '.'LIKE '.escapeString('%'.$bbv.'%');
298
                }
299
            }
300
        }
301
302
        return $browseBy;
303
    }
304
305
    /**
306
     * Get trailer using IMDB Id.
307
     *
308
     * @return bool|string
309
     *
310
     * @throws \Exception
311
     * @throws GuzzleException
312
     */
313
    public function getTrailer(int $imdbId)
314
    {
315
        $trailer = MovieInfo::query()->where('imdbid', $imdbId)->where('trailer', '<>', '')->first(['trailer']);
316
        if ($trailer !== null) {
317
            return $trailer['trailer'];
318
        }
319
320
        if ($this->traktcheck !== null) {
321
            $data = $this->traktTv->client->movieSummary('tt'.$imdbId, 'full');
322
            if (($data !== false) && ! empty($data['trailer'])) {
323
                return $data['trailer'];
324
            }
325
        }
326
327
        $trailer = Utility::imdb_trailers($imdbId);
328
        if ($trailer) {
329
            MovieInfo::query()->where('imdbid', $imdbId)->update(['trailer' => $trailer]);
330
331
            return $trailer;
332
        }
333
334
        return false;
335
    }
336
337
    /**
338
     * Parse trakt info, insert into DB.
339
     *
340
     * @return mixed
341
     */
342
    public function parseTraktTv(array &$data)
343
    {
344
        if (empty($data['ids']['imdb'])) {
345
            return false;
346
        }
347
348
        if (! empty($data['trailer'])) {
349
            $data['trailer'] = str_ireplace(
350
                ['watch?v=', 'http://'],
351
                ['embed/', 'https://'],
352
                $data['trailer']
353
            );
354
        }
355
        $imdbId = (str_starts_with($data['ids']['imdb'], 'tt')) ? substr($data['ids']['imdb'], 2) : $data['ids']['imdb'];
356
        $cover = 0;
357
        if (File::isFile($this->imgSavePath.$imdbId).'-cover.jpg') {
358
            $cover = 1;
359
        }
360
361
        return $this->update([
362
            'genre' => implode(', ', $data['genres']),
363
            'imdbid' => $this->checkTraktValue($imdbId),
364
            'language' => $this->checkTraktValue($data['language']),
365
            'plot' => $this->checkTraktValue($data['overview']),
366
            'rating' => $this->checkTraktValue($data['rating']),
367
            'tagline' => $this->checkTraktValue($data['tagline']),
368
            'title' => $this->checkTraktValue($data['title']),
369
            'tmdbid' => $this->checkTraktValue($data['ids']['tmdb']),
370
            'traktid' => $this->checkTraktValue($data['ids']['trakt']),
371
            'trailer' => $this->checkTraktValue($data['trailer']),
372
            'cover' => $cover,
373
            'year' => $this->checkTraktValue($data['year']),
374
        ]);
375
    }
376
377
    /**
378
     * @return mixed|string
379
     */
380
    private function checkTraktValue($value)
381
    {
382
        if (\is_array($value) && ! empty($value)) {
383
            $temp = '';
384
            foreach ($value as $val) {
385
                if (! \is_array($val) && ! \is_object($val)) {
386
                    $temp .= $val;
387
                }
388
            }
389
            $value = $temp;
390
        }
391
392
        return ! empty($value) ? $value : '';
393
    }
394
395
    /**
396
     * Get array of column keys, for inserting / updating.
397
     */
398
    public function getColumnKeys(): array
399
    {
400
        return [
401
            'actors', 'backdrop', 'cover', 'director', 'genre', 'imdbid', 'language',
402
            'plot', 'rating', 'rtrating', 'tagline', 'title', 'tmdbid', 'traktid', 'trailer', 'type', 'year',
403
        ];
404
    }
405
406
    /**
407
     * Update movie on movie-edit page.
408
     *
409
     * @param  array  $values  Array of keys/values to update. See $validKeys
410
     */
411
    public function update(array $values): bool
412
    {
413
        if (! \count($values)) {
414
            return false;
415
        }
416
417
        $query = [];
418
        $onDuplicateKey = ['created_at' => now()];
419
        $found = 0;
420
        foreach ($values as $key => $value) {
421
            if (! empty($value)) {
422
                $found++;
423
                if (\in_array($key, ['genre', 'language'], false)) {
424
                    $value = substr($value, 0, 64);
425
                }
426
                $query += [$key => $value];
427
                $onDuplicateKey += [$key => $value];
428
            }
429
        }
430
        if (! $found) {
431
            return false;
432
        }
433
        foreach ($query as $key => $value) {
434
            $query[$key] = rtrim($value, ', ');
435
        }
436
437
        MovieInfo::upsert($query, ['imdbid'], $onDuplicateKey);
438
439
        return true;
440
    }
441
442
    /**
443
     * @return array|string
444
     */
445
    protected function setVariables(string|array $variable1, string|array $variable2, string|array $variable3, string|array $variable4, string|array $variable5)
446
    {
447
        if (! empty($variable1)) {
448
            return $variable1;
449
        }
450
        if (! empty($variable2)) {
451
            return $variable2;
452
        }
453
        if (! empty($variable3)) {
454
            return $variable3;
455
        }
456
        if (! empty($variable4)) {
457
            return $variable4;
458
        }
459
        if (! empty($variable5)) {
460
            return $variable5;
461
        }
462
463
        return '';
464
    }
465
466
    /**
467
     * Fetch IMDB/TMDB/TRAKT/OMDB/iTunes info for the movie.
468
     *
469
     *
470
     * @throws \Exception
471
     */
472
    public function updateMovieInfo($imdbId): bool
473
    {
474
        if ($this->echooutput && $this->service !== '') {
475
            $this->colorCli->primary('Fetching IMDB info from TMDB/IMDB/Trakt/OMDB/iTunes using IMDB id: '.$imdbId);
476
        }
477
478
        // Check TMDB for IMDB info.
479
        $tmdb = $this->fetchTMDBProperties($imdbId);
480
481
        // Check IMDB for movie info.
482
        $imdb = $this->fetchIMDBProperties($imdbId);
483
484
        // Check TRAKT for movie info
485
        $trakt = $this->fetchTraktTVProperties($imdbId);
486
487
        // Check OMDb for movie info
488
        $omdb = $this->fetchOmdbAPIProperties($imdbId);
489
490
        // Check iTunes for movie info as last resort (iTunes do not provide all the info we need)
491
492
        $iTunes = $this->fetchItunesMovieProperties($this->currentTitle);
493
494
        if (! $imdb && ! $tmdb && ! $trakt && ! $omdb && empty($iTunes)) {
495
            return false;
496
        }
497
498
        // Check FanArt.tv for cover and background images.
499
        $fanart = $this->fetchFanartTVProperties($imdbId);
500
501
        $mov = [];
502
503
        $mov['cover'] = $mov['backdrop'] = $mov['banner'] = 0;
504
        $mov['type'] = $mov['director'] = $mov['actors'] = $mov['language'] = '';
505
506
        $mov['imdbid'] = $imdbId;
507
        $mov['tmdbid'] = (! isset($tmdb['tmdbid']) || $tmdb['tmdbid'] === '') ? 0 : $tmdb['tmdbid'];
508
        $mov['traktid'] = (! isset($trakt['id']) || $trakt['id'] === '') ? 0 : $trakt['id'];
509
510
        // Prefer Fanart.tv cover over TMDB,TMDB over IMDB,IMDB over OMDB and OMDB over iTunes.
511
        if (! empty($fanart['cover'])) {
512
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', $fanart['cover'], $this->imgSavePath);
513
        } elseif (! empty($tmdb['cover'])) {
514
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', $tmdb['cover'], $this->imgSavePath);
515
        } elseif (! empty($imdb['cover'])) {
516
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', $imdb['cover'], $this->imgSavePath);
0 ignored issues
show
Bug introduced by
It seems like $imdb['cover'] can also be of type false; however, parameter $imgLoc of Blacklight\ReleaseImage::saveImage() 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

516
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', /** @scrutinizer ignore-type */ $imdb['cover'], $this->imgSavePath);
Loading history...
517
        } elseif (! empty($omdb['cover'])) {
518
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', $omdb['cover'], $this->imgSavePath);
519
        } elseif (! empty($iTunes['cover'])) {
520
            $mov['cover'] = $this->releaseImage->saveImage($imdbId.'-cover', $iTunes['cover'], $this->imgSavePath);
521
        }
522
523
        // Backdrops.
524
        if (! empty($fanart['backdrop'])) {
525
            $mov['backdrop'] = $this->releaseImage->saveImage($imdbId.'-backdrop', $fanart['backdrop'], $this->imgSavePath, 1920, 1024);
526
        } elseif (! empty($tmdb['backdrop'])) {
527
            $mov['backdrop'] = $this->releaseImage->saveImage($imdbId.'-backdrop', $tmdb['backdrop'], $this->imgSavePath, 1920, 1024);
528
        }
529
530
        // Banner
531
        if (! empty($fanart['banner'])) {
532
            $mov['banner'] = $this->releaseImage->saveImage($imdbId.'-banner', $fanart['banner'], $this->imgSavePath);
533
        }
534
535
        // RottenTomatoes rating from OmdbAPI
536
        if ($omdb !== false && ! empty($omdb['rtRating'])) {
537
            $mov['rtrating'] = $omdb['rtRating'];
538
        }
539
540
        $mov['title'] = $this->setVariables($imdb['title'] ?? '', $tmdb['title'] ?? '', $trakt['title'] ?? '', $omdb['title'] ?? '', $iTunes['title'] ?? '');
541
        $mov['rating'] = $this->setVariables($imdb['rating'] ?? '', $tmdb['rating'] ?? '', $trakt['rating'] ?? '', $omdb['rating'] ?? '', $iTunes['rating'] ?? '');
542
        $mov['plot'] = $this->setVariables($imdb['plot'] ?? '', $tmdb['plot'] ?? '', $trakt['overview'] ?? '', $omdb['plot'] ?? '', $iTunes['plot'] ?? '');
543
        $mov['tagline'] = $this->setVariables($imdb['tagline'] ?? '', $tmdb['tagline'] ?? '', $trakt['tagline'] ?? '', $omdb['tagline'] ?? '', $iTunes['tagline'] ?? '');
544
        $mov['year'] = $this->setVariables($imdb['year'] ?? '', $tmdb['year'] ?? '', $trakt['year'] ?? '', $omdb['year'] ?? '', $iTunes['year'] ?? '');
545
        $mov['genre'] = $this->setVariables($imdb['genre'] ?? '', $tmdb['genre'] ?? '', $trakt['genres'] ?? '', $omdb['genre'] ?? '', $iTunes['genre'] ?? '');
546
547
        if (! empty($imdb['type'])) {
548
            $mov['type'] = $imdb['type'];
549
        }
550
551
        if (! empty($imdb['director'])) {
552
            $mov['director'] = \is_array($imdb['director']) ? implode(', ', array_unique($imdb['director'])) : $imdb['director'];
553
        } elseif (! empty($omdb['director'])) {
554
            $mov['director'] = \is_array($omdb['director']) ? implode(', ', array_unique($omdb['director'])) : $omdb['director'];
555
        } elseif (! empty($tmdb['director'])) {
556
            $mov['director'] = \is_array($tmdb['director']) ? implode(', ', array_unique($tmdb['director'])) : $tmdb['director'];
557
        }
558
559
        if (! empty($imdb['actors'])) {
560
            $mov['actors'] = \is_array($imdb['actors']) ? implode(', ', array_unique($imdb['actors'])) : $imdb['actors'];
561
        } elseif (! empty($omdb['actors'])) {
562
            $mov['actors'] = \is_array($omdb['actors']) ? implode(', ', array_unique($omdb['actors'])) : $omdb['actors'];
563
        } elseif (! empty($tmdb['actors'])) {
564
            $mov['actors'] = \is_array($tmdb['actors']) ? implode(', ', array_unique($tmdb['actors'])) : $tmdb['actors'];
565
        }
566
567
        if (! empty($imdb['language'])) {
568
            $mov['language'] = \is_array($imdb['language']) ? implode(', ', array_unique($imdb['language'])) : $imdb['language'];
0 ignored issues
show
introduced by
The condition is_array($imdb['language']) is always false.
Loading history...
569
        } elseif (! empty($omdb['language']) && ! is_bool($omdb['language'])) {
570
            $mov['language'] = \is_array($omdb['language']) ? implode(', ', array_unique($omdb['language'])) : $omdb['language'];
571
        }
572
573
        if (\is_array($mov['genre'])) {
574
            $mov['genre'] = implode(', ', array_unique($mov['genre']));
575
        }
576
577
        if (\is_array($mov['type'])) {
578
            $mov['type'] = implode(', ', array_unique($mov['type']));
579
        }
580
581
        $mov['title'] = html_entity_decode($mov['title'], ENT_QUOTES, 'UTF-8');
582
583
        $mov['title'] = str_replace(['/', '\\'], '', $mov['title']);
584
        $movieID = $this->update([
585
            'actors' => html_entity_decode($mov['actors'], ENT_QUOTES, 'UTF-8'),
586
            'backdrop' => $mov['backdrop'],
587
            'cover' => $mov['cover'],
588
            'director' => html_entity_decode($mov['director'], ENT_QUOTES, 'UTF-8'),
589
            'genre' => html_entity_decode($mov['genre'], ENT_QUOTES, 'UTF-8'),
590
            'imdbid' => $mov['imdbid'],
591
            'language' => html_entity_decode($mov['language'], ENT_QUOTES, 'UTF-8'),
592
            'plot' => html_entity_decode(preg_replace('/\s+See full summary »/u', ' ', $mov['plot']), ENT_QUOTES, 'UTF-8'),
593
            'rating' => round((int) $mov['rating'], 1),
594
            'rtrating' => $mov['rtrating'] ?? 'N/A',
595
            'tagline' => html_entity_decode($mov['tagline'], ENT_QUOTES, 'UTF-8'),
596
            'title' => $mov['title'],
597
            'tmdbid' => $mov['tmdbid'],
598
            'traktid' => $mov['traktid'],
599
            'type' => html_entity_decode(ucwords(preg_replace('/[\.\_]/', ' ', $mov['type'])), ENT_QUOTES, 'UTF-8'),
600
            'year' => $mov['year'],
601
        ]);
602
603
        if ($this->echooutput && $this->service !== '') {
604
            PHP_EOL.$this->colorCli->headerOver('Added/updated movie: ').
0 ignored issues
show
Bug introduced by
Are you sure $this->colorCli->headerO...Added/updated movie: ') of type void can be used in concatenation? ( Ignorable by Annotation )

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

604
            PHP_EOL./** @scrutinizer ignore-type */ $this->colorCli->headerOver('Added/updated movie: ').
Loading history...
Bug introduced by
Are you sure the usage of $this->colorCli->headerO...Added/updated movie: ') targeting Blacklight\ColorCLI::headerOver() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
605
            $this->colorCli->primary(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->colorCli->primary...) - ' . $mov['imdbid']) targeting Blacklight\ColorCLI::primary() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure $this->colorCli->primary...) - ' . $mov['imdbid']) of type void can be used in concatenation? ( Ignorable by Annotation )

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

605
            /** @scrutinizer ignore-type */ $this->colorCli->primary(
Loading history...
606
                $mov['title'].
607
                ' ('.
608
                $mov['year'].
609
                ') - '.
610
                $mov['imdbid']
611
            );
612
        }
613
614
        return $movieID;
615
    }
616
617
    /**
618
     * Fetch FanArt.tv backdrop / cover / title.
619
     *
620
     * @return array|false
621
     */
622
    protected function fetchFanartTVProperties($imdbId)
623
    {
624
        if ($this->fanartapikey !== null) {
625
            $art = $this->fanart->getMovieFanArt('tt'.$imdbId);
626
627
            if (! empty($art)) {
628
                if (isset($art['status']) && $art['status'] === 'error') {
629
                    return false;
630
                }
631
                $ret = [];
632
                if (! empty($art['moviebackground'][0]['url'])) {
633
                    $ret['backdrop'] = $art['moviebackground'][0]['url'];
634
                } elseif (! empty($art['moviethumb'][0]['url'])) {
635
                    $ret['backdrop'] = $art['moviethumb'][0]['url'];
636
                }
637
                if (! empty($art['movieposter'][0]['url'])) {
638
                    $ret['cover'] = $art['movieposter'][0]['url'];
639
                }
640
                if (! empty($art['moviebanner'][0]['url'])) {
641
                    $ret['banner'] = $art['moviebanner'][0]['url'];
642
                }
643
644
                if (isset($ret['backdrop'], $ret['cover'])) {
645
                    $ret['title'] = $imdbId;
646
                    if (isset($art['name'])) {
647
                        $ret['title'] = $art['name'];
648
                    }
649
                    if ($this->echooutput) {
650
                        $this->colorCli->climate()->info('Fanart found '.$ret['title']);
651
                    }
652
653
                    return $ret;
654
                }
655
            }
656
        }
657
658
        return false;
659
    }
660
661
    /**
662
     * Fetch info for IMDB id from TMDB.
663
     *
664
     *
665
     * @return array|false
666
     */
667
    public function fetchTMDBProperties($imdbId, bool $text = false)
668
    {
669
        $lookupId = $text === false && (\strlen($imdbId) === 7 || strlen($imdbId) === 8) ? 'tt'.$imdbId : $imdbId;
670
671
        try {
672
            $tmdbLookup = Tmdb::getMoviesApi()->getMovie($lookupId, ['append_to_response' => 'credits']);
673
        } catch (TmdbApiException|\ErrorException $error) {
674
            return false;
675
        }
676
677
        if (! empty($tmdbLookup)) {
678
            if ($this->currentTitle !== '') {
679
                // Check the similarity.
680
                similar_text($this->currentTitle, $tmdbLookup['title'], $percent);
681
                if ($percent < self::MATCH_PERCENT) {
682
                    return false;
683
                }
684
            }
685
686
            if ($this->currentYear !== '') {
687
                // Check the similarity.
688
                similar_text($this->currentYear, Carbon::parse($tmdbLookup['release_date'])->year, $percent);
689
                if ($percent < self::YEAR_MATCH_PERCENT) {
690
                    return false;
691
                }
692
            }
693
694
            $ret = [];
695
            $ret['title'] = $tmdbLookup['title'];
696
697
            $ret['tmdbid'] = $tmdbLookup['id'];
698
            $ret['imdbid'] = str_replace('tt', '', $tmdbLookup['imdb_id']);
699
            $vote = $tmdbLookup['vote_average'];
700
            if ($vote !== null) {
701
                $ret['rating'] = (int) $vote === 0 ? '' : $vote;
702
            } else {
703
                $ret['rating'] = '';
704
            }
705
            $actors = Arr::pluck($tmdbLookup['credits']['cast'], 'name');
706
            if (! empty($actors)) {
707
                $ret['actors'] = $actors;
708
            } else {
709
                $ret['actors'] = '';
710
            }
711
            foreach ($tmdbLookup['credits']['crew'] as $crew) {
712
                if ($crew['department'] === 'Directing' && $crew['job'] === 'Director') {
713
                    $ret['director'] = $crew['name'];
714
                }
715
            }
716
            $overview = $tmdbLookup['overview'];
717
            if (! empty($overview)) {
718
                $ret['plot'] = $overview;
719
            } else {
720
                $ret['plot'] = '';
721
            }
722
            $tagline = $tmdbLookup['tagline'];
723
724
            $ret['tagline'] = $tagline ?? '';
725
726
            $released = $tmdbLookup['release_date'];
727
            if (! empty($released)) {
728
                $ret['year'] = Carbon::parse($released)->year;
729
            } else {
730
                $ret['year'] = '';
731
            }
732
            $genresa = $tmdbLookup['genres'];
733
            if (! empty($genresa) && \count($genresa) > 0) {
734
                $genres = [];
735
                foreach ($genresa as $genre) {
736
                    $genres[] = $genre['name'];
737
                }
738
                $ret['genre'] = $genres;
739
            } else {
740
                $ret['genre'] = '';
741
            }
742
            $posterp = $tmdbLookup['poster_path'];
743
            if (! empty($posterp)) {
744
                $ret['cover'] = 'https://image.tmdb.org/t/p/original'.$posterp;
745
            } else {
746
                $ret['cover'] = '';
747
            }
748
            $backdrop = $tmdbLookup['backdrop_path'];
749
            if (! empty($backdrop)) {
750
                $ret['backdrop'] = 'https://image.tmdb.org/t/p/original'.$backdrop;
751
            } else {
752
                $ret['backdrop'] = '';
753
            }
754
            if ($this->echooutput) {
755
                $this->colorCli->climate()->info('TMDb found '.$ret['title']);
756
            }
757
758
            return $ret;
759
        }
760
761
        return false;
762
    }
763
764
    /**
765
     * @return array|false
766
     */
767
    public function fetchIMDBProperties($imdbId)
768
    {
769
        $realId = (new Title($imdbId, $this->config))->real_id();
770
        $result = new Title($realId, $this->config);
771
        $title = ! empty($result->orig_title()) ? $result->orig_title() : $result->title();
772
        if (! empty($title)) {
773
            if (! empty($this->currentTitle)) {
774
                similar_text($this->currentTitle, $title, $percent);
775
                if ($percent >= self::MATCH_PERCENT) {
776
                    similar_text($this->currentYear, $result->year(), $percent);
777
                    if ($percent >= self::YEAR_MATCH_PERCENT) {
778
                        $ret = [
779
                            'title' => $title,
780
                            'tagline' => $result->tagline() ?? '',
781
                            'plot' => Arr::get($result->plot_split(), '0.plot'),
782
                            'rating' => ! empty($result->rating()) ? $result->rating() : '',
783
                            'year' => $result->year() ?? '',
784
                            'cover' => $result->photo() ?? '',
785
                            'genre' => $result->genre() ?? '',
786
                            'language' => $result->language() ?? '',
787
                            'type' => $result->movietype() ?? '',
788
                        ];
789
790
                        if ($this->echooutput) {
791
                            $this->colorCli->climate()->info('IMDb found '.$title);
792
                        }
793
794
                        return $ret;
795
                    }
796
797
                    return false;
798
                }
799
800
                return false;
801
            }
802
803
            return [
804
                'title' => $title,
805
                'tagline' => $result->tagline() ?? '',
806
                'plot' => Arr::get($result->plot_split(), '0.plot'),
807
                'rating' => ! empty($result->rating()) ? $result->rating() : '',
808
                'year' => $result->year() ?? '',
809
                'cover' => $result->photo() ?? '',
810
                'genre' => $result->genre() ?? '',
811
                'language' => $result->language() ?? '',
812
                'type' => $result->movietype() ?? '',
813
            ];
814
        }
815
816
        return false;
817
    }
818
819
    /**
820
     * Fetch TraktTV backdrop / cover / title.
821
     *
822
     * @return array|false
823
     *
824
     * @throws \Exception
825
     * @throws GuzzleException
826
     */
827
    public function fetchTraktTVProperties($imdbId)
828
    {
829
        if ($this->traktcheck !== null) {
830
            $resp = $this->traktTv->client->movieSummary('tt'.$imdbId, 'full');
831
            if ($resp !== false) {
832
                similar_text($this->currentTitle, $resp['title'], $percent);
833
                if ($percent >= self::MATCH_PERCENT) {
834
                    similar_text($this->currentYear, $resp['year'], $percent);
835
                    if ($percent >= self::YEAR_MATCH_PERCENT) {
836
                        $ret = [];
837
                        if (isset($resp['ids']['trakt'])) {
838
                            $ret['id'] = $resp['ids']['trakt'];
839
                        }
840
841
                        $ret['overview'] = $resp['overview'] ?? '';
842
                        $ret['tagline'] = $resp['tagline'] ?? '';
843
                        $ret['year'] = $resp['year'] ?? '';
844
                        $ret['genres'] = $resp['genres'] ?? '';
845
846
                        if (isset($resp['title'])) {
847
                            $ret['title'] = $resp['title'];
848
                        } else {
849
                            return false;
850
                        }
851
852
                        if ($this->echooutput) {
853
                            $this->colorCli->climate()->info('Trakt found '.$ret['title']);
854
                        }
855
856
                        return $ret;
857
                    }
858
859
                    return false;
860
                }
861
862
                return false;
863
            }
864
865
            return false;
866
        }
867
868
        return false;
869
    }
870
871
    /**
872
     * Fetch OMDb backdrop / cover / title.
873
     *
874
     * @return array|false
875
     */
876
    public function fetchOmdbAPIProperties($imdbId)
877
    {
878
        if ($this->omdbapikey !== null) {
879
            $resp = $this->omdbApi->fetch('i', 'tt'.$imdbId);
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on null. ( Ignorable by Annotation )

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

879
            /** @scrutinizer ignore-call */ 
880
            $resp = $this->omdbApi->fetch('i', 'tt'.$imdbId);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
880
881
            if (\is_object($resp) && $resp->message === 'OK' && ! Str::contains($resp->data->Response, 'Error:') && $resp->data->Response !== 'False') {
0 ignored issues
show
introduced by
The condition is_object($resp) is always false.
Loading history...
882
                similar_text($this->currentTitle, $resp->data->Title, $percent);
883
                if ($percent >= self::MATCH_PERCENT) {
884
                    similar_text($this->currentYear, $resp->data->Year, $percent);
885
                    if ($percent >= self::YEAR_MATCH_PERCENT) {
886
                        $ret = [
887
                            'title' => $resp->data->Title ?? '',
888
                            'cover' => $resp->data->Poster ?? '',
889
                            'genre' => $resp->data->Genre ?? '',
890
                            'year' => $resp->data->Year ?? '',
891
                            'plot' => $resp->data->Plot ?? '',
892
                            'rating' => $resp->data->imdbRating ?? '',
893
                            'rtRating' => $resp->data->Ratings[1]->Value ?? '',
894
                            'tagline' => $resp->data->Tagline ?? '',
895
                            'director' => $resp->data->Director ?? '',
896
                            'actors' => $resp->data->Actors ?? '',
897
                            'language' => $resp->data->Language ?? '',
898
                            'boxOffice' => $resp->data->BoxOffice ?? '',
899
                        ];
900
901
                        if ($this->echooutput) {
902
                            $this->colorCli->climate()->info('OMDbAPI Found '.$ret['title']);
903
                        }
904
905
                        return $ret;
906
                    }
907
908
                    return false;
909
                }
910
911
                return false;
912
            }
913
914
            return false;
915
        }
916
917
        return false;
918
    }
919
920
    /**
921
     * @return array|bool
922
     *
923
     * @throws InvalidProviderException
924
     * @throws \Exception
925
     */
926
    public function fetchItunesMovieProperties(string $title)
927
    {
928
        $movie = true;
929
        try {
930
            $iTunesMovie = iTunes::load('movie')->fetchOneByName($title);
931
        } catch (MovieNotFoundException $e) {
932
            $movie = false;
933
        } catch (SearchNoResultsException $e) {
934
            $movie = false;
935
        }
936
937
        if ($movie !== false) {
938
            similar_text($this->currentTitle, $iTunesMovie->getName(), $percent);
939
            if ($percent >= self::MATCH_PERCENT) {
940
                $movie = [
941
                    'title' => $iTunesMovie->getName(),
942
                    'director' => $iTunesMovie->getDirector() ?? '',
0 ignored issues
show
Bug introduced by
The method getDirector() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of DariusIII\ItunesApi\Entities\EntityInterface such as DariusIII\ItunesApi\Entities\Movie. ( Ignorable by Annotation )

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

942
                    'director' => $iTunesMovie->/** @scrutinizer ignore-call */ getDirector() ?? '',
Loading history...
943
                    'tagline' => $iTunesMovie->getTagLine() ?? '',
0 ignored issues
show
Bug introduced by
The method getTagLine() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of DariusIII\ItunesApi\Entities\EntityInterface such as DariusIII\ItunesApi\Entities\Movie. ( Ignorable by Annotation )

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

943
                    'tagline' => $iTunesMovie->/** @scrutinizer ignore-call */ getTagLine() ?? '',
Loading history...
944
                    'cover' => str_replace('100x100', '800x800', $iTunesMovie->getCover()),
0 ignored issues
show
Bug introduced by
The method getCover() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in DariusIII\ItunesApi\Entities\Artist. Are you sure you never get one of those? ( Ignorable by Annotation )

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

944
                    'cover' => str_replace('100x100', '800x800', $iTunesMovie->/** @scrutinizer ignore-call */ getCover()),
Loading history...
945
                    'genre' => $iTunesMovie->getGenre() ?? '',
0 ignored issues
show
Bug introduced by
The method getGenre() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in DariusIII\ItunesApi\Entities\Artist. Are you sure you never get one of those? ( Ignorable by Annotation )

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

945
                    'genre' => $iTunesMovie->/** @scrutinizer ignore-call */ getGenre() ?? '',
Loading history...
946
                    'plot' => $iTunesMovie->getDescription() ?? '',
0 ignored issues
show
Bug introduced by
The method getDescription() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of DariusIII\ItunesApi\Entities\EntityInterface such as DariusIII\ItunesApi\Entities\Ebook or DariusIII\ItunesApi\Entities\Movie. ( Ignorable by Annotation )

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

946
                    'plot' => $iTunesMovie->/** @scrutinizer ignore-call */ getDescription() ?? '',
Loading history...
947
                    'year' => $iTunesMovie->getReleaseDate() ? $iTunesMovie->getReleaseDate()->format('Y') : '',
0 ignored issues
show
Bug introduced by
The method getReleaseDate() does not exist on DariusIII\ItunesApi\Entities\EntityInterface. It seems like you code against a sub-type of said class. However, the method does not exist in DariusIII\ItunesApi\Entities\Track or DariusIII\ItunesApi\Entities\Artist. Are you sure you never get one of those? ( Ignorable by Annotation )

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

947
                    'year' => $iTunesMovie->/** @scrutinizer ignore-call */ getReleaseDate() ? $iTunesMovie->getReleaseDate()->format('Y') : '',
Loading history...
948
                ];
949
            } else {
950
                $movie = false;
951
            }
952
        }
953
954
        return $movie;
955
    }
956
957
    /**
958
     * Update a release with a IMDB id.
959
     *
960
     * @param  string  $buffer  Data to parse a IMDB id/Trakt Id from.
961
     * @param  string  $service  Method that called this method.
962
     * @param  int  $id  id of the release.
963
     * @param  int  $processImdb  To get IMDB info on this IMDB id or not.
964
     *
965
     * @throws \Exception
966
     */
967
    public function doMovieUpdate(string $buffer, string $service, int $id, int $processImdb = 1): string
968
    {
969
        $imdbId = false;
970
        if (preg_match('/(?:imdb.*?)?(?:tt|Title\?)(?P<imdbid>\d{5,8})/i', $buffer, $hits)) {
971
            $imdbId = $hits['imdbid'];
972
        }
973
974
        if ($imdbId !== false) {
975
            $this->service = $service;
976
            if ($this->echooutput && $this->service !== '') {
977
                $this->colorCli->climate()->info($this->service.' found IMDBid: tt'.$imdbId);
978
            }
979
980
            $movieInfoId = MovieInfo::query()->where('imdbid', $imdbId)->first(['id']);
981
982
            Release::query()->where('id', $id)->update(['imdbid' => $imdbId, 'movieinfo_id' => $movieInfoId !== null ? $movieInfoId['id'] : null]);
983
984
            // If set, scan for imdb info.
985
            if ($processImdb === 1) {
986
                $movCheck = $this->getMovieInfo($imdbId);
987
                if ($movCheck === null || (isset($movCheck['updated_at']) && (time() - strtotime($movCheck['updated_at'])) > 2592000)) {
988
                    $info = $this->updateMovieInfo($imdbId);
989
                    if ($info === false) {
990
                        Release::query()->where('id', $id)->update(['imdbid' => 0000000]);
991
                    } elseif ($info === true) {
0 ignored issues
show
introduced by
The condition $info === true is always true.
Loading history...
992
                        $movieInfoId = MovieInfo::query()->where('imdbid', $imdbId)->first(['id']);
993
994
                        Release::query()->where('id', $id)->update(['imdbid' => $imdbId, 'movieinfo_id' => $movieInfoId !== null ? $movieInfoId['id'] : null]);
995
                    }
996
                }
997
            }
998
        }
999
1000
        return $imdbId;
1001
    }
1002
1003
    /**
1004
     * Process releases with no IMDB id's.
1005
     *
1006
     *
1007
     *
1008
     * @throws \Exception
1009
     * @throws GuzzleException
1010
     */
1011
    public function processMovieReleases(string $groupID = '', string $guidChar = '', int $lookupIMDB = 1): void
1012
    {
1013
        if ($lookupIMDB === 0) {
1014
            return;
1015
        }
1016
1017
        // Get all releases without an IMDB id.
1018
        $sql = Release::query()
1019
            ->select(['searchname', 'id'])
1020
            ->whereBetween('categories_id', [Category::MOVIE_ROOT, Category::MOVIE_OTHER])
1021
            ->whereNull('imdbid')
1022
            ->where('nzbstatus', '=', 1);
1023
        if ($groupID !== '') {
1024
            $sql->where('groups_id', $groupID);
1025
        }
1026
1027
        if ($guidChar !== '') {
1028
            $sql->where('leftguid', $guidChar);
1029
        }
1030
1031
        if ((int) $lookupIMDB === 2) {
1032
            $sql->where('isrenamed', '=', 1);
1033
        }
1034
1035
        $res = $sql->limit($this->movieqty)->get();
1036
1037
        $movieCount = \count($res);
1038
1039
        if ($movieCount > 0) {
1040
            if ($this->echooutput && $movieCount > 1) {
1041
                $this->colorCli->header('Processing '.$movieCount.' movie releases.');
1042
            }
1043
1044
            // Loop over releases.
1045
            foreach ($res as $arr) {
1046
                // Try to get a name/year.
1047
                if (! $this->parseMovieSearchName($arr['searchname'])) {
1048
                    //We didn't find a name, so set to all 0's so we don't parse again.
1049
                    Release::query()->where('id', $arr['id'])->update(['imdbid' => 0000000]);
1050
1051
                    continue;
1052
                }
1053
                $this->currentRelID = $arr['id'];
1054
1055
                $movieName = $this->currentTitle;
1056
                if ($this->currentYear !== '') {
1057
                    $movieName .= ' ('.$this->currentYear.')';
1058
                }
1059
1060
                if ($this->echooutput) {
1061
                    $this->colorCli->climate()->info('Looking up: '.$movieName);
1062
                }
1063
1064
                $movieUpdated = false;
1065
1066
                // Check local DB.
1067
                $getIMDBid = $this->localIMDBSearch();
1068
1069
                if ($getIMDBid !== false) {
1070
                    $imdbId = $this->doMovieUpdate('tt'.$getIMDBid, 'Local DB', $arr['id']);
1071
                    if ($imdbId !== false) {
1072
                        $movieUpdated = true;
1073
                    }
1074
                }
1075
1076
                // Check on IMDb first
1077
                if ($movieUpdated === false) {
1078
                    try {
1079
                        $imdbSearch = new TitleSearch($this->config);
1080
                        foreach ($imdbSearch->search($this->currentTitle, [TitleSearch::MOVIE]) as $imdbTitle) {
1081
                            if (! empty($imdbTitle->title())) {
1082
                                similar_text($imdbTitle->title(), $this->currentTitle, $percent);
1083
                                if ($percent >= self::MATCH_PERCENT) {
1084
                                    similar_text($this->currentYear, $imdbTitle->year(), $percent);
1085
                                    if ($percent >= self::YEAR_MATCH_PERCENT) {
1086
                                        $getIMDBid = $imdbTitle->imdbid();
1087
                                        $imdbId = $this->doMovieUpdate('tt'.$getIMDBid, 'IMDb', $arr['id']);
1088
                                        if ($imdbId !== false) {
1089
                                            $movieUpdated = true;
1090
                                        }
1091
                                    }
1092
                                }
1093
                            }
1094
                        }
1095
                    } catch (\ErrorException $e) {
1096
                        $this->colorCli->error('Error fetching data from imdb occurred', true);
1097
                        Log::debug($e->getMessage());
1098
                    }
1099
                }
1100
1101
                // Check on OMDbAPI
1102
                if ($movieUpdated === false) {
1103
                    $omdbTitle = strtolower(str_replace(' ', '_', $this->currentTitle));
1104
                    if ($this->omdbapikey !== null) {
1105
                        if ($this->currentYear !== '') {
1106
                            $buffer = $this->omdbApi->search($omdbTitle, 'movie', $this->currentYear);
1107
                        } else {
1108
                            $buffer = $this->omdbApi->search($omdbTitle, 'movie');
1109
                        }
1110
1111
                        if (\is_object($buffer) && $buffer->message === 'OK' && ! Str::contains($buffer->data->Response, 'Error:') && $buffer->data->Response === 'True') {
1112
                            $getIMDBid = $buffer->data->Search[0]->imdbID;
1113
1114
                            if (! empty($getIMDBid)) {
1115
                                $imdbId = $this->doMovieUpdate($getIMDBid, 'OMDbAPI', $arr['id']);
1116
                                if ($imdbId !== false) {
1117
                                    $movieUpdated = true;
1118
                                }
1119
                            }
1120
                        }
1121
                    }
1122
                }
1123
1124
                // Check on Trakt.
1125
                if ($movieUpdated === false && $this->traktcheck !== null) {
1126
                    $data = $this->traktTv->client->movieSummary($movieName, 'full');
1127
                    if ($data !== false) {
1128
                        $this->parseTraktTv($data);
1129
                        if (! empty($data['ids']['imdb'])) {
1130
                            $imdbId = $this->doMovieUpdate($data['ids']['imdb'], 'Trakt', $arr['id']);
1131
                            if ($imdbId !== false) {
1132
                                $movieUpdated = true;
1133
                            }
1134
                        }
1135
                    }
1136
                }
1137
1138
                // Check on The Movie Database.
1139
                if ($movieUpdated === false) {
1140
                    try {
1141
                        $data = Tmdb::getSearchApi()->searchMovies($this->currentTitle);
1142
                        if (($data['total_results'] > 0) && ! empty($data['results'])) {
1143
                            foreach ($data['results'] as $result) {
1144
                                if (! empty($result['id']) && ! empty($result['release_date'])) {
1145
                                    similar_text($this->currentYear, Carbon::parse($result['release_date'])->year, $percent);
1146
                                    if ($percent >= self::YEAR_MATCH_PERCENT) {
1147
                                        $ret = $this->fetchTMDBProperties($result['id'], true);
1148
                                        if ($ret !== false && ! empty($ret['imdbid'])) {
1149
                                            $imdbId = $this->doMovieUpdate('tt'.$ret['imdbid'], 'TMDB', $arr['id']);
1150
                                            if ($imdbId !== false) {
1151
                                                $movieUpdated = true;
1152
                                            }
1153
                                        }
1154
                                    }
1155
                                } else {
1156
                                    $movieUpdated = false;
1157
                                }
1158
                            }
1159
                        } else {
1160
                            $movieUpdated = false;
1161
                        }
1162
                    } catch (TmdbApiException|\ErrorException $error) {
1163
                        $movieUpdated = false;
1164
                    }
1165
                }
1166
1167
                // We failed to get an IMDB id from all sources.
1168
                if ($movieUpdated === false) {
1169
                    Release::query()->where('id', $arr['id'])->update(['imdbid' => 0000000]);
1170
                }
1171
            }
1172
        }
1173
    }
1174
1175
    /**
1176
     * @return false|mixed
1177
     */
1178
    protected function localIMDBSearch()
1179
    {
1180
        //If we found a year, try looking in a 4 year range.
1181
        $check = MovieInfo::query()
1182
            ->where('title', 'like', '%'.$this->currentTitle.'%');
1183
1184
        if ($this->currentYear !== '') {
1185
            $start = Carbon::createFromFormat('Y', $this->currentYear)->subYears(2)->year;
0 ignored issues
show
Bug introduced by
The method subYears() does not exist on DateTime. It seems like you code against a sub-type of said class. However, the method does not exist in pq\DateTime. Are you sure you never get one of those? ( Ignorable by Annotation )

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

1185
            $start = Carbon::createFromFormat('Y', $this->currentYear)->/** @scrutinizer ignore-call */ subYears(2)->year;
Loading history...
1186
            $end = Carbon::createFromFormat('Y', $this->currentYear)->addYears(2)->year;
0 ignored issues
show
Bug introduced by
The method addYears() does not exist on DateTime. It seems like you code against a sub-type of said class. However, the method does not exist in pq\DateTime. Are you sure you never get one of those? ( Ignorable by Annotation )

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

1186
            $end = Carbon::createFromFormat('Y', $this->currentYear)->/** @scrutinizer ignore-call */ addYears(2)->year;
Loading history...
1187
            $check->whereBetween('year', [$start, $end]);
1188
        }
1189
        $IMDBCheck = $check->get(['imdbid']);
1190
        foreach ($IMDBCheck as $check) {
1191
            // match the title and year of the movie as close as possible.
1192
            if ($this->currentYear !== '') {
1193
                $IMDBCheck = MovieInfo::query()
1194
                    ->where('imdbid', $check['imdbid'])
1195
                    ->where('title', 'like', '%'.$this->currentTitle.'%')
1196
                    ->whereBetween('year', [$start, $end])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $end does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $start does not seem to be defined for all execution paths leading up to this point.
Loading history...
1197
                    ->first(['imdbid', 'title']);
1198
            } else {
1199
                $IMDBCheck = MovieInfo::query()
1200
                    ->where('imdbid', $check['imdbid'])
1201
                    ->where('title', 'like', '%'.$this->currentTitle.'%')
1202
                    ->first(['imdbid', 'title']);
1203
            }
1204
            // If we found a match, check if percentage is high enough. If so, return the IMDB id.
1205
            if ($IMDBCheck !== null) {
1206
                similar_text($this->currentTitle, $IMDBCheck['title'], $percent);
1207
                if ($percent >= self::MATCH_PERCENT) {
1208
                    return $IMDBCheck['imdbid'];
1209
                }
1210
            }
1211
        }
1212
1213
        return false;
1214
    }
1215
1216
    /**
1217
     * Parse a movie name from a release search name.
1218
     */
1219
    protected function parseMovieSearchName(string $releaseName): bool
1220
    {
1221
        $name = $year = '';
1222
        $followingList = '[^\w]((1080|480|720)p|AC3D|Directors([^\w]CUT)?|DD5\.1|(DVD|BD|BR)(Rip)?|BluRay|divx|HDTV|iNTERNAL|LiMiTED|(Real\.)?Proper|RE(pack|Rip)|Sub\.?(fix|pack)|Unrated|WEB-DL|(x|H)[ ._-]?264|xvid)[^\w]';
1223
1224
        /* Initial scan of getting a year/name.
1225
         * [\w. -]+ Gets 0-9a-z. - characters, most scene movie titles contain these chars.
1226
         * ie: [61420]-[FULL]-[a.b.foreignEFNet]-[ Coraline.2009.DUTCH.INTERNAL.1080p.BluRay.x264-VeDeTT ]-[21/85] - "vedett-coralien-1080p.r04" yEnc
1227
         * Then we look up the year, (19|20)\d\d, so $hits[1] would be Coraline $hits[2] 2009
1228
         */
1229
        if (preg_match('/(?P<name>[\w. -]+)[^\w](?P<year>(19|20)\d\d)/i', $releaseName, $hits)) {
1230
            $name = $hits['name'];
1231
            $year = $hits['year'];
1232
1233
            /* If we didn't find a year, try to get a name anyways.
1234
             * Try to look for a title before the $followingList and after anything but a-z0-9 two times or more (-[ for example)
1235
             */
1236
        } elseif (preg_match('/([^\w]{2,})?(?P<name>[\w .-]+?)'.$followingList.'/i', $releaseName, $hits)) {
1237
            $name = $hits['name'];
1238
        }
1239
1240
        // Check if we got something.
1241
        if ($name !== '') {
1242
            // If we still have any of the words in $followingList, remove them.
1243
            $name = preg_replace('/'.$followingList.'/i', ' ', $name);
1244
            // Remove periods, underscored, anything between parenthesis.
1245
            $name = preg_replace('/\(.*?\)|[._]/i', ' ', $name);
1246
            // Finally remove multiple spaces and trim leading spaces.
1247
            $name = trim(preg_replace('/\s{2,}/', ' ', $name));
1248
            // Check if the name is long enough and not just numbers.
1249
            if (\strlen($name) > 4 && ! preg_match('/^\d+$/', $name)) {
1250
                $this->currentTitle = $name;
1251
                $this->currentYear = $year;
1252
1253
                return true;
1254
            }
1255
        }
1256
1257
        return false;
1258
    }
1259
1260
    /**
1261
     * Get IMDB genres.
1262
     */
1263
    public function getGenres(): array
1264
    {
1265
        return [
1266
            'Action',
1267
            'Adventure',
1268
            'Animation',
1269
            'Biography',
1270
            'Comedy',
1271
            'Crime',
1272
            'Documentary',
1273
            'Drama',
1274
            'Family',
1275
            'Fantasy',
1276
            'Film-Noir',
1277
            'Game-Show',
1278
            'History',
1279
            'Horror',
1280
            'Music',
1281
            'Musical',
1282
            'Mystery',
1283
            'News',
1284
            'Reality-TV',
1285
            'Romance',
1286
            'Sci-Fi',
1287
            'Sport',
1288
            'Talk-Show',
1289
            'Thriller',
1290
            'War',
1291
            'Western',
1292
        ];
1293
    }
1294
}
1295