Skip to main content
Skip table of contents

Roundtrip Example Code

Here is some example code, demonstrating how to:

  1. GET audio data from a DigAS Entry
  2. Send entry to a (simulated) transcription service
  3. Receive back (simulated) transformation of response data to DAVID S2T Transcription Data Format
  4. POST new transcription onto original DigAS Entry

The code below is also available for download as a file: Roundtrip Example Code

See clearS2T for implementation of Roundtrip Example Code, a DPE Workflow Template which should be used to "clear" the targeted Entry after each S2T POST attempt, to avoid having a mismatch between the filename of the created .s2t and the other files of the Entry.

Audio-Transcription-S2T Workflow Logic

s2t-workflow.js

JS
export const s2tWorkflow = async (dpeServiceRoot = 'https://newmedia.davidsystems.com/DpeWebApplication', tableId = 'NewMedia\\Test', entryId = 87, dpeToken, useMain = false, simTranscriptionServiceTableId = 'NewMedia\\Production', simTranscriptionServiceEntryId = 60) => {

  // returns the filename property of an Entry, without the extension
  // used to control the filename for the S2T...
  // ... these must match in order to enable Express Editing functionality
  const getFilenameBase = async () => {
    const url = `${dpeServiceRoot}/api/entries/${entryId}?tableId=${tableId}&fields=filename`;
    const entry = await fetch(url, {
      headers: {
        'Accept': 'application/json',
        'Dpe-Auth': `DpeToken ${dpeToken}`
      }
    }).then(res => res.json());

    const filename = entry.filename.split('\\').pop();
    const filenameBase = filename.split('.')[0];
    return filenameBase;
  }

  // get audio data for an Entry
  const getAudioData = async () => {
    const url = `${dpeServiceRoot}/Media.ashx?tableId=${tableId}&entryId=${entryId}&dpe-auth=${dpeToken}${useMain ? '&main=true' : ''}`;
    const audioData = await fetch(url, {}).then(res => res.blob());
    return audioData;
  }

  // using existing data to simulate a transcription service response
  const getTranscriptFromTranscriptionService = async (audioData, myTranscriptionServiceCoords) => {
    // getting an existing DAVID S2T (JSON), just to simulate
    // you would do something else, according to whatever service you use
    const url = `https://newmedia.davidsystems.com/DpeWebApplication/DownloadMedium.ashx?tableId=${simTranscriptionServiceTableId}&entryId=${simTranscriptionServiceEntryId}&mediumType=Data.SpeechToText&dpe-auth=${dpeToken}`;
    const transcriptResponseData = await fetch(url, {}).then(res => res.blob());
    return transcriptResponseData
  }

  // transform whatever data you get from your transcription service, to DAVID S2T format
  const transformTranscriptDataToS2T = async (transcriptResponseData) => {
    // simulated transformation...
    const s2tData = await transcriptResponseData;
    return s2tData;
  }

  // POST S2T JSON data to create a new Medium on the Entry
  // returns an id for the new Medium
  const uploadS2TtoEntry = async (s2tData) => {
    const filenameBase = await getFilenameBase();

    const formData = new FormData();
    formData.append('uploadFile', s2tData, `${filenameBase}.s2t`);

    const url = `${dpeServiceRoot}/UploadMedium.ashx?tableid=${tableId}&entryId=${entryId}&dpe-auth=${dpeToken}`;
    const uploadMediumResponse = await fetch(url, {
      method: 'POST',
      body: formData
    });
    const s2tMediumId = uploadMediumResponse.json();
    return s2tMediumId;
  }

  const audioData = await getAudioData()
  const transcriptResponseData = await getTranscriptFromTranscriptionService(audioData, {});
  const s2tData = await transformTranscriptDataToS2T(transcriptResponseData);
  const s2tMediumId = await uploadS2TtoEntry(s2tData, dpeServiceRoot, tableId, entryId, dpeToken);
  console.log(s2tMediumId);
}

// call the ClearS2T workflow template to prepare the target Entry for a new POST request
export const clearS2T = async (dpeServiceRoot, tableId, entryId, dpeToken) => {
  const data = {
    'Title': `ClearS2TMedium on ${tableId}#${entryId}`,
    'WorkflowType': 'ClearS2T',
    'RawInputArguments': `<Arguments><Arg_TableId type="System.String">${tableId}</Arg_TableId><Arg_EntryId type="System.Int32">${entryId}</Arg_EntryId></Arguments>`,
    'CreatingComponent': 'MyDpeServicesApp'
  };
  console.log(data);
  const url = `${dpeServiceRoot}/api/workflows`
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Dpe-Auth': `DpeToken ${dpeToken}`
    },
    body: JSON.stringify(data)
  });
  const workflowId = await response.json();
  console.log(workflowId);
}


// utility for clear DPE tokens
export const getDpeToken = (userName, password, computerName) => {
  const date = new Date
  const dateString = date.getFullYear() + date.getMonth().toString().padStart(2, '0') + date.getDay().toString().padStart(2, '0') + date.getHours().toString().padStart(2, '0') + date.getMinutes().toString().padStart(2, '0') + date.getSeconds().toString().padStart(2, '0') + date.getMilliseconds().toString().padStart(3, '0');
  const token = btoa(dateString + ':' + computerName + ':' + userName + ':' + password);
  return token;
};

Example code for host HTML and main script

XML
<input type="checkbox" id="doItMain" checked>
<button id="doItButton">Do it</button>
<button id="clearItButton">Clear it</button>
JS
import { getDpeToken, s2tWorkflow, clearS2T } from './app-helpers/s2t-workflow.js';
const doItButton = document.querySelector('#doItButton');
doItButton.addEventListener('click', async (e) => {
  const dpeToken = getDpeToken('apiuser', 'apiuser', 'SHA-MBP');
  s2tWorkflow('https://newmedia.davidsystems.com/DpeWebApplication', 'NewMedia\\Test', 87, dpeToken);
});
const clearItButton = document.querySelector('#clearItButton');
clearItButton.addEventListener('click', async (e) => {
  const dpeToken = getDpeToken('apiuser', 'apiuser', 'SHA-MBP');
  clearS2T('https://newmedia.davidsystems.com/DpeWebApplication', 'NewMedia\\Test', 87, dpeToken);
});
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.