-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_to_docs.go
More file actions
233 lines (204 loc) · 6.1 KB
/
Copy pathvideo_to_docs.go
File metadata and controls
233 lines (204 loc) · 6.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Video-to-Docs: Full pipeline example (Go)
//
// Submits a video URL, polls for analysis, triggers AI rewrite,
// and downloads the result as markdown, DOCX, and PDF.
//
// Usage:
//
// export DOCSIE_API_KEY="your_key"
// go run video_to_docs.go https://example.com/video.mp4
// go run video_to_docs.go https://example.com/video.mp4 sop
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)
var (
apiKey = os.Getenv("DOCSIE_API_KEY")
baseURL = envOrDefault("DOCSIE_BASE_URL", "https://app.docsie.io")
api = baseURL + "/api_v2/003"
client = &http.Client{Timeout: 60 * time.Second}
)
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
// ── HTTP helpers ─────────────────────────────────────────
func apiRequest(method, path string, body interface{}) (map[string]interface{}, error) {
var reqBody io.Reader
if body != nil {
b, _ := json.Marshal(body)
reqBody = bytes.NewReader(b)
}
req, err := http.NewRequest(method, api+path, reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Api-Key "+apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
return result, nil
}
func apiGet(path string) (map[string]interface{}, error) {
return apiRequest("GET", path, nil)
}
func apiPost(path string, body map[string]interface{}) (map[string]interface{}, error) {
return apiRequest("POST", path, body)
}
func poll(path string, timeoutSec, intervalSec int) (map[string]interface{}, error) {
deadline := time.Now().Add(time.Duration(timeoutSec) * time.Second)
for time.Now().Before(deadline) {
data, err := apiGet(path)
if err != nil {
return nil, err
}
status := str(data, "status")
if status == "" {
status = str(data, "job_status")
}
switch status {
case "done", "failed", "canceled":
return data, nil
}
fmt.Printf(" ... %s\n", status)
time.Sleep(time.Duration(intervalSec) * time.Second)
}
return nil, fmt.Errorf("timed out after %ds", timeoutSec)
}
func downloadFile(url, filename string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
n, _ := io.Copy(f, resp.Body)
fmt.Printf(" Downloaded: %s (%d KB)\n", filename, n/1024)
return nil
}
func str(m map[string]interface{}, key string) string {
if v, ok := m[key]; ok && v != nil {
return fmt.Sprintf("%v", v)
}
return ""
}
func objMap(m map[string]interface{}, key string) map[string]interface{} {
if v, ok := m[key].(map[string]interface{}); ok {
return v
}
return map[string]interface{}{}
}
func arrLen(m map[string]interface{}, key string) int {
if v, ok := m[key].([]interface{}); ok {
return len(v)
}
return 0
}
// ── Main ─────────────────────────────────────────────────
func main() {
if apiKey == "" {
fmt.Fprintln(os.Stderr, "Error: Set DOCSIE_API_KEY environment variable")
os.Exit(1)
}
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "Usage: go run video_to_docs.go <video_url> [doc_style]")
os.Exit(1)
}
videoURL := os.Args[1]
docStyle := "guide"
if len(os.Args) > 2 {
docStyle = os.Args[2]
}
// 1. Submit
fmt.Printf("==> Submitting video job (style=%s)...\n", docStyle)
submitResp, err := apiPost("/video-to-docs/submit/", map[string]interface{}{
"video_url": videoURL,
"quality": "draft",
"language": "english",
"doc_style": docStyle,
"auto_generate": false,
})
check(err)
jobID := str(submitResp, "job_id")
fmt.Printf(" Job ID: %s\n", jobID)
// 2. Poll analysis
fmt.Println("==> Polling analysis...")
_, err = poll("/video-to-docs/"+jobID+"/status/", 900, 15)
check(err)
// 3. Get result
fmt.Println("==> Fetching result...")
result, err := apiGet("/video-to-docs/" + jobID + "/result/")
check(err)
markdown := str(result, "markdown")
transcription := str(result, "transcription")
fmt.Printf(" Markdown: %d chars\n", len(markdown))
fmt.Printf(" Transcription: %d chars\n", len(transcription))
fmt.Printf(" Sections: %d\n", arrLen(result, "sections"))
fmt.Printf(" Images: %d\n", arrLen(result, "images"))
os.WriteFile("analysis_result.md", []byte(markdown), 0644)
os.WriteFile("transcription.txt", []byte(transcription), 0644)
fmt.Println(" Saved: analysis_result.md, transcription.txt")
// 4. Generate
fmt.Println("==> Triggering AI rewrite...")
genResp, err := apiPost("/video-to-docs/"+jobID+"/generate/", map[string]interface{}{
"doc_style": docStyle,
"output_formats": []string{"md", "docx", "pdf"},
})
check(err)
genJobID := str(genResp, "generate_job_id")
fmt.Printf(" Generate job: %s\n", genJobID)
fmt.Println("==> Polling generate job...")
genResult, err := poll("/jobs/"+genJobID+"/", 300, 10)
check(err)
gen := objMap(genResult, "result")
fmt.Printf(" Title: %s\n", str(gen, "title"))
fmt.Printf(" Words: %s -> %s\n", str(gen, "input_word_count"), str(gen, "output_word_count"))
os.WriteFile("generated.md", []byte(str(gen, "markdown")), 0644)
fmt.Println(" Saved: generated.md")
// 5. Download exports
exports := objMap(gen, "exports")
for _, fmt_ := range []string{"docx", "pdf"} {
info := objMap(exports, fmt_)
exportJobID := str(info, "job_id")
if exportJobID == "" {
continue
}
fmt.Printf("==> Waiting for %s export...\n", fmt_)
exportResult, err := poll("/jobs/"+exportJobID+"/", 120, 5)
check(err)
exportData := objMap(exportResult, "result")
url := str(exportData, "url")
filename := str(exportData, "filename")
if filename == "" {
filename = "output." + fmt_
}
if url != "" {
downloadFile(url, filename)
}
}
fmt.Println("\n==> Complete!")
}
func check(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}