forked from revdotcom/revai-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcript.go
94 lines (75 loc) · 2.34 KB
/
transcript.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package revai
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
)
// TranscriptService provides access to the transcript related functions
// in the Rev.ai API.
type TranscriptService service
// Transcript represents a Rev.ai job json transcript
type Transcript struct {
Monologues []Monologue `json:"monologues"`
}
// Monologue represents a Rev.ai monologue
type Monologue struct {
Speaker int `json:"speaker"`
Elements []Element `json:"elements"`
}
// Element represents a Rev.ai element
type Element struct {
Type string `json:"type"`
Value string `json:"value"`
Ts float64 `json:"ts"`
EndTs float64 `json:"end_ts"`
Confidence float64 `json:"confidence"`
}
// GetTranscriptParams specifies the parameters to the
// TranscriptService.Get method.
type GetTranscriptParams struct {
JobID string
}
// Get returns the transcript for a completed transcription job in JSON format.
// https://www.rev.ai/docs#operation/GetTranscriptById
func (s *TranscriptService) Get(ctx context.Context, params *GetTranscriptParams) (*Transcript, error) {
urlPath := "/speechtotext/v1/jobs/" + params.JobID + "/transcript"
req, err := s.client.newRequest(http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("failed creating request %w", err)
}
req.Header.Add("Accept", RevTranscriptJSONHeader)
var transcript Transcript
if err := s.client.doJSON(ctx, req, &transcript); err != nil {
return nil, err
}
return &transcript, nil
}
// TextTranscript represents a Rev.ai job text transcript
type TextTranscript struct {
Value string
}
// Get returns the transcript for a completed transcription job in text format.
// https://www.rev.ai/docs#operation/GetTranscriptById
func (s *TranscriptService) GetText(ctx context.Context, params *GetTranscriptParams) (*TextTranscript, error) {
urlPath := "/speechtotext/v1/jobs/" + params.JobID + "/transcript"
req, err := s.client.newRequest(http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("failed creating request %w", err)
}
req.Header.Add("Accept", RevTranscriptJSONHeader)
resp, err := s.client.do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, resp.Body); err != nil {
return nil, err
}
transcript := TextTranscript{
Value: buf.String(),
}
return &transcript, nil
}