import_gamelist_xml_file.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from django.conf import settings
  2. from django.core.management.base import BaseCommand, CommandError
  3. from games.utils import import_gamelist_file_to_db_for_system
  4. class Command(BaseCommand):
  5. help = "Import all games found in a given gamelist XML file"
  6. def add_arguments(self, parser):
  7. parser.add_argument("system", type=str)
  8. parser.add_argument(
  9. "--file",
  10. action="store_true",
  11. help="Import from specific file",
  12. )
  13. parser.add_argument(
  14. "--full-scan",
  15. action="store_true",
  16. help="Update all files, even ones we already know about",
  17. )
  18. def import_from_slug(self, slug, file_path, full_scan=False):
  19. results = import_gamelist_file_to_db_for_system(slug, file_path, full_scan)
  20. if not results:
  21. self.style.ERROR("No games imported for {slug}, check for gamelist.xml file or re-run scraper")
  22. return
  23. imported = results["imported"]
  24. not_imported = results["not_imported"]
  25. if imported:
  26. self.stdout.write(
  27. self.style.SUCCESS(f"Successfully imported {len(imported)} games for {slug}")
  28. )
  29. if not_imported:
  30. self.stdout.write(
  31. self.style.SUCCESS(
  32. f"Found, but did not update {len(not_imported)} games for {slug} (use --full-scan to update)"
  33. )
  34. )
  35. if not imported and not not_imported:
  36. self.stdout.write(
  37. self.style.ERROR(
  38. "No games imported for {slug}, check for gamelist.xml file or re-run scraper"
  39. )
  40. )
  41. def handle(self, *args, **options):
  42. game_system_slug = options["system"]
  43. if not game_system_slug:
  44. self.style.ERROR("Please provide a game system slug, or all to import from all systems")
  45. return False
  46. if game_system_slug == "all":
  47. for slug in settings.GAME_SYSTEM_SLUG_MAP.keys():
  48. self.import_from_slug(slug, options["file"], options["full_scan"])
  49. else:
  50. self.import_from_slug(game_system_slug, options["file"], options["full_scan"])