Skip to content

Commit bf7d3a6

Browse files
authored
fix - token bug, srt time format (#89)
* fix - token bug, srt time format * fix - summary bug * fix - tests * fix - message * fix - prints and comment
1 parent eb20c63 commit bf7d3a6

8 files changed

Lines changed: 87 additions & 29 deletions

File tree

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var configCmd = &cobra.Command{
3030

3131
checkToken := U.CheckIfTokenValid()
3232
if !checkToken {
33-
fmt.Println("Your token appears to be invalid. Try again, and if the problem persists, contact support at support@assemblyai.com")
33+
fmt.Println(U.INVALID_TOKEN)
3434
return
3535
}
3636

cmd/get.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ var getCmd = &cobra.Command{
4343
return
4444
}
4545

46+
checkToken := U.CheckIfTokenValid()
47+
if !checkToken {
48+
printErrorProps := S.PrintErrorProps{
49+
Error: errors.New("Invalid token"),
50+
Message: U.INVALID_TOKEN,
51+
}
52+
U.PrintError(printErrorProps)
53+
return
54+
}
55+
4656
U.PollTranscription(id, flags)
4757
},
4858
}

main_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
S "github.com/AssemblyAI/assemblyai-cli/schemas"
1010
"github.com/AssemblyAI/assemblyai-cli/utils"
11+
U "github.com/AssemblyAI/assemblyai-cli/utils"
1112
)
1213

1314
func TestVersion(t *testing.T) {
@@ -39,7 +40,7 @@ func TestAuthBad(t *testing.T) {
3940
if err != nil {
4041
fmt.Println(err)
4142
}
42-
if string(out) != "Your token appears to be invalid. Try again, and if the problem persists, contact support at support@assemblyai.com\n" {
43+
if string(out) != U.INVALID_TOKEN+"\n" {
4344
t.Errorf("Expected Something just went wrong. Please try again., got %s.", string(out))
4445
}
4546
}
@@ -60,7 +61,7 @@ func TestTranscribeInvalidFlags(t *testing.T) {
6061
if err != nil {
6162
fmt.Println(err)
6263
}
63-
if string(out) != "requires at least 1 arg(s), only received 0\n" {
64+
if string(out) != "\nrequires at least 1 arg(s), only received 0\n" {
6465
t.Errorf("Expected requires at least 1 arg(s), only received 0, got %s.", string(out))
6566
}
6667
}
@@ -70,7 +71,7 @@ func TestTranscribeBadYoutube(t *testing.T) {
7071
if err != nil {
7172
fmt.Println(err)
7273
}
73-
if string(out) != "Could not find YouTube ID in URL\n" {
74+
if string(out) != "\nCould not find YouTube ID in URL\n" {
7475
t.Errorf("Expected Could not find YouTube ID in URL, got %s.", string(out))
7576
}
7677
}
@@ -80,7 +81,7 @@ func TestTranscribeBadFile(t *testing.T) {
8081
if err != nil {
8182
fmt.Println(err)
8283
}
83-
if string(out) != "Error opening file\n" {
84+
if string(out) != "\nError opening file\n" {
8485
t.Errorf("Expected Error opening file, got %s.", string(out))
8586
}
8687
}
@@ -165,7 +166,7 @@ func TestTranscribeRestrictions(t *testing.T) {
165166
if err != nil {
166167
fmt.Println(err)
167168
}
168-
if string(out) != "Speaker labels are not supported for dual channel audio\n" {
169+
if string(out) != "\nSpeaker labels are not supported for dual channel audio\n" {
169170
t.Errorf("Expected Speaker labels are not supported for dual channel audio, got %s.", string(out))
170171
}
171172

@@ -185,7 +186,7 @@ func TestTranscribeRestrictions(t *testing.T) {
185186
if err != nil {
186187
fmt.Println(err)
187188
}
188-
if string(out) != "Auto chapters are not supported for summarization\n" {
189+
if string(out) != "\nAuto chapters are not supported for summarization\n" {
189190
t.Errorf("Expected Auto chapters are not supported for summarization, got %s.", string(out))
190191
}
191192

@@ -205,7 +206,7 @@ func TestTranscribeRestrictions(t *testing.T) {
205206
if err != nil {
206207
fmt.Println(err)
207208
}
208-
if string(out) != "Please provide either language detection or language code, not both.\n" {
209+
if string(out) != "\nPlease provide either language detection or language code, not both.\n" {
209210
t.Errorf("Expected Please provide either language detection or language code, not both., got %s.", string(out))
210211
}
211212

@@ -225,7 +226,7 @@ func TestTranscribeRestrictions(t *testing.T) {
225226
if err != nil {
226227
fmt.Println(err)
227228
}
228-
if string(out) != "Speaker labels are not supported for languages other than English.\n" {
229+
if string(out) != "\nSpeaker labels are not supported for languages other than English.\n" {
229230
t.Errorf("Expected Speaker labels are not supported for languages other than English., got %s.", string(out))
230231
}
231232
}

schemas/types.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type TranscriptResponse struct {
6565
SpeedBoost *bool `json:"speed_boost,omitempty"`
6666
Status *string `json:"status,omitempty"`
6767
Summarization *bool `json:"summarization,omitempty"`
68-
Summary *string `json:"summary,omitempty"`
68+
Summary *interface{} `json:"summary,omitempty"`
6969
SummaryType *string `json:"summary_type,omitempty"`
7070
Text *string `json:"text,omitempty"`
7171
Throttled interface{} `json:"throttled"`
@@ -403,3 +403,11 @@ var ValidFileTypes = []string{
403403
"m4v",
404404
"mxf",
405405
}
406+
407+
type SummaryObject struct {
408+
Summary string `json:"summary"`
409+
Headline string `json:"headline"`
410+
Gist string `json:"gist"`
411+
Start int `json:"start"`
412+
End int `json:"end"`
413+
}

utils/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func CheckIfTokenValid() bool {
3232
Message: "Something went wrong. Please try again.",
3333
}
3434
PrintError(printErrorProps)
35+
return false
3536
}
3637
if result.Error != nil {
3738
return false

utils/messages.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package utils
2+
3+
var INVALID_TOKEN = "Your token is invalid. Try running assemblyai config [new_token], and if the problem persists, contact support at support@assemblyai.com"

utils/transcribe.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ func Transcribe(params S.TranscribeParams, flags S.TranscribeFlags) {
3434
return
3535
}
3636

37+
checkToken := CheckIfTokenValid()
38+
if !checkToken {
39+
printErrorProps := S.PrintErrorProps{
40+
Error: errors.New("Invalid token"),
41+
Message: INVALID_TOKEN,
42+
}
43+
PrintError(printErrorProps)
44+
return
45+
}
46+
3747
if isUrl(params.AudioURL) {
3848
if isYoutubeLink(params.AudioURL) {
3949
if isYoutubeShortLink(params.AudioURL) {
@@ -248,6 +258,7 @@ func PollTranscription(id string, flags S.TranscribeFlags) {
248258
s.Stop()
249259
return
250260
}
261+
251262
if transcript.Error != nil {
252263
s.Stop()
253264
fmt.Println(*transcript.Error)
@@ -355,7 +366,7 @@ func getFormattedOutput(transcript S.TranscriptResponse, flags S.TranscribeFlags
355366
}
356367
if transcript.Summarization != nil && *transcript.Summarization == true {
357368
fmt.Fprintf(os.Stdin, "\033[1m%s\033[0m\n", "Summary")
358-
summaryPrintFormatted(transcript.Summary)
369+
summaryPrintFormatted(*transcript.Summary)
359370
}
360371
if flags.Srt {
361372
srtDownloadTranscript(*transcript.ID, transcript.Words)
@@ -391,7 +402,7 @@ func dualChannelPrintFormatted(utterances *[]S.SentimentAnalysisResult) {
391402
table.Wrap = true
392403
table.MaxColWidth = uint(width - 21)
393404
for _, utterance := range *utterances {
394-
start := TransformMsToTimestamp(*utterance.Start)
405+
start := TransformMsToTimestamp(*utterance.Start, false)
395406
speaker := fmt.Sprintf("(Channel %s)", utterance.Channel)
396407

397408
sentences := SplitSentences(utterance.Text, false)
@@ -533,8 +544,8 @@ func chaptersPrintFormatted(chapters *[]S.Chapter) {
533544
table.MaxColWidth = uint(width - 19)
534545
table.Separator = " |\t"
535546
for _, chapter := range *chapters {
536-
start := TransformMsToTimestamp(*chapter.Start)
537-
end := TransformMsToTimestamp(*chapter.End)
547+
start := TransformMsToTimestamp(*chapter.Start, false)
548+
end := TransformMsToTimestamp(*chapter.End, false)
538549
table.AddRow("| timestamp", fmt.Sprintf("%s-%s", start, end))
539550
table.AddRow("| Gist", chapter.Gist)
540551
table.AddRow("| Headline", chapter.Headline)
@@ -576,7 +587,7 @@ func entityDetectionPrintFormatted(entities *[]S.Entity) {
576587
fmt.Println()
577588
}
578589

579-
func summaryPrintFormatted(summary *string) {
590+
func summaryPrintFormatted(summary interface{}) {
580591
if summary == nil {
581592
fmt.Println("Could not retrieve summary")
582593
return
@@ -586,10 +597,24 @@ func summaryPrintFormatted(summary *string) {
586597
table.MaxColWidth = uint(width - 20)
587598
table.Separator = " |\t"
588599

589-
table.AddRow(*summary)
600+
// summary could be either a string or Array<Record<string, string | number>>
601+
// check if summary is a string
602+
if _, ok := (summary).(string); ok {
603+
table.AddRow(summary)
604+
} else {
605+
summaryArray := (summary).([]interface{})
606+
for _, row := range summaryArray {
607+
row := row.(map[string]interface{})
608+
table.AddRow("| Headline", row["headline"].(string))
609+
table.AddRow("| Gist", row["gist"].(string))
610+
table.AddRow("| Summary", row["summary"].(string))
611+
table.AddRow("", "")
612+
}
613+
}
590614

591615
fmt.Println(table)
592616
fmt.Println()
617+
593618
}
594619

595620
func srtDownloadTranscript(id string, words []S.SentimentAnalysisResult) {

utils/utils.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func PrintError(props S.PrintErrorProps) {
126126
sentry.CaptureException(err)
127127
}
128128
}
129-
fmt.Println(message)
129+
fmt.Printf("\n%s\n", message)
130130
os.Exit(1)
131131
}
132132
}
@@ -245,8 +245,17 @@ var splitExceptions = [][]string{
245245
{".com", "{{}}com"},
246246
}
247247

248-
func TransformMsToTimestamp(ms int64) string {
249-
duration := time.Duration(ms) * time.Millisecond
248+
func TransformMsToTimestamp(n int64, showMs bool) string {
249+
if showMs {
250+
ms := n % 1000
251+
n /= 1000
252+
sec := n % 60
253+
n /= 60
254+
min := n % 60
255+
n = n / 60
256+
return fmt.Sprintf("%02d:%02d:%02d,%03d", n, min, sec, ms)
257+
}
258+
duration := time.Duration(n) * time.Millisecond
250259
return fmt.Sprintf("%02d:%02d", int(duration.Minutes()), int(duration.Seconds())%60)
251260
}
252261

@@ -255,27 +264,27 @@ func GetSentenceTimestamps(sentences []string, words []S.SentimentAnalysisResult
255264
timestamps := []string{}
256265
for index, sentence := range sentences {
257266
if index == 0 {
258-
timestamps = append(timestamps, TransformMsToTimestamp(*words[0].Start))
267+
timestamps = append(timestamps, TransformMsToTimestamp(*words[0].Start, false))
259268
lastIndex = 0
260269
} else {
261270
sentenceWords := strings.Split(sentence, " ")
262271
for i := lastIndex; i < len(words); i++ {
263272
if strings.Contains(sentence, words[i].Text) {
264273
if len(words) >= i+2 {
265274
if words[i].Text == sentenceWords[0] && words[i+1].Text == sentenceWords[1] && words[i+2].Text == sentenceWords[2] {
266-
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start))
275+
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start, false))
267276
lastIndex = i
268277
break
269278
}
270279
} else if len(words) >= i+1 {
271280
if words[i].Text == sentenceWords[0] && words[i+1].Text == sentenceWords[1] {
272-
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start))
281+
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start, false))
273282
lastIndex = i
274283
break
275284
}
276285
} else {
277286
if words[i].Text == sentenceWords[0] {
278-
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start))
287+
timestamps = append(timestamps, TransformMsToTimestamp(*words[i].Start, false))
279288
lastIndex = i
280289
break
281290
}
@@ -295,11 +304,12 @@ func GetSrtText(words []S.SentimentAnalysisResult) string {
295304
var start string
296305
for _, word := range words {
297306
if sentence == "" {
298-
start = TransformMsToTimestamp(*word.Start)
307+
start = TransformMsToTimestamp(*word.Start, true)
299308
}
300309
sentence += word.Text + " "
301-
if strings.Contains(word.Text, ".") {
302-
srtText += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", index, start, TransformMsToTimestamp(*word.End), sentence)
310+
if strings.HasSuffix(word.Text, ".") {
311+
312+
srtText += fmt.Sprintf("%d\n%s --> %s\n%s\n\n", index, start, TransformMsToTimestamp(*word.End, true), sentence)
303313
sentence = ""
304314
index++
305315
}
@@ -317,21 +327,21 @@ func GetSentenceTimestampsAndSpeaker(sentences []string, words []S.SentimentAnal
317327
}
318328
if sentence != "" {
319329
if index == 0 {
320-
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[0].Start), fmt.Sprintf("(Speaker %s)", words[0].Speaker)})
330+
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[0].Start, false), fmt.Sprintf("(Speaker %s)", words[0].Speaker)})
321331
lastIndex = 0
322332
} else {
323333
sentenceWords := strings.Split(sentence, " ")
324334
for i := lastIndex; i < len(words); i++ {
325335
if strings.Contains(sentence, words[i].Text) {
326336
if len(words) >= i+1 {
327337
if words[i].Text == sentenceWords[0] {
328-
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[i].Start), fmt.Sprintf("(Speaker %s)", words[i].Speaker)})
338+
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[i].Start, false), fmt.Sprintf("(Speaker %s)", words[i].Speaker)})
329339
lastIndex = i
330340
break
331341
}
332342
} else {
333343
if words[i].Text == sentenceWords[0] && words[i+1].Text == sentenceWords[1] {
334-
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[i].Start), fmt.Sprintf("(Speaker %s)", words[i].Speaker)})
344+
timestamps = append(timestamps, []string{TransformMsToTimestamp(*words[i].Start, false), fmt.Sprintf("(Speaker %s)", words[i].Speaker)})
335345
lastIndex = i
336346
break
337347
}

0 commit comments

Comments
 (0)