import hmac
import requests
import hashlib
import json
from datetime import datetime, timezone

accesskey = 'QY4Tuiug6XidAKjDS5zTQHGSI'
access_secret = '62A03zCgPflc3NZwFHliphpKFt4tppOpdmUHgqPR'

text = 'これはテストです。'
date: str = str(int(datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()))
data: str = json.dumps({
    'coefont': '2b174967-1a8a-42e4-b1ae-5f6548cfa05d',
    'text': text
})
signature = hmac.new(bytes(access_secret, 'utf-8'), (date+data).encode('utf-8'), hashlib.sha256).hexdigest()

response = requests.post('https://api.coefont.cloud/v2/text2speech', data=data, headers={
    'Content-Type': 'application/json',
    'Authorization': accesskey,
    'X-Coefont-Date': date,
    'X-Coefont-Content': signature
})

if response.status_code == 200:
    with open('response.wav', 'wb') as f:
        f.write(response.content)
else:
    print(response.json())
