Repository navigation
Expand file tree
/
Copy pathexample.dart
More file actions
51 lines (47 loc) · 1.65 KB
/
Copy pathexample.dart
File metadata and controls
51 lines (47 loc) · 1.65 KB
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
import 'dart:io';
import 'dart:math';
import 'package:transcriptdock/transcriptdock.dart';
/// Returns a random key for one intended submission.
String newIdempotencyKey() {
final random = Random.secure();
final bytes = List<int>.generate(16, (_) => random.nextInt(256));
return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();
}
Future<void> main() async {
final apiKey = Platform.environment['TRANSCRIPTDOCK_API_KEY'];
if (apiKey == null || apiKey.isEmpty) {
stderr.writeln('Set TRANSCRIPTDOCK_API_KEY first.');
exitCode = 64;
return;
}
final client = TranscriptDock(apiKey);
try {
print(await client.capabilities());
// Generate once per intended operation. Save this key and the job ID.
// Reuse the same key and exact body if a submission's response is lost.
final submissionKey = newIdempotencyKey();
final body = {
'source': {'url': 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'},
'mode': 'captions_only',
'max_credits': 1,
};
var job = await client.createJob(body, idempotencyKey: submissionKey);
try {
job = await client.waitForJob(job['id'] as String);
} on JobTimeoutError catch (error) {
print('Still running: ${error.job}');
return;
}
if (job['status'] == 'succeeded') {
final resultId = job['result_id'] as String;
print(await client.getTranscript(resultId));
print(await client.exportTranscript(resultId, format: 'srt'));
} else {
print('${job['status']} ${job['error']}');
}
} on ApiError catch (error) {
print('${error.code} ${error.status} ${error.requestId}');
} finally {
client.close();
}
}