from typing import Dict
from typing import Any
from typing import Optional


def get_name(hook: Dict[Any, Any]) -> Optional[str]:
    """
    Creates a name based on the webhook call it recieved
    Format: Timestamp_Devicename.mp4
    Removes any characters that are not regular characters, numbers, '_' '.' or '-'
    to ensure the filename is valid
    :param hook: Dict with webhook response
    :return: filename: str, None if invalid hook failed
    """
    if 'extraInfo' not in hook:
        return
    timestamp = hook.get('createdOn', '').replace(':', '-')
    device_name = hook['extraInfo'].get('Device name', '').replace(' ', '_')
    if timestamp == '' or device_name == '':
        return
    name = f'{timestamp}_{device_name}.mp4'
    # https://stackoverflow.com/questions/7406102/create-sane-safe-filename-from-any-unsafe-string
    file_name = "".join([c for c in name if
                         c.isalpha()
                         or c.isdigit()
                         or c == '_'
                         or c == '.'
                         or c == '-']).rstrip()
    return file_name