Skip to content

Commit 8fde781

Browse files
committed
refactor: save igdb games to the database
Step 1 towards cleaner meta fetching code. Users with a game category must run `php artisan fetch:meta` after migration.
1 parent 78a4e6b commit 8fde781

26 files changed

+728
-172
lines changed

.cspell/laravel.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ doesntcontain
55
doesntexist
66
dontbroadcasttocurrentuser
77
dontflash
8+
dontrelease
89
dontreport
910
ehlo
1011
encryptable

app/Console/Commands/FetchMeta.php

+48-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace App\Console\Commands;
1818

1919
use App\Models\Torrent;
20+
use App\Services\Igdb\IgdbScraper;
2021
use App\Services\Tmdb\TMDBScraper;
2122
use Exception;
2223
use Illuminate\Console\Command;
@@ -45,25 +46,60 @@ class FetchMeta extends Command
4546
*/
4647
final public function handle(): void
4748
{
48-
$this->alert('Meta Fetcher Started');
49+
$start = now();
50+
$this->alert('Meta fetch queueing started. Fetching is done asynchronously in a separate job queue.');
4951

5052
$tmdbScraper = new TMDBScraper();
51-
$torrents = Torrent::with('category')->select('tmdb', 'category_id', 'name')->whereNotNull('tmdb')->where('tmdb', '!=', 0)->oldest()->get();
53+
$igdbScraper = new IgdbScraper();
5254

53-
foreach ($torrents as $torrent) {
55+
$this->info('Querying all tmdb movie ids');
56+
57+
$tmdbMovieIds = Torrent::query()
58+
->whereRelation('category', 'movie_meta', '=', true)
59+
->select('tmdb')
60+
->distinct()
61+
->pluck('tmdb');
62+
63+
$this->info('Queueing all tmdb movie metadata fetching');
64+
65+
foreach ($tmdbMovieIds as $id) {
66+
sleep(3);
67+
$tmdbScraper->movie($id);
68+
$this->info("Movie metadata fetched for tmdb {$id}");
69+
}
70+
71+
$this->info('Querying all tmdb tv ids');
72+
73+
$tmdbTvIds = Torrent::query()
74+
->whereRelation('category', 'tv_meta', '=', true)
75+
->select('tmdb')
76+
->distinct()
77+
->pluck('tmdb');
78+
79+
$this->info('Queueing all tmdb tv metadata fetching');
80+
81+
foreach ($tmdbTvIds as $id) {
5482
sleep(3);
83+
$tmdbScraper->tv($id);
84+
$this->info("Movie metadata fetched for tmdb {$id}");
85+
}
86+
87+
$this->info('Querying all igdb game ids');
88+
89+
$igdbGameIds = Torrent::query()
90+
->whereRelation('category', 'game_meta', '=', true)
91+
->select('igdb')
92+
->distinct()
93+
->pluck('igdb');
5594

56-
if ($torrent->category->tv_meta) {
57-
$tmdbScraper->tv($torrent->tmdb);
58-
$this->info(\sprintf('(%s) Metadata Fetched For Torrent %s ', $torrent->category->name, $torrent->name));
59-
}
95+
$this->info('Queueing all igdb game metadata fetching');
6096

61-
if ($torrent->category->movie_meta) {
62-
$tmdbScraper->movie($torrent->tmdb);
63-
$this->info(\sprintf('(%s) Metadata Fetched For Torrent %s ', $torrent->category->name, $torrent->name));
64-
}
97+
foreach ($igdbGameIds as $id) {
98+
usleep(250_000);
99+
$igdbScraper->game($id);
100+
$this->info("Game metadata fetched for igdb {$id}");
65101
}
66102

67-
$this->alert('Meta Fetcher Complete');
103+
$this->alert('Meta fetch queueing complete in '.now()->floatDiffInSeconds($start).'s.');
68104
}
69105
}

app/Http/Controllers/API/TorrentController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use App\Http\Resources\TorrentsResource;
2525
use App\Models\Category;
2626
use App\Models\FeaturedTorrent;
27+
use App\Models\IgdbGame;
2728
use App\Models\Keyword;
2829
use App\Models\Movie;
2930
use App\Models\Torrent;
@@ -40,7 +41,6 @@
4041
use Illuminate\Support\Carbon;
4142
use Illuminate\Support\Facades\Storage;
4243
use Illuminate\Validation\Rule;
43-
use MarcReichel\IGDBLaravel\Models\Game;
4444
use Meilisearch\Endpoints\Indexes;
4545

4646
/**
@@ -474,7 +474,7 @@ public function show(int $id): TorrentResource
474474
}
475475

476476
if ($torrent->category->game_meta && $torrent->igdb) {
477-
$torrent->setAttribute('meta', Game::with(['genres' => ['name']])->find($torrent->igdb));
477+
$torrent->setAttribute('meta', IgdbGame::with(['genres'])->find($torrent->igdb));
478478
}
479479

480480
TorrentResource::withoutWrapping();

app/Http/Controllers/RequestController.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use App\Http\Requests\StoreTorrentRequestRequest;
2020
use App\Http\Requests\UpdateTorrentRequestRequest;
2121
use App\Models\Category;
22+
use App\Models\IgdbGame;
2223
use App\Models\Movie;
2324
use App\Models\Resolution;
2425
use App\Models\TorrentRequest;
@@ -28,7 +29,6 @@
2829
use App\Repositories\ChatRepository;
2930
use App\Services\Tmdb\TMDBScraper;
3031
use Illuminate\Http\Request;
31-
use MarcReichel\IGDBLaravel\Models\Game;
3232
use Exception;
3333

3434
/**
@@ -80,13 +80,9 @@ public function show(Request $request, TorrentRequest $torrentRequest): \Illumin
8080
'collection'
8181
])
8282
->find($torrentRequest->tmdb),
83-
($torrentRequest->category->game_meta && $torrentRequest->igdb) => Game::with([
84-
'cover' => ['url', 'image_id'],
85-
'artworks' => ['url', 'image_id'],
86-
'genres' => ['name'],
87-
'videos' => ['video_id', 'name'],
88-
'involved_companies.company',
89-
'involved_companies.company.logo',
83+
($torrentRequest->category->game_meta && $torrentRequest->igdb) => IgdbGame::with([
84+
'genres',
85+
'companies',
9086
'platforms',
9187
])
9288
->find($torrentRequest->igdb),

app/Http/Controllers/SimilarTorrentController.php

+48-59
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
namespace App\Http\Controllers;
1818

1919
use App\Models\Category;
20+
use App\Models\IgdbGame;
2021
use App\Models\Movie;
2122
use App\Models\Torrent;
2223
use App\Models\TorrentRequest;
2324
use App\Models\Tv;
25+
use App\Services\Igdb\IgdbScraper;
2426
use App\Services\Tmdb\TMDBScraper;
2527
use Illuminate\Http\Request;
2628
use Illuminate\Support\Carbon;
27-
use MarcReichel\IGDBLaravel\Models\Game;
28-
use MarcReichel\IGDBLaravel\Models\PlatformLogo;
2929

3030
class SimilarTorrentController extends Controller
3131
{
@@ -71,18 +71,12 @@ public function show(int $categoryId, int $tmdbId): \Illuminate\Contracts\View\F
7171

7272
abort_unless($hasTorrents, 404, 'No Similar Torrents Found');
7373

74-
$meta = Game::with([
75-
'cover' => ['url', 'image_id'],
76-
'artworks' => ['url', 'image_id'],
77-
'genres' => ['name'],
78-
'videos' => ['video_id', 'name'],
79-
'involved_companies.company',
80-
'involved_companies.company.logo',
74+
$meta = IgdbGame::with([
75+
'genres',
76+
'companies',
8177
'platforms',
82-
])
83-
->findOrFail($tmdbId);
84-
$link = collect($meta->videos)->take(1)->pluck('video_id');
85-
$platforms = PlatformLogo::whereIn('id', collect($meta->platforms)->pluck('platform_logo')->toArray())->get();
78+
]);
79+
8680
$igdb = $tmdbId;
8781

8882
break;
@@ -95,66 +89,61 @@ public function show(int $categoryId, int $tmdbId): \Illuminate\Contracts\View\F
9589
return view('torrent.similar', [
9690
'meta' => $meta,
9791
'personal_freeleech' => $personalFreeleech,
98-
'platforms' => $platforms ?? null,
9992
'category' => $category,
10093
'tmdb' => $tmdb ?? null,
10194
'igdb' => $igdb ?? null,
10295
]);
10396
}
10497

105-
public function update(Request $request, Category $category, int $tmdbId): \Illuminate\Http\RedirectResponse
98+
public function update(Request $request, Category $category, int $metaId): \Illuminate\Http\RedirectResponse
10699
{
100+
if (!($category->movie_meta || $category->tv_meta || $category->game_meta)) {
101+
return to_route('torrents.similar', ['category_id' => $category->id, 'tmdb' => $metaId])
102+
->withErrors('This meta type can not be updated.');
103+
}
104+
107105
if (
108-
$tmdbId === 0
106+
$metaId === 0
109107
|| (
110-
Torrent::where('category_id', '=', $category->id)->where('tmdb', '=', $tmdbId)->doesntExist()
111-
&& TorrentRequest::where('category_id', '=', $category->id)->where('tmdb', '=', $tmdbId)->doesntExist()
108+
($category->movie_meta || $category->tv_meta)
109+
&& Torrent::where('category_id', '=', $category->id)->where('tmdb', '=', $metaId)->doesntExist()
110+
&& TorrentRequest::where('category_id', '=', $category->id)->where('tmdb', '=', $metaId)->doesntExist()
111+
)
112+
|| (
113+
$category->game_meta
114+
&& Torrent::where('category_id', '=', $category->id)->where('igdb', '=', $metaId)->doesntExist()
115+
&& TorrentRequest::where('category_id', '=', $category->id)->where('igdb', '=', $metaId)->doesntExist()
112116
)
113117
) {
114-
return to_route('torrents.similar', ['category_id' => $category->id, 'tmdb' => $tmdbId])
118+
return to_route('torrents.similar', ['category_id' => $category->id, 'tmdb' => $metaId])
115119
->withErrors('There exists no torrent with this tmdb.');
116120
}
117121

118-
$tmdbScraper = new TMDBScraper();
119-
120-
switch (true) {
121-
case $category->movie_meta:
122-
$cacheKey = 'tmdb-movie-scraper:'.$tmdbId;
123-
124-
/** @var Carbon $lastUpdated */
125-
$lastUpdated = cache()->get($cacheKey);
126-
127-
abort_if(
128-
$lastUpdated !== null
129-
&& $lastUpdated->addDay()->isFuture()
130-
&& !($request->user()->group->is_modo || $request->user()->group->is_editor),
131-
403
132-
);
133-
134-
cache()->put($cacheKey, now(), now()->addDay());
135-
136-
$tmdbScraper->movie($tmdbId);
137-
138-
break;
139-
case $category->tv_meta:
140-
$cacheKey = 'tmdb-tv-scraper:'.$tmdbId;
141-
142-
/** @var Carbon $lastUpdated */
143-
$lastUpdated = cache()->get($cacheKey);
144-
145-
abort_if(
146-
$lastUpdated !== null
147-
&& $lastUpdated->addDay()->isFuture()
148-
&& !($request->user()->group->is_modo || $request->user()->group->is_editor),
149-
403
150-
);
151-
152-
cache()->put($cacheKey, now(), now()->addDay());
153-
154-
$tmdbScraper->tv($tmdbId);
155-
156-
break;
157-
}
122+
/** @phpstan-ignore match.unhandled (The first line of this method ensures that at least one of these are true) */
123+
$cacheKey = match (true) {
124+
$category->movie_meta => "tmdb-movie-scraper:{$metaId}",
125+
$category->tv_meta => "tmdb-tv-scraper:{$metaId}",
126+
$category->game_meta => "igdb-game-scraper:{$metaId}",
127+
};
128+
129+
/** @var ?Carbon $lastUpdated */
130+
$lastUpdated = cache()->get($cacheKey);
131+
132+
abort_if(
133+
$lastUpdated !== null
134+
&& $lastUpdated->addDay()->isFuture()
135+
&& !($request->user()->group->is_modo || $request->user()->group->is_torrent_modo || $request->user()->group->is_editor),
136+
403
137+
);
138+
139+
cache()->put($cacheKey, now(), now()->addDay());
140+
141+
/** @phpstan-ignore match.unhandled (The first line of this method ensures that at least one of these are true) */
142+
match (true) {
143+
$category->movie_meta => new TMDBScraper()->movie($metaId),
144+
$category->tv_meta => new TMDBScraper()->tv($metaId),
145+
$category->game_meta => new IgdbScraper()->game($metaId),
146+
};
158147

159148
return back()->with('success', 'Metadata update queued successfully.');
160149
}

app/Http/Controllers/TorrentController.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use App\Models\Category;
2828
use App\Models\Distributor;
2929
use App\Models\History;
30+
use App\Models\IgdbGame;
3031
use App\Models\Keyword;
3132
use App\Models\Movie;
3233
use App\Models\Region;
@@ -44,8 +45,6 @@
4445
use Illuminate\Http\Request;
4546
use Illuminate\Support\Carbon;
4647
use Intervention\Image\Facades\Image;
47-
use MarcReichel\IGDBLaravel\Models\Game;
48-
use MarcReichel\IGDBLaravel\Models\PlatformLogo;
4948
use Exception;
5049
use Illuminate\Support\Facades\Notification;
5150
use Illuminate\Support\Facades\Storage;
@@ -100,7 +99,6 @@ public function show(Request $request, int|string $id): \Illuminate\Contracts\Vi
10099
->findOrFail($id);
101100

102101
$meta = null;
103-
$platforms = null;
104102

105103
if ($torrent->category->tv_meta && $torrent->tmdb) {
106104
$meta = Tv::with([
@@ -130,17 +128,12 @@ public function show(Request $request, int|string $id): \Illuminate\Contracts\Vi
130128
}
131129

132130
if ($torrent->category->game_meta && $torrent->igdb) {
133-
$meta = Game::with([
134-
'cover' => ['url', 'image_id'],
135-
'artworks' => ['url', 'image_id'],
136-
'genres' => ['name'],
137-
'videos' => ['video_id', 'name'],
138-
'involved_companies.company',
139-
'involved_companies.company.logo',
140-
'platforms', ])
131+
$meta = IgdbGame::with([
132+
'genres',
133+
'companies',
134+
'platforms',
135+
])
141136
->find($torrent->igdb);
142-
$link = collect($meta->videos)->take(1)->pluck('video_id');
143-
$platforms = PlatformLogo::whereIn('id', collect($meta->platforms)->pluck('platform_logo')->toArray())->get();
144137
}
145138

146139
return view('torrent.show', [
@@ -157,7 +150,6 @@ public function show(Request $request, int|string $id): \Illuminate\Contracts\Vi
157150
),
158151
'personal_freeleech' => cache()->get('personal_freeleech:'.$user->id),
159152
'meta' => $meta,
160-
'platforms' => $platforms,
161153
'total_tips' => $torrent->tips()->sum('bon'),
162154
'user_tips' => $torrent->tips()->where('sender_id', '=', $user->id)->sum('bon'),
163155
'mediaInfo' => $torrent->mediainfo !== null ? (new MediaInfo())->parse($torrent->mediainfo) : null,

app/Http/Livewire/SimilarTorrent.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use App\Models\Category;
2121
use App\Models\Distributor;
2222
use App\Models\History;
23+
use App\Models\IgdbGame;
2324
use App\Models\Movie;
2425
use App\Models\Region;
2526
use App\Models\Resolution;
@@ -433,7 +434,7 @@ final public function deleteRecords(): void
433434
$title = match (true) {
434435
$this->category->movie_meta => ($movie = Movie::find($this->tmdbId))->title.' ('.$movie->release_date->format('Y').')',
435436
$this->category->tv_meta => ($tv = Tv::find($this->tmdbId))->name.' ('.$tv->first_air_date->format('Y').')',
436-
$this->category->game_meta => ($game = Game::find($this->igdbId))->name.' ('.$game->first_release_date->format('Y').')',
437+
$this->category->game_meta => ($game = IgdbGame::find($this->igdbId))->name.' ('.$game->first_release_date->format('Y').')',
437438
default => $torrents->pluck('name')->join(', '),
438439
};
439440

0 commit comments

Comments
 (0)