-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththinkific.go
More file actions
1171 lines (1032 loc) · 33.8 KB
/
thinkific.go
File metadata and controls
1171 lines (1032 loc) · 33.8 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"encoding/json"
"fmt"
"html"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/cdproto/storage"
"github.com/chromedp/cdproto/target"
"github.com/chromedp/chromedp"
)
// ── Types ──────────────────────────────────────────────────────────────────
// ThinkificState holds session cookies, detected base URL, and cached course list.
type ThinkificState struct {
cookies string
baseURL string // e.g. "https://mysite.thinkific.com"
courses []ThinkificCourse
cachedSlugs []string // slugs harvested from the live browser during login
}
// ThinkificCourse represents a single enrolled course.
type ThinkificCourse struct {
Slug string
Name string
ImageURL string
FolderName string
}
// Pre-compiled regexes used inside thinkific functions.
var (
thinkificBaseURLRe = regexp.MustCompile(`(https?://[^/]+).*`) // strips path from URL
thinkificPathRe = regexp.MustCompile(`https?://[^/]+(.*)`) // captures path portion
thinkificOGImageRe1 = regexp.MustCompile(`(?i)property=["']og:image["']\s+content=["']([^"']+)["']`)
thinkificOGImageRe2 = regexp.MustCompile(`(?i)content=["']([^"']+)["']\s+property=["']og:image["']`)
thinkificOGImageRe3 = regexp.MustCompile(`(?i)<meta[^>]+og:image[^>]+content=["']([^"']+)["']`)
thinkificCoverOGRe = regexp.MustCompile(`property=["']og:image["']\s+content=["']([^"']+)["']`)
thinkificPlayerEmbRe = regexp.MustCompile(`player\.thinkific\.com/embed/([a-f0-9-]+)`)
thinkificMp4Re = regexp.MustCompile(`https?://[^\s"']+\.mp4`)
)
// ── Login via Browser (chromedp) ────────────────────────────────────────────
// thinkificLogin opens Chrome via chromedp, navigates to the Thinkific login page,
// waits for the user to authenticate, then captures cookies automatically —
// exactly mirroring the Python Selenium browser_login() flow.
//
// The "email" field receives the site URL. You can pass:
// - Just the domain: "mysite.thinkific.com" → opens /users/sign_in
// - Full login URL: "mysite.thinkific.com/users/sign_in" (or any custom login path)
// - With scheme: "https://mysite.thinkific.com/my/login"
//
// The "password" field is ignored (login happens interactively in the browser).
func (a *App) thinkificLogin(siteURL, _ string) map[string]interface{} {
rawURL := strings.TrimSpace(siteURL)
if rawURL == "" {
return map[string]interface{}{
"success": false,
"error": "Enter your Thinkific site URL (e.g. mysite.thinkific.com).",
}
}
// Extract the base URL (scheme + host only) for API calls later.
baseURL := thinkificNormaliseURL(rawURL)
// Determine the sign-in URL.
// If the user supplied a path (anything after the host), use the full URL as-is.
// Otherwise fall back to the default Thinkific login path.
signInURL := thinkificExtractSignInURL(rawURL, baseURL)
// ── Build Chrome allocator ─────────────────────────────────────────────
// Use a dedicated, isolated profile for the downloader so we never
// conflict with the user's existing Chrome session (which causes a second
// window to open with all their restored tabs).
headlessOff := chromedp.Flag("headless", false)
// Try to find chrome.exe explicitly on Windows
var chromeBin chromedp.ExecAllocatorOption
for _, candidate := range []string{
filepath.Join(os.Getenv("PROGRAMFILES"), "Google", "Chrome", "Application", "chrome.exe"),
filepath.Join(os.Getenv("PROGRAMFILES(X86)"), "Google", "Chrome", "Application", "chrome.exe"),
filepath.Join(os.Getenv("LOCALAPPDATA"), "Google", "Chrome", "Application", "chrome.exe"),
} {
if _, err := os.Stat(candidate); err == nil {
chromeBin = chromedp.ExecPath(candidate)
break
}
}
// Dedicated profile dir — persistent so cookies survive between runs,
// but fully separate from the user's real Chrome profile.
profileDir := filepath.Join(os.Getenv("LOCALAPPDATA"), "vintecinco_dl", "chrome-profile")
_ = os.MkdirAll(profileDir, 0755)
opts := []chromedp.ExecAllocatorOption{
headlessOff,
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.UserDataDir(profileDir),
chromedp.Flag("disable-blink-features", "AutomationControlled"),
chromedp.Flag("exclude-switches", "enable-automation"),
chromedp.Flag("no-default-browser-check", true),
chromedp.Flag("restore-last-session", false),
chromedp.Flag("disable-session-crashed-bubble", true),
chromedp.WindowSize(1100, 800),
}
if chromeBin != nil {
opts = append(opts, chromeBin)
}
allocCtx, cancelAlloc := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancelCtx := chromedp.NewContext(allocCtx)
if err := chromedp.Run(ctx, chromedp.Navigate(signInURL)); err != nil {
cancelCtx()
cancelAlloc()
return map[string]interface{}{
"success": false,
"error": "Could not open Chrome. Make sure it is installed: " + err.Error(),
}
}
// Close any extra tabs Chrome may have opened (e.g. an initial about:blank).
_ = chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
tgts, err := chromedp.Targets(ctx)
if err != nil {
return nil // non-fatal
}
current := chromedp.FromContext(ctx)
if current == nil || current.Target == nil {
return nil
}
currentID := current.Target.TargetID
for _, t := range tgts {
if t.Type == "page" && t.TargetID != currentID {
_ = target.CloseTarget(t.TargetID).Do(ctx)
}
}
return nil
}))
defer func() {
cancelCtx()
cancelAlloc()
}()
// ── Poll until authenticated ───────────────────────────────────────────
// Auth is confirmed when the session/remember cookies exist AND
// the user has navigated away from the sign_in page.
authCookieNames := map[string]bool{
"remember_user_token": true,
"_thinkific_session": true,
}
const timeout = 5 * time.Minute
const pollInterval = 1500 * time.Millisecond
deadline := time.Now().Add(timeout)
var capturedCookies []*network.Cookie
loggedIn := false
for time.Now().Before(deadline) {
time.Sleep(pollInterval)
var currentURL string
var cookies []*network.Cookie
err := chromedp.Run(ctx,
chromedp.Location(¤tURL),
chromedp.ActionFunc(func(ctx context.Context) error {
var e error
cookies, e = storage.GetCookies().Do(ctx)
return e
}),
)
if err != nil {
// Window was closed — treat as cancellation.
break
}
// Must have left the sign-in page.
leftSignIn := !strings.Contains(currentURL, "sign_in") &&
!strings.Contains(currentURL, "sign_up")
// Must have at least one auth cookie.
hasAuthCookie := false
for _, c := range cookies {
if authCookieNames[c.Name] {
hasAuthCookie = true
break
}
}
if leftSignIn && hasAuthCookie {
loggedIn = true
capturedCookies = cookies
break
}
}
if !loggedIn {
return map[string]interface{}{
"success": false,
"error": "Login timeout or browser closed. Please try again.",
}
}
// Let session cookies fully stabilise.
time.Sleep(1 * time.Second)
// ── Navigate to /enrollments to merge any new cookies ─────────────────
_ = chromedp.Run(ctx,
chromedp.Navigate(baseURL+"/enrollments"),
chromedp.Sleep(3*time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
newCookies, e := storage.GetCookies().Do(ctx)
if e != nil {
return nil // non-fatal
}
existing := map[string]int{}
for i, c := range capturedCookies {
existing[c.Name] = i
}
for _, c := range newCookies {
if idx, ok := existing[c.Name]; ok {
capturedCookies[idx].Value = c.Value
} else {
capturedCookies = append(capturedCookies, c)
}
}
return nil
}),
)
// ── Build cookie header string ─────────────────────────────────────────
var parts []string
for _, c := range capturedCookies {
if c.Value != "" {
parts = append(parts, c.Name+"="+c.Value)
}
}
cookieStr := strings.Join(parts, "; ")
if cookieStr == "" {
return map[string]interface{}{
"success": false,
"error": "No cookies captured. Please try again.",
}
}
a.thinkific.cookies = cookieStr
a.thinkific.baseURL = baseURL
// ── Harvest course slugs from the live browser (SPA rendered) ──────────
// This is crucial for React/SPA Thinkific sites where plain HTTP requests
// only get the empty pre-render HTML shell.
var renderedHTML string
_ = chromedp.Run(ctx,
chromedp.Navigate(baseURL+"/enrollments"),
chromedp.Sleep(4*time.Second), // wait for SPA hydration
chromedp.OuterHTML("html", &renderedHTML, chromedp.ByQuery),
)
// Harvest slugs from the rendered HTML
a.thinkific.cachedSlugs = thinkificHarvestSlugsFromHTML(renderedHTML)
return map[string]interface{}{"success": true}
}
// thinkificNormaliseURL ensures a scheme is present and strips any path,
// returning only the base URL (scheme + host), e.g. "https://mysite.thinkific.com".
func thinkificNormaliseURL(raw string) string {
raw = strings.TrimSpace(raw)
if !strings.HasPrefix(raw, "http://") && !strings.HasPrefix(raw, "https://") {
raw = "https://" + raw
}
// Strip everything from the first path segment onward.
raw = thinkificBaseURLRe.ReplaceAllString(raw, "$1")
raw = strings.TrimRight(raw, "/")
return raw
}
// thinkificExtractSignInURL decides which URL to open in Chrome for login.
// If the user supplied a custom path (e.g. mysite.com/my/login), that full URL
// is used directly. If only a bare domain was given, the default
// "<baseURL>/users/sign_in" is returned.
func thinkificExtractSignInURL(rawURL, baseURL string) string {
// Ensure the raw URL has a scheme so we can parse it cleanly.
withScheme := rawURL
if !strings.HasPrefix(withScheme, "http://") && !strings.HasPrefix(withScheme, "https://") {
withScheme = "https://" + withScheme
}
// Extract everything after the host (the path portion).
pathPart := thinkificPathRe.ReplaceAllString(withScheme, "$1")
pathPart = strings.TrimRight(pathPart, "/")
// If the user gave us a real path, honour it.
if pathPart != "" && pathPart != "/" {
return baseURL + pathPart
}
// Default Thinkific login page.
return baseURL + "/users/sign_in"
}
// ── HTTP Headers ───────────────────────────────────────────────────────────
func thinkificHeaders(cookies string) map[string]string {
return map[string]string{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"Accept": "application/json",
"Cookie": cookies,
}
}
// ── Course Listing ─────────────────────────────────────────────────────────
// thinkificHarvestSlugsFromHTML extracts all course slugs from rendered HTML.
// Uses a broad regex that catches all Thinkific URL patterns, then filters out
// purely-numeric tokens (which are lesson/content IDs, not course slugs).
func thinkificHarvestSlugsFromHTML(pageHTML string) []string {
re := regexp.MustCompile(`(?i)/courses/(?:take/|enrolled/|enroll/)?([a-zA-Z][a-zA-Z0-9_-]{2,})\b`)
skipSet := map[string]bool{
"take": true, "new": true, "edit": true, "enroll": true,
"preview": true, "sign_in": true, "sign_up": true, "enrolled": true,
"progress": true, "admin": true, "users": true, "bundle": true,
"checkout": true, "cart": true, "payment": true,
}
numericRe := regexp.MustCompile(`^\d+$`)
seen := map[string]bool{}
var slugs []string
for _, m := range re.FindAllStringSubmatch(pageHTML, -1) {
slug := m[1]
lower := strings.ToLower(slug)
// Skip purely numeric strings (lesson IDs, not slugs)
if numericRe.MatchString(slug) {
continue
}
if seen[lower] || skipSet[lower] {
continue
}
seen[lower] = true
slugs = append(slugs, slug)
}
return slugs
}
// thinkificSlugsToCourses resolves a list of slugs to full ThinkificCourse records
// by querying the course-player API for each slug.
// Slugs that return a non-200 or empty response are skipped (not real enrolled courses).
func (a *App) thinkificSlugsToCourses(slugs []string, hdrs map[string]string) []ThinkificCourse {
seen := map[string]bool{}
var courses []ThinkificCourse
for _, slug := range slugs {
if seen[slug] {
continue
}
seen[slug] = true
// Validate: only include slugs that actually resolve via the API
detail, s, e := httpGetJSON(
a.thinkific.baseURL+"/api/course_player/v2/courses/"+slug, hdrs,
)
if e != nil || s != 200 {
continue // not a real enrolled course on this account
}
name := slug
imageURL := ""
var dm map[string]interface{}
if json.Unmarshal(detail, &dm) == nil {
if co, ok := dm["course"].(map[string]interface{}); ok {
if n := jsonStr(co, "name"); n != "" {
name = n
}
imageURL = thinkificExtractImage(co)
} else {
if n := jsonStr(dm, "name"); n != "" {
name = n
}
imageURL = jsonStr(dm, "logo")
}
}
courses = append(courses, ThinkificCourse{
Slug: slug,
Name: name,
ImageURL: imageURL,
FolderName: sanitizeFilename(name),
})
}
return courses
}
// thinkificExtractImage tries every known image field from a Thinkific API object.
// Different site versions / plans expose different field names.
func thinkificExtractImage(obj map[string]interface{}) string {
for _, field := range []string{
"logo",
"course_card_image_url",
"card_image_url",
"banner_image_url",
"image_url",
"thumbnail_url",
"cover_image_url",
"image",
} {
if v := jsonStr(obj, field); v != "" {
return v
}
}
return ""
}
// thinkificScrapeOGImage fetches the HTML of a URL and returns the og:image content,
// or "" if none is found.
func thinkificScrapeOGImage(pageURL string, hdrs map[string]string) string {
pageData, s, e := httpGetJSON(pageURL, hdrs)
if e != nil || s != 200 {
return ""
}
// Try both attribute orderings of <meta property="og:image" content="...">
for _, re := range []*regexp.Regexp{
thinkificOGImageRe1,
thinkificOGImageRe2,
thinkificOGImageRe3,
} {
if m := re.FindSubmatch(pageData); len(m) > 1 && string(m[1]) != "" {
return string(m[1])
}
}
return ""
}
func (a *App) thinkificGetCourses() []map[string]interface{} {
hdrs := thinkificHeaders(a.thinkific.cookies)
hdrs["Referer"] = a.thinkific.baseURL + "/"
var courses []ThinkificCourse
// Strategy 1: use slugs captured from the live browser during login
// (most reliable — browser renders the SPA, HTTP requests only get shell HTML)
if len(a.thinkific.cachedSlugs) > 0 {
courses = a.thinkificSlugsToCourses(a.thinkific.cachedSlugs, hdrs)
}
// Strategy 2: enrollments JSON API
if len(courses) == 0 {
data, status, err := httpGetJSON(
a.thinkific.baseURL+"/api/course_player/v2/enrollments", hdrs,
)
if err == nil && status == 200 {
courses = a.thinkificParseEnrollments(data, hdrs)
}
}
// Strategy 3: additional API variants
if len(courses) == 0 {
for _, endpoint := range []string{
"/api/course_player/v2/courses",
"/api/v0/enrollments",
"/api/v0/courses",
} {
data, status, err := httpGetJSON(a.thinkific.baseURL+endpoint, hdrs)
if err == nil && status == 200 {
courses = a.thinkificParseEnrollments(data, hdrs)
if len(courses) > 0 {
break
}
}
}
}
// Strategy 4: scrape /enrollments and /dashboard HTML via HTTP
// (works for server-rendered Thinkific sites, not SPAs)
if len(courses) == 0 {
courses = a.thinkificScrapeEnrollments(hdrs)
}
a.thinkific.courses = courses
result := make([]map[string]interface{}, len(courses))
for i, c := range courses {
dlStatus := courseDownloadStatus(filepath.Join(a.platformDir(), c.FolderName))
result[i] = map[string]interface{}{
"name": c.Name,
"preview_url": c.ImageURL,
"downloaded": dlStatus != "none",
"dl_status": dlStatus,
}
}
return result
}
func (a *App) thinkificParseEnrollments(data []byte, hdrs map[string]string) []ThinkificCourse {
var raw interface{}
if json.Unmarshal(data, &raw) != nil {
return nil
}
var items []interface{}
switch v := raw.(type) {
case []interface{}:
items = v
case map[string]interface{}:
for _, key := range []string{"items", "enrollments", "courses"} {
if arr, ok := v[key].([]interface{}); ok {
items = arr
break
}
}
}
var courses []ThinkificCourse
seen := map[string]bool{}
for _, item := range items {
im, ok := item.(map[string]interface{})
if !ok {
continue
}
slug := jsonStr(im, "slug")
if slug == "" {
slug = jsonStr(im, "course_slug")
}
if slug == "" || seen[slug] {
continue
}
seen[slug] = true
name := jsonStr(im, "name")
if name == "" {
name = jsonStr(im, "course_name")
}
if name == "" {
name = slug
}
imageURL := thinkificExtractImage(im)
// Enrich from course detail API if name is still just the slug or imageURL is empty
if name == slug || imageURL == "" {
if detail, s, e := httpGetJSON(a.thinkific.baseURL+"/api/course_player/v2/courses/"+slug, hdrs); e == nil && s == 200 {
var dm map[string]interface{}
if json.Unmarshal(detail, &dm) == nil {
if co, ok := dm["course"].(map[string]interface{}); ok {
if n := jsonStr(co, "name"); n != "" {
name = n
}
if imageURL == "" {
imageURL = thinkificExtractImage(co)
}
} else {
if n := jsonStr(dm, "name"); name == slug && n != "" {
name = n
}
if imageURL == "" {
imageURL = thinkificExtractImage(dm)
}
}
}
}
}
// Final fallback: scrape og:image from the course HTML page
if imageURL == "" {
imageURL = thinkificScrapeOGImage(a.thinkific.baseURL+"/courses/"+slug, hdrs)
}
courses = append(courses, ThinkificCourse{
Slug: slug,
Name: name,
ImageURL: imageURL,
FolderName: sanitizeFilename(name),
})
}
return courses
}
func (a *App) thinkificScrapeEnrollments(hdrs map[string]string) []ThinkificCourse {
pageBody, status, err := httpGetJSON(a.thinkific.baseURL+"/enrollments", hdrs)
if err != nil || status != 200 {
return nil
}
pageHTML := string(pageBody)
slugRe := regexp.MustCompile(`href=["']/?courses/(?:take|enrolled|enroll)?/?([^"'/?#]+)["']`)
skipSet := map[string]bool{
"take": true, "new": true, "edit": true, "enroll": true,
"preview": true, "admin": true, "sign_in": true, "sign_up": true,
"users": true, "enrolled": true, "progress": true,
}
var courses []ThinkificCourse
seen := map[string]bool{}
for _, m := range slugRe.FindAllStringSubmatch(pageHTML, -1) {
slug := m[1]
if slug == "" || seen[slug] || skipSet[strings.ToLower(slug)] {
continue
}
seen[slug] = true
name := slug
imageURL := ""
if detail, s, e := httpGetJSON(a.thinkific.baseURL+"/api/course_player/v2/courses/"+slug, hdrs); e == nil && s == 200 {
var dm map[string]interface{}
if json.Unmarshal(detail, &dm) == nil {
if co, ok := dm["course"].(map[string]interface{}); ok {
if n := jsonStr(co, "name"); n != "" {
name = n
}
imageURL = thinkificExtractImage(co)
} else {
if n := jsonStr(dm, "name"); n != "" {
name = n
}
imageURL = thinkificExtractImage(dm)
}
}
}
// Final fallback: scrape og:image from the course HTML page
if imageURL == "" {
imageURL = thinkificScrapeOGImage(a.thinkific.baseURL+"/courses/"+slug, hdrs)
}
courses = append(courses, ThinkificCourse{
Slug: slug,
Name: name,
ImageURL: imageURL,
FolderName: sanitizeFilename(name),
})
}
return courses
}
// ── Download Orchestration ─────────────────────────────────────────────────
func (a *App) thinkificDownloadBatch(indices []int) {
a.emit("batch_started", map[string]interface{}{"count": len(indices)})
for qi, idx := range indices {
if a.cancel.Load() {
break
}
a.emit("batch_progress", map[string]interface{}{
"queue_pos": qi, "queue_total": len(indices),
})
if !a.thinkificDownloadOne(idx) {
if a.cancel.Load() {
break
}
}
}
a.emit("batch_done", map[string]interface{}{})
}
func (a *App) thinkificDownloadOne(index int) bool {
if index < 0 || index >= len(a.thinkific.courses) {
a.emit("dl_error", map[string]interface{}{"index": index, "message": "Invalid course index."})
return false
}
for attempt := 1; attempt <= 3; attempt++ {
ok, retry := a.thinkificDownloadOneAttempt(index)
if ok {
return true
}
if !retry || a.cancel.Load() {
return false
}
if attempt < 3 {
a.emit("dl_progress", map[string]interface{}{
"index": index, "current": 0, "total": 0,
"filename": fmt.Sprintf("Error, retrying... (%d/3)", attempt),
"percent": 0,
})
time.Sleep(time.Duration(3*attempt) * time.Second)
}
}
return false
}
func (a *App) thinkificDownloadOneAttempt(index int) (ok bool, retryable bool) {
defer func() {
if r := recover(); r != nil {
a.emit("dl_error", map[string]interface{}{
"index": index, "message": fmt.Sprintf("Error: %v", r),
})
ok = false
retryable = true
}
}()
course := a.thinkific.courses[index]
hdrs := thinkificHeaders(a.thinkific.cookies)
hdrs["Referer"] = a.thinkific.baseURL + "/"
courseDir := filepath.Join(a.platformDir(), course.FolderName)
os.MkdirAll(courseDir, 0755)
a.emit("dl_started", map[string]interface{}{"index": index, "folder": courseDir})
// Clean leftover .part files
filepath.Walk(courseDir, func(path string, info os.FileInfo, err error) error {
if err == nil && !info.IsDir() && filepath.Ext(path) == ".part" {
os.Remove(path)
}
return nil
})
// Fetch course data
data, status, err := httpGetJSON(a.thinkific.baseURL+"/api/course_player/v2/courses/"+course.Slug, hdrs)
if err != nil || status != 200 {
a.emit("dl_error", map[string]interface{}{
"index": index, "message": fmt.Sprintf("Failed to fetch course (HTTP %d)", status),
})
return false, true
}
var courseData map[string]interface{}
if json.Unmarshal(data, &courseData) != nil {
a.emit("dl_error", map[string]interface{}{"index": index, "message": "Failed to parse course data"})
return false, true
}
courseObj := jsonMap(courseData, "course")
contents := jsonArray(courseData, "contents")
chapters := jsonArray(courseData, "chapters")
// Build chapter-id → name map
chMap := map[float64]string{}
for _, ch := range chapters {
if cm, ok2 := ch.(map[string]interface{}); ok2 {
if id, ok3 := cm["id"].(float64); ok3 {
chMap[id] = jsonStr(cm, "name")
}
}
}
// Download cover image
coverURL := ""
if courseObj != nil {
coverURL = jsonStr(courseObj, "logo")
}
if coverURL == "" {
if pageData, s, e := httpGetJSON(a.thinkific.baseURL+"/courses/"+course.Slug, hdrs); e == nil && s == 200 {
if m := thinkificCoverOGRe.FindSubmatch(pageData); len(m) > 1 {
coverURL = string(m[1])
}
}
}
if coverURL != "" {
ext := "jpg"
if parts := strings.Split(strings.Split(coverURL, "?")[0], "."); len(parts) > 1 {
if e := parts[len(parts)-1]; len(e) <= 4 {
ext = e
}
}
coverPath := filepath.Join(courseDir, "cover."+ext)
if _, err2 := os.Stat(coverPath); os.IsNotExist(err2) {
downloadToFileSimple(coverURL, coverPath, hdrs)
}
}
// Track chapter index for folder numbering
chapterOrder := map[float64]int{}
for _, ch := range chapters {
if cm2, ok2 := ch.(map[string]interface{}); ok2 {
if id, ok3 := cm2["id"].(float64); ok3 {
// position field (1-based), fallback to insertion order
pos := int(id) // will be overwritten below
if p, ok4 := cm2["position"].(float64); ok4 {
pos = int(p)
}
chapterOrder[id] = pos
}
}
}
// If positions are all 0 or missing, assign sequential order
chSeq := map[float64]int{}
seqIdx := 1
for _, ch := range chapters {
if cm2, ok2 := ch.(map[string]interface{}); ok2 {
if id, ok3 := cm2["id"].(float64); ok3 {
chSeq[id] = seqIdx
seqIdx++
}
}
}
chPad := len(fmt.Sprintf("%d", len(chapters)))
if chPad < 2 {
chPad = 2
}
total := len(contents) + 1
current := 1
pad := len(fmt.Sprintf("%d", len(contents)))
if pad < 2 {
pad = 2
}
for i, rawContent := range contents {
if a.cancel.Load() {
a.emit("dl_cancelled", map[string]interface{}{"index": index})
return false, false
}
cm, ok2 := rawContent.(map[string]interface{})
if !ok2 {
continue
}
current++
lessonName := jsonStr(cm, "name")
if lessonName == "" {
lessonName = fmt.Sprintf("Lesson_%d", i+1)
}
chID, _ := cm["chapter_id"].(float64)
chName := chMap[chID]
if chName == "" {
chName = "Extras"
}
contentableID, _ := cm["contentable_id"].(float64)
lessonID := int(contentableID)
ctype := jsonStr(cm, "contentable_type")
safeName := fmt.Sprintf("%0*d. %s", pad, i+1, sanitizeFilename(lessonName))
// Number the chapter folder too: "01. Welcome!"
chIdx := chSeq[chID]
if chIdx == 0 {
chIdx = len(chSeq) + 1 // put unknowns at the end
}
var chFolderName string
if chName == "Extras" {
chFolderName = "Extras"
} else {
chFolderName = fmt.Sprintf("%0*d. %s", chPad, chIdx, sanitizeFilename(chName))
}
chDir := filepath.Join(courseDir, chFolderName)
os.MkdirAll(chDir, 0755)
a.emit("dl_progress", map[string]interface{}{
"index": index, "current": current, "total": total,
"filename": lessonName, "percent": 0,
})
if lessonID == 0 {
continue
}
switch ctype {
case "Download":
a.thinkificDownloadFiles(lessonID, safeName, chDir, hdrs, index, current, total, lessonName)
case "HtmlItem":
a.thinkificSaveHTMLItem(lessonID, safeName, chDir, hdrs)
default:
a.thinkificDownloadLesson(lessonID, safeName, chDir, hdrs, index, current, total, lessonName, course.Slug)
}
time.Sleep(300 * time.Millisecond)
}
markCourseComplete(courseDir)
a.emit("dl_complete", map[string]interface{}{"index": index, "folder": courseDir})
return true, false
}
// ── Lesson Downloaders ─────────────────────────────────────────────────────
func (a *App) thinkificDownloadLesson(
lessonID int, safeName, chDir string,
hdrs map[string]string,
index, current, total int,
lessonName, _ string,
) {
data, status, err := httpGetJSON(
fmt.Sprintf("%s/api/course_player/v2/lessons/%d", a.thinkific.baseURL, lessonID),
hdrs,
)
if err != nil || status != 200 {
a.emit("dl_progress", map[string]interface{}{
"index": index, "current": current, "total": total,
"filename": lessonName + " (fetch failed)", "percent": 0,
})
return
}
var lessonData map[string]interface{}
if json.Unmarshal(data, &lessonData) != nil {
return
}
lesson := jsonMap(lessonData, "lesson")
if lesson == nil {
lesson = lessonData
}
files := jsonArray(lessonData, "download_files")
// 1. Save HTML body
a.thinkificSaveHTML(jsonStr(lesson, "html_text"),
filepath.Join(chDir, safeName+".html"), lessonName)
// 2. Download video
videoURL := jsonStr(lesson, "video_url")
if videoURL == "" {
videoURL = jsonStr(lesson, "video_embed_url")
}
if videoURL != "" {
_ = a.thinkificDownloadVideo(videoURL,
filepath.Join(chDir, safeName+".mp4"),
index, current, total, lessonName)
}
// 3. File attachments
for _, f := range files {
fm, ok2 := f.(map[string]interface{})
if !ok2 {
continue
}
dlURL := jsonStr(fm, "download_url")
fname := jsonStr(fm, "file_name")
if fname == "" {
fname = jsonStr(fm, "label")
}
if fname == "" {
fname = "attachment"
}
if dlURL != "" {
dest := filepath.Join(chDir, sanitizeFilename(fname))
if _, err2 := os.Stat(dest); os.IsNotExist(err2) {
downloadToFileSimple(dlURL, dest, hdrs)
}
}
}
a.emit("dl_progress", map[string]interface{}{
"index": index, "current": current, "total": total,
"filename": lessonName, "percent": 100,
})
}
func (a *App) thinkificDownloadFiles(
lessonID int, _ string, chDir string,
hdrs map[string]string,
index, current, total int,
lessonName string,
) {
data, status, err := httpGetJSON(
fmt.Sprintf("%s/api/course_player/v2/downloads/%d", a.thinkific.baseURL, lessonID),
hdrs,
)
if err != nil || status != 200 {
data, status, err = httpGetJSON(
fmt.Sprintf("%s/api/course_player/v2/lessons/%d", a.thinkific.baseURL, lessonID),
hdrs,
)
if err != nil || status != 200 {
return
}
}
var resp map[string]interface{}
if json.Unmarshal(data, &resp) != nil {
return
}
for _, f := range jsonArray(resp, "download_files") {
fm, ok2 := f.(map[string]interface{})
if !ok2 {
continue
}
dlURL := jsonStr(fm, "download_url")
fname := jsonStr(fm, "file_name")
if fname == "" {
fname = jsonStr(fm, "label")
}
if fname == "" {
fname = "file"
}
if dlURL != "" {
dest := filepath.Join(chDir, sanitizeFilename(fname))
if _, err2 := os.Stat(dest); os.IsNotExist(err2) {