This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
/
udemy_types.go
161 lines (142 loc) · 4.24 KB
/
udemy_types.go
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
package client
import (
"encoding/json"
"fmt"
"time"
)
type User struct {
ID int `json:"id"`
Title string `json:"title"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
URL string `json:"url"`
}
type Courses struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []*Course `json:"results"`
}
type Course struct {
ID int `json:"id"`
Title string `json:"title"`
URL string `json:"url"`
PublishedTitle string `json:"published_title"`
}
type PriceDetail struct {
Amount float64 `json:"amount"`
Currency string `json:"currency"`
PriceString string `json:"price_string"`
CurrencySymbol string `json:"currency_symbol"`
}
type Lectures struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []*Lecture `json:"results"`
}
type Lecture struct {
Chapter *Chapter `json:"-"`
ID int `json:"id"`
Title string `json:"title"`
TitleCleaned string `json:"title_cleaned"`
Asset *Asset `json:"asset"`
SupplementaryAssets []*Asset `json:"supplementary_assets"`
ObjectIndex int `json:"object_index"`
}
type Chapter struct {
ID int `json:"id"`
Title string `json:"title"`
ObjectIndex int `json:"object_index"`
}
type Asset struct {
ID int `json:"id"`
AssetType string `json:"asset_type"`
Title string `json:"title"`
ExternalURL string `json:"external_url"`
DownloadUrls *DownloadURLs `json:"download_urls"`
//SlideUrls []interface{} `json:"slide_urls"`
StreamUrls *StreamURLs `json:"stream_urls"`
Captions []*Caption `json:"captions"`
}
type DownloadURLs struct {
Video []*Video `json:"Video"`
File []*File `json:"File"`
Ebook []*File `json:"E-Book"`
}
type StreamURLs struct {
Video []*Video `json:"Video"`
}
type Video struct {
Type string `json:"type"`
Label string `json:"label"`
File string `json:"file"`
}
type File struct {
Label string `json:"label"`
File string `json:"file"`
}
type Caption struct {
Status int `json:"status"`
Locale Locale `json:"locale"`
ID int `json:"id"`
Source string `json:"source"`
Title string `json:"title"`
VideoLabel string `json:"video_label"`
Created time.Time `json:"created"`
FileName string `json:"file_name"`
URL string `json:"url"`
}
type Locale struct {
Locale string `json:"locale"`
}
type Curriculum struct {
Count int `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results CurriculumItems `json:"results"`
}
// CurriculumItem contains either *Chapter or *Lecture items
type CurriculumItems []interface{}
func (c *CurriculumItems) UnmarshalJSON(data []byte) error {
// Load all the "possible" fields
var items []struct {
Class string `json:"_class"`
ID int `json:"id"`
Title string `json:"title"`
TitleCleaned string `json:"title_cleaned"`
Asset *Asset `json:"asset"`
SupplementaryAssets []*Asset `json:"supplementary_assets"`
ObjectIndex int `json:"object_index"`
}
if err := json.Unmarshal(data, &items); err != nil {
return err
}
var currentChapter *Chapter
for idx, i := range items {
if i.Class == "chapter" {
// ok it's a chapter
currentChapter = &Chapter{
ID: i.ID,
Title: i.Title,
ObjectIndex: i.ObjectIndex,
}
*c = append(*c, currentChapter)
} else if i.Class == "lecture" {
*c = append(*c, &Lecture{
ID: i.ID,
Title: i.Title,
ObjectIndex: i.ObjectIndex,
TitleCleaned: i.TitleCleaned,
Asset: i.Asset,
SupplementaryAssets: i.SupplementaryAssets,
Chapter: currentChapter,
})
} else if i.Class == "quiz" || i.Class == "practice" {
// ignore for now on
} else {
return fmt.Errorf("unknown type for curriculum item at position %d: want \"chapter\" or \"lecture\", got %q", idx, i.Class)
}
}
return nil
}