-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadTranscriptsSummary.cs
More file actions
144 lines (120 loc) · 4.95 KB
/
DownloadTranscriptsSummary.cs
File metadata and controls
144 lines (120 loc) · 4.95 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
public class DownloadTranscriptsSummary
{
private const string OUTPUT_DIR = "transcripts_download";
private const int PAGE_SIZE = 5;
public static async Task FetchAndProcess(string apiKey, string baseUrl)
{
Directory.CreateDirectory(OUTPUT_DIR);
string cursor = null;
int pageNum = 1;
bool hasMore = true;
using var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(30);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apiKey);
while (hasMore)
{
Console.WriteLine($"\n--- Fetching Page {pageNum} (Cursor: {cursor ?? "None"}) ---");
var queryParams = new List<string> { $"limit={PAGE_SIZE}" };
if (!string.IsNullOrEmpty(cursor))
{
queryParams.Add($"cursor={cursor}");
}
string queryString = string.Join("&", queryParams);
string url = $"{baseUrl}/calls/?{queryString}";
try
{
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
var data = await response.Content.ReadFromJsonAsync<CallsResponse>();
if (data == null)
{
Console.WriteLine("Failed to parse response.");
break;
}
var calls = data.Calls ?? new List<Call>();
cursor = data.Cursor;
hasMore = data.HasMore;
if (calls.Count == 0)
{
Console.WriteLine("No calls found on this page.");
}
foreach (var call in calls)
{
string summary = call.ExecutiveSummary ?? "No summary available";
if (summary.Length > 100) summary = summary.Substring(0, 100) + "...";
Console.WriteLine($"\n[Call {call.CallId}]");
Console.WriteLine($"Summary: {summary}");
if (call.Transcript != null && !string.IsNullOrEmpty(call.Transcript.TranscriptUrl))
{
try
{
// Use a separate client without default headers (specifically Authorization)
// because sending the Bearer token to S3/R2 presigned URLs causes 400 errors.
using var downloadClient = new HttpClient();
var tResponse = await downloadClient.GetAsync(call.Transcript.TranscriptUrl);
if (!tResponse.IsSuccessStatusCode)
{
var errorContent = await tResponse.Content.ReadAsStringAsync();
Console.WriteLine($"Failed to download transcript. Status: {tResponse.StatusCode}, Response: {errorContent}");
}
else
{
string content = await tResponse.Content.ReadAsStringAsync();
string filename = Path.Combine(OUTPUT_DIR, $"{call.CallId}.md");
await File.WriteAllTextAsync(filename, content);
Console.WriteLine($"Saved transcript to {filename}");
}
}
catch (Exception e)
{
Console.WriteLine($"Failed to download transcript: {e.Message}");
}
}
else
{
Console.WriteLine("No transcript URL available.");
}
}
pageNum++;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
break;
}
}
}
}
public class CallsResponse
{
[JsonPropertyName("calls")]
public List<Call> Calls { get; set; }
[JsonPropertyName("cursor")]
public string Cursor { get; set; }
[JsonPropertyName("hasMore")]
public bool HasMore { get; set; }
}
public class Call
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("callId")]
public string CallId { get; set; }
[JsonPropertyName("executiveSummary")]
public string ExecutiveSummary { get; set; }
[JsonPropertyName("transcript")]
public TranscriptInfo Transcript { get; set; }
}
public class TranscriptInfo
{
[JsonPropertyName("transcriptUrl")]
public string TranscriptUrl { get; set; }
}