get_test_zip.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # get_test_zip.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Wed Oct 19 15:33:20 UTC 2022
  3. import sys
  4. import json
  5. import enum
  6. import logging
  7. import argparse
  8. import subprocess
  9. from pathlib import Path
  10. from typing import Final, Optional
  11. import requests
  12. logging.basicConfig(level=logging.INFO)
  13. logger = logging.getLogger(__name__)
  14. class Camera(enum.Enum):
  15. HERO_9 = "HERO9 Black"
  16. HERO_10 = "HERO10 Black"
  17. HERO_11 = "HERO11 Black"
  18. cameras: Final[list[str]] = [camera.name for camera in Camera]
  19. FW_CATALOG: Final = r"https://api.gopro.com/firmware/v2/catalog"
  20. def parse_arguments() -> argparse.Namespace:
  21. parser = argparse.ArgumentParser(
  22. description="Get the firmware catalog JSON, extract the camera OTA URL, and download the .zip"
  23. )
  24. parser.add_argument("camera", type=str, choices=cameras, help=f"Camera to get FW for")
  25. parser.add_argument("-d", "--debug", action="store_true", help="Set to output debug information")
  26. return parser.parse_args()
  27. def main(args: argparse.Namespace) -> None:
  28. # Configure logging
  29. if args.debug:
  30. logger.setLevel(logging.DEBUG)
  31. # Get firmware catalog
  32. target = eval(f"Camera.{args.camera}").value
  33. logger.info(f"Getting .zip for camera: {target}")
  34. with requests.get(FW_CATALOG, timeout=10) as response:
  35. response.raise_for_status()
  36. catalog = json.loads(response.text)
  37. if logger.level == logging.DEBUG:
  38. logger.debug(json.dumps(catalog, indent=4))
  39. # Extract link for camera-specific firmware
  40. fw_url: Optional[str] = None
  41. for camera in catalog["cameras"]:
  42. if camera["name"] == target:
  43. fw_url = camera["url"]
  44. logger.info(f"FW URL is: {fw_url}")
  45. break
  46. else:
  47. raise RuntimeError("Firmware URL not found in firmware catalog")
  48. # Download zip
  49. assert fw_url
  50. local_filename = Path("UPDATE.zip")
  51. logger.info(f"Downloading zip to {local_filename}")
  52. with requests.get(fw_url, stream=True) as response:
  53. response.raise_for_status()
  54. with open(local_filename, "wb") as fp:
  55. for chunk in response.iter_content(chunk_size=8192):
  56. fp.write(chunk)
  57. if __name__ == "__main__":
  58. main(parse_arguments())