Skip to content

Helpers

psengine.playbook_alerts.helpers

save_pba_images

save_pba_images(
    playbook_alerts: Union[
        PBA_DomainAbuse, list[PBA_DomainAbuse]
    ],
    output_directory: str = DEFAULT_ALERTS_OUTPUT_DIR,
) -> None

Save Domain Abuse images/screenshots to disk as a .png file.

PARAMETER DESCRIPTION
playbook_alerts

Domain Abuse alert or a list of Domain Abuse alerts.

TYPE: Union[PBA_DomainAbuse, list[PBA_DomainAbuse]]

output_directory

A directory to save the images to.

TYPE: str DEFAULT: DEFAULT_ALERTS_OUTPUT_DIR

RAISES DESCRIPTION
TypeError

If alerts are not PBA_DomainAbuse objects.

WriteFileError

If the image save fails with an OSError.

Source code in psengine/playbook_alerts/helpers.py
@debug_call
def save_pba_images(
    playbook_alerts: Annotated[
        Union[PBA_DomainAbuse, list[PBA_DomainAbuse]],
        Doc('Domain Abuse alert or a list of Domain Abuse alerts.'),
    ],
    output_directory: Annotated[
        str, Doc('A directory to save the images to.')
    ] = DEFAULT_ALERTS_OUTPUT_DIR,
) -> None:
    """Save Domain Abuse images/screenshots to disk as a `.png` file.

    Raises:
        TypeError: If alerts are not `PBA_DomainAbuse` objects.
        WriteFileError: If the image save fails with an `OSError`.
    """
    if not isinstance(playbook_alerts, (list, PBA_DomainAbuse)):
        raise TypeError('Image saving is only supported by Domain Abuse alerts')

    playbook_alerts = playbook_alerts if isinstance(playbook_alerts, list) else [playbook_alerts]
    if not all(isinstance(alert, PBA_DomainAbuse) for alert in playbook_alerts):
        raise TypeError('Image saving is only supported by Domain Abuse alerts')

    for alert in playbook_alerts:
        LOG.info(f'Saving {len(alert.images)} image(s) to disk for alert {alert.playbook_alert_id}')
        for image_id, meta in alert.images.items():
            file_name = f'{alert.playbook_alert_id[5:]}_{image_id[4:]}'
            _save_image(
                file_name=file_name,
                image_bytes=meta['image_bytes'],
                output_directory=output_directory,
            )