Passed
Push — master ( 4fa3d2...60c841 )
by Darko
07:44
created

Movie::buildCategorySearch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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

603
            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...
604
            $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

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

878
            /** @scrutinizer ignore-call */ 
879
            $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...
879
880
            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...
881
                similar_text($this->currentTitle, $resp->data->Title, $percent);
882
                if ($percent >= self::MATCH_PERCENT) {
883
                    similar_text($this->currentYear, $resp->data->Year, $percent);
884
                    if ($percent >= self::YEAR_MATCH_PERCENT) {
885
                        $ret = [
886
                            'title' => $resp->data->Title ?? '',
887
                            'cover' => $resp->data->Poster ?? '',
888
                            'genre' => $resp->data->Genre ?? '',
889
                            'year' => $resp->data->Year ?? '',
890
                            'plot' => $resp->data->Plot ?? '',
891
                            'rating' => $resp->data->imdbRating ?? '',
892
                            'rtRating' => $resp->data->Ratings[1]->Value ?? '',
893
                            'tagline' => $resp->data->Tagline ?? '',
894
                            'director' => $resp->data->Director ?? '',
895
                            'actors' => $resp->data->Actors ?? '',
896
                            'language' => $resp->data->Language ?? '',
897
                            'boxOffice' => $resp->data->BoxOffice ?? '',
898
                        ];
899
900
                        if ($this->echooutput) {
901
                            $this->colorCli->climate()->info('OMDbAPI Found '.$ret['title']);
902
                        }
903
904
                        return $ret;
905
                    }
906
907
                    return false;
908
                }
909
910
                return false;
911
            }
912
913
            return false;
914
        }
915
916
        return false;
917
    }
918
919
    /**
920
     * @return array|bool
921
     *
922
     * @throws InvalidProviderException
923
     * @throws \Exception
924
     */
925
    public function fetchItunesMovieProperties(string $title)
926
    {
927
        $movie = true;
928
        try {
929
            $iTunesMovie = iTunes::load('movie')->fetchOneByName($title);
930
        } catch (MovieNotFoundException $e) {
931
            $movie = false;
932
        } catch (SearchNoResultsException $e) {
933
            $movie = false;
934
        }
935
936
        if ($movie !== false) {
937
            similar_text($this->currentTitle, $iTunesMovie->getName(), $percent);
938
            if ($percent >= self::MATCH_PERCENT) {
939
                $movie = [
940
                    'title' => $iTunesMovie->getName(),
941
                    '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

941
                    'director' => $iTunesMovie->/** @scrutinizer ignore-call */ getDirector() ?? '',
Loading history...
942
                    '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

942
                    'tagline' => $iTunesMovie->/** @scrutinizer ignore-call */ getTagLine() ?? '',
Loading history...
943
                    '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

943
                    'cover' => str_replace('100x100', '800x800', $iTunesMovie->/** @scrutinizer ignore-call */ getCover()),
Loading history...
944
                    '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

944
                    'genre' => $iTunesMovie->/** @scrutinizer ignore-call */ getGenre() ?? '',
Loading history...
945
                    '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

945
                    'plot' => $iTunesMovie->/** @scrutinizer ignore-call */ getDescription() ?? '',
Loading history...
946
                    '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

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

1184
            $start = Carbon::createFromFormat('Y', $this->currentYear)->/** @scrutinizer ignore-call */ subYears(2)->year;
Loading history...
1185
            $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

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