Преглед изворни кода

Add all option to scraping and importing

Colin Powell пре 3 година
родитељ
комит
bea9703c99

+ 29 - 11
games/management/commands/import_gamelist_xml_file.py

@@ -1,3 +1,4 @@
+from django.conf import settings
 from django.core.management.base import BaseCommand, CommandError
 
 from games.utils import import_gamelist_file_to_db_for_system
@@ -19,25 +20,42 @@ class Command(BaseCommand):
             help="Update all files, even ones we already know about",
         )
 
-    def handle(self, *args, **options):
-        results = import_gamelist_file_to_db_for_system(
-            options["system"],
-            options["file"],
-            options["full_scan"],
-        )
+    def import_from_slug(self, slug, file_path, full_scan=False):
+        results = import_gamelist_file_to_db_for_system(slug, file_path, full_scan)
+    
+        if not results:
+            self.style.ERROR("No games imported for {slug}, check for gamelist.xml file or re-run scraper")
+            return
+    
         imported = results["imported"]
         not_imported = results["not_imported"]
+    
         if imported:
             self.stdout.write(
-                self.style.SUCCESS(f"Successfully imported {len(imported)} games")
+                self.style.SUCCESS(f"Successfully imported {len(imported)} games for {slug}")
             )
-        if not_imported and not imported:
+        if not_imported:
             self.stdout.write(
-                self.style.SUCCESS(f"Found, but did not update {len(not_imported)} games (use --full-scan to update)")
+                self.style.SUCCESS(
+                    f"Found, but did not update {len(not_imported)} games for {slug} (use --full-scan to update)"
+                    )
             )
-        else:
+        if not imported and not not_imported:
             self.stdout.write(
                 self.style.ERROR(
-                    "No games imported, check for gamelist.xml file or re-run scraper"
+                    "No games imported for {slug}, check for gamelist.xml file or re-run scraper"
                 )
             )
+
+    def handle(self, *args, **options):
+        game_system_slug = options["system"]
+        if not game_system_slug:
+            self.style.ERROR("Please provide a game system slug, or all to import from all systems")
+            return False
+        if game_system_slug == "all":
+            for slug in settings.GAME_SYSTEM_SLUG_MAP.keys():
+                self.import_from_slug(slug, options["file"], options["full_scan"])
+        else:
+            self.import_from_slug(game_system_slug, options["file"], options["full_scan"])
+
+

+ 14 - 2
games/management/commands/scrape_roms.py

@@ -1,4 +1,5 @@
 from django.core.management.base import BaseCommand, CommandError
+from django.conf import settings
 
 from games.utils import skyscrape_console
 
@@ -10,5 +11,16 @@ class Command(BaseCommand):
         parser.add_argument("system", type=str)
 
     def handle(self, *args, **options):
-        scrape_out, load_out = skyscrape_console(options["system"])
-        self.stdout.write(self.style.SUCCESS(f"Successfully scraped roms"))
+        game_system_slug = options["system"]
+        if not game_system_slug:
+            self.stdout.write(self.style.ERROR(f"No game system, or all specified")
+            return False
+
+        if game_system_slug == "all":
+            for slug in settings.GAME_SYSTEM_SLUG_MAP.keys():
+                scrape_out, load_out = skyscrape_console(slug)
+                self.stdout.write(self.style.SUCCESS(f"Successfully scraped roms for {slug}"))
+
+        else:
+            scrape_out, load_out = skyscrape_console(game_system_slug)
+            self.stdout.write(self.style.SUCCESS("Successfully scraped roms for {slug}"))