Use this file to discover all available pages before exploring further.
This tutorial guides you through the process of scheduling a DELETE request for a transcript created with AssemblyAI’s transcription service, utilizing EasyCron for scheduling.
import assemblyai as aaiimport requestsfrom datetime import datetime, timedelta, timezone# Set up AssemblyAI API key# In production, store this in an environment variableaai.settings.api_key = "YOUR_ASSEMBLYAI_API_KEY"# Get an EasyCron API key here: https://www.easycron.com/user/token# Set up EasyCron API key# In production, store this in an environment variableEASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"# Create transcriber instance and transcribe audioconfig = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])transcriber = aai.Transcriber()transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)# Get the transcript IDtranscript_id = transcript.id# Construct the URL for the DELETE requesturl_to_call = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"# Calculate the time 24 hours from now for the cron expressiontime_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)cron_minute = time_24_hours_from_now.minutecron_hour = time_24_hours_from_now.hourcron_day = time_24_hours_from_now.daycron_month = time_24_hours_from_now.monthcron_year = time_24_hours_from_now.year# Create the cron expressioncron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'# EasyCron API endpoint for creating a new cron jobapi_endpoint = 'https://www.easycron.com/rest/add'# Data payload for EasyCron APIdata = { 'token': EASYCRON_API_TOKEN, 'url': url_to_call, 'cron_expression': cron_expression, 'http_method': "DELETE", 'http_headers': f'Authorization: {aai.settings.api_key}', 'timezone': 'UTC'}# Make the request to EasyCron's APIresponse = requests.post(api_endpoint, data=data)# Print the response from EasyCronprint("EasyCron API Response:")print(response.text)
To get started, install the AssemblyAI Python SDK.
pip install "assemblyai"
Finally, import the assemblyai package and set your API token in the settings:
import assemblyai as aai# set the API keyaai.settings.api_key = f"{YOUR_API_KEY}"
config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])transcriber = aai.Transcriber()# this is just an example filetranscript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)
Store the transcript ID
transcript_id = transcript.id
Using this, we now construct the URL that we want our cron job to call.
First, sign up for an account with EasyCron here. Locate your EasyCron API key in your user dashboard.Then, insert your EasyCron API key into your code.Next, we will use the datetime module to generate a cron expression for 24 hours from now (although you can configure the timedelta argument to be whatever you want)
# In production, you should store your API keys in environment variablesEASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"from datetime import datetime, timedelta, timezone# Calculate the time 24 hours from now in EasyCron's expected format# EasyCron uses a slightly different format, but for simplicity, we'll use the standard UTC format# and convert this into a cron expressiontime_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)cron_minute = time_24_hours_from_now.minutecron_hour = time_24_hours_from_now.hourcron_day = time_24_hours_from_now.daycron_month = time_24_hours_from_now.monthcron_year = time_24_hours_from_now.yearcron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'
Now, we will schedule a cron job 24 hours from now with EasyCron to send a delete request to AssemblyAI for the transcript that we just generated.
import requests# EasyCron API endpoint for creating a new cron jobapi_endpoint = 'https://www.easycron.com/rest/add'# The data to be sent to EasyCron's APIdata = { 'token': EASYCRON_API_TOKEN, 'url': url_to_call, 'cron_expression': cron_expression, 'http_method': "DELETE", 'http_headers': f'Authorization: {AAI_API_TOKEN}', 'timezone': 'UTC'}# Make the request to EasyCron's APIresponse = requests.post(api_endpoint, data=data)# Print the response from EasyCron's APIprint(response.text)
Error 3: The cron expression you entered is invalid or it cannot be matched in a realitic future.: Check that your EasyCron account settings has UTC configured as the default timezone.