import_gamelist_xml_file.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.conf import settings
  2. from django.core.management.base import BaseCommand
  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(
  22. "No games imported for {slug}, check for gamelist.xml file or re-run scraper"
  23. )
  24. return
  25. imported = results["imported"]
  26. not_imported = results["not_imported"]
  27. if imported:
  28. self.stdout.write(
  29. self.style.SUCCESS(
  30. f"Successfully imported {len(imported)} games for {slug}"
  31. )
  32. )
  33. if not_imported:
  34. self.stdout.write(
  35. self.style.SUCCESS(
  36. f"Found, but did not update {len(not_imported)} games for {slug} (use --full-scan to update)"
  37. )
  38. )
  39. if not imported and not not_imported:
  40. self.stdout.write(
  41. self.style.ERROR(
  42. "No games imported for {slug}, check for gamelist.xml file or re-run scraper"
  43. )
  44. )
  45. def handle(self, *args, **options):
  46. game_system_slug = options["system"]
  47. if not game_system_slug:
  48. self.style.ERROR(
  49. "Please provide a game system slug, or all to import from all systems"
  50. )
  51. return False
  52. if game_system_slug == "all":
  53. for slug in settings.GAME_SYSTEM_DEFAULTS.keys():
  54. self.import_from_slug(slug, options["file"], options["full_scan"])
  55. else:
  56. self.import_from_slug(
  57. game_system_slug, options["file"], options["full_scan"]
  58. )