From 445bd3b1f7c93d11779c46da8df2aa897362d269 Mon Sep 17 00:00:00 2001 From: "Edward Tirado Jr." Date: Mon, 20 Apr 2026 23:38:13 -0500 Subject: [PATCH] Created django import command --- .gitignore | 1 + app/Console/Commands/django_import.php | 226 ++++++++++++++++++ app/Models/Schedule.php | 15 ++ app/Models/Showing.php | 16 ++ ...26_04_20_233335_create_schedules_table.php | 32 +++ ...026_04_20_233347_create_showings_table.php | 32 +++ 6 files changed, 322 insertions(+) create mode 100644 app/Console/Commands/django_import.php create mode 100644 app/Models/Schedule.php create mode 100644 app/Models/Showing.php create mode 100644 database/migrations/2026_04_20_233335_create_schedules_table.php create mode 100644 database/migrations/2026_04_20_233347_create_showings_table.php diff --git a/.gitignore b/.gitignore index 3cc8dd3..66aeb7b 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ /public/storage /storage/*.key /storage/pail +/storage/dbbackups /vendor Homestead.json Homestead.yaml diff --git a/app/Console/Commands/django_import.php b/app/Console/Commands/django_import.php new file mode 100644 index 0000000..83b9790 --- /dev/null +++ b/app/Console/Commands/django_import.php @@ -0,0 +1,226 @@ +wipeTables(); + + $this->importMovies(); + $this->importMovielists(); + $this->importMovieListMovies(); + $this->importSchedules(); + $this->importShowings(); + } + + private function wipeTables() + { + $this->info('Truncating tables...'); + + DB::statement('SET FOREIGN_KEY_CHECKS=0;'); + DB::table('movie_movie_list')->truncate(); + DB::table('movies')->truncate(); + DB::table('movie_lists')->truncate(); + DB::table('schedules')->truncate(); + DB::table('showings')->truncate(); + DB::statement('SET FOREIGN_KEY_CHECKS=1;'); + } + + private function importMovies($fileName = 'moviemanager_movies.csv'): void + { + $this->info('Importing movies...'); + + $file = fopen(storage_path($this->csvPath.$fileName), 'r'); + if (! $file) { + $this->error('File not found: '.$fileName); + + return; + } + + // Skip the header row + fgetcsv($file); + + while (($row = fgetcsv($file)) !== false) { + Movie::forceCreate([ + 'id' => $row[0], + 'title' => $row[1], + 'imdb_id' => $row[2], + 'year' => $row[3], + 'director' => $row[4], + 'actors' => $row[5], + 'plot' => $row[6], + 'genre' => $row[7], + 'mpaa_rating' => $row[8], + 'critic_scores' => $this->parsePythonList($row[9]), + 'poster' => $row[10], + 'added_by' => $row[11], + ]); + } + + fclose($file); + + } + + /** + * Convert a Python-style list string (single-quoted) to a PHP array. + * + * @return array> + */ + private function parsePythonList(string $value): array + { + if (empty($value) || $value === '[]') { + return []; + } + + $json = str_replace("'", '"', $value); + if (str_starts_with($json, '{')) { + // Fixes incorrect key for Source in some older data + $json = str_replace('Score', 'Source', $json); + $json = '['.$json.']'; + + } + $decoded = json_decode($json, true); + + if (is_array($decoded)) { + return $decoded; + } elseif (is_string($decoded)) { + return [$decoded]; + } + + return []; + } + + private function importMovielists($fileName = 'moviemanager_movielist.csv'): void + { + $this->info('Importing Movie Lists...'); + + $file = fopen(storage_path($this->csvPath.$fileName), 'r'); + if (! $file) { + $this->error('File not found: '.$fileName); + + return; + } + + // Skip the header row + fgetcsv($file); + + while (($row = fgetcsv($file)) !== false) { + MovieList::create([ + 'name' => $row[1], + 'is_public' => $row[2] === 't' ? true : false, + 'slug' => Str::slug($row[1]), + 'owner' => $row[3], + ]); + } + + fclose($file); + } + + private function importMovieListMovies($fileName = 'moviemanager_movielist_movies.csv'): void + { + $this->info('Importing movie_list_movies...'); + $file = fopen(storage_path($this->csvPath.$fileName), 'r'); + if (! $file) { + $this->error('File not found: '.$fileName); + + return; + } + + // Skip the header row + fgetcsv($file); + + while (($row = fgetcsv($file)) !== false) { + $movieList = MovieList::find($row[1]); + $movie = Movie::find($row[2]); + + if ($movieList && $movie) { + $movieList->movies()->attach($movie); + } else { + $this->error('Movie or MovieList not found. Movie ID: '.$row[2].', MovieList ID: '.$row[1]); + } + } + + fclose($file); + } + + private function importSchedules($fileName = 'moviemanager_schedule.csv'): void + { + $this->info('Importing schedules...'); + $file = fopen(storage_path($this->csvPath.$fileName), 'r'); + if (! $file) { + $this->error('File not found: '.$fileName); + + return; + } + + // Skip the header row + fgetcsv($file); + + while (($row = fgetcsv($file)) !== false) { + Schedule::create([ + 'name' => $row[1], + 'is_public' => $row[2] === 't' ? true : false, + 'slug' => $row[3], + 'owner' => $row[4], + ]); + } + + fclose($file); + } + + private function importShowings($fileName = 'moviemanager_showing.csv'): void + { + $this->info('Importing showings...'); + $file = fopen(storage_path($this->csvPath.$fileName), 'r'); + if (! $file) { + $this->error('File not found: '.$fileName); + + return; + } + + // Skip the header row + fgetcsv($file); + + while (($row = fgetcsv($file)) !== false) { + Showing::create([ + 'is_public' => $row[1] === 't' ? true : false, + 'showtime' => Carbon::parse($row[2]), + 'movie_id' => $row[3], + 'owner_id' => $row[4], + 'schedule_id' => $row[5], + ]); + } + + fclose($file); + } +} diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php new file mode 100644 index 0000000..14614f2 --- /dev/null +++ b/app/Models/Schedule.php @@ -0,0 +1,15 @@ +id(); + $table->string('name'); + $table->boolean('is_public')->default(false); + $table->string('slug'); + $table->foreignId('owner')->constrained('users')->cascadeOnDelete(); + $table->softDeletes(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('schedules'); + } +}; diff --git a/database/migrations/2026_04_20_233347_create_showings_table.php b/database/migrations/2026_04_20_233347_create_showings_table.php new file mode 100644 index 0000000..3fe8dc9 --- /dev/null +++ b/database/migrations/2026_04_20_233347_create_showings_table.php @@ -0,0 +1,32 @@ +id(); + $table->dateTime('showtime'); + $table->boolean('is_public')->default(false); + $table->foreignId('movie_id')->constrained('movies')->cascadeOnDelete(); + $table->foreignId('owner_id')->constrained('users')->cascadeOnDelete(); + $table->foreignId('schedule_id')->constrained('schedules')->cascadeOnDelete(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('showings'); + } +};