-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple-tv-content-builder.go
130 lines (109 loc) · 3.44 KB
/
apple-tv-content-builder.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
package main
import (
"bytes"
"encoding/json"
"html/template"
"io/ioutil"
"net/http"
)
// Response models the response we get when asking for /me/videos
type Response struct {
Total int `json:"total"`
Page int `json:"page"`
VideoRecords []VideoRecord `json:"data"`
}
// CountOfVideoRecords returns the number of video records in the response
func (response Response) CountOfVideoRecords() int {
return len(response.VideoRecords)
}
// VideoRecord models the videos of our video collection in Response
type VideoRecord struct {
Name string `json:"name"`
Description string `json:"description"`
URI string `json:"uri"`
Pictures PictureCatalog `json:"pictures"`
VideoFiles []VideoFile `json:"files"`
}
// PictureCatalog models the pictures for a video record
type PictureCatalog struct {
URI string `json:"uri"`
Sizes []PictureSize `json:"sizes"`
}
// ThumbnailSize return our prefered PictureSize when needing a thumbnail
func (pc PictureCatalog) ThumbnailSize() PictureSize {
for _, element := range pc.Sizes {
if element.Height == 360 {
return element
}
}
return pc.Sizes[0]
}
// PictureSize models the multiple size offerings for a picture
type PictureSize struct {
Width int `json:"width"`
Height int `json:"height"`
Link string `json:"link"`
}
// HDFile returns the file of files we pref when needing to link to the HD version of this video
func (vr VideoRecord) HDFile() VideoFile {
for _, element := range vr.VideoFiles {
if element.Height == 1080 {
return element
}
}
return vr.VideoFiles[0]
}
// VideoFile models the perminent file urls for the video
type VideoFile struct {
Width int `json:"width"`
Height int `json:"height"`
Link string `json:"link"`
}
// Basic error checking function: check for error, if present panic
func checkError(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Build a http request with auth headers
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.vimeo.com/me/videos", nil)
req.Header.Set("Authorization", "bearer 4a19abe051f1f17f6dc5b5204f2d461c")
res, err := client.Do(req)
checkError(err)
// parse the response body into a byte slice
defer res.Body.Close()
bodyBytes, err := ioutil.ReadAll(res.Body)
checkError(err)
// unmarshal the response bytes from JSON into a struct
var r Response
err = json.Unmarshal(bodyBytes, &r)
checkError(err)
// FIXME: Need to find a nicer way than hardcoding this path
PROJECTFOLDER := "/Users/zorn/go/src/github.com/phillycocoa/apple-tv-content-builder/"
// grab out template file
filepath := PROJECTFOLDER + "main-template.tmpl"
contents, err := ioutil.ReadFile(filepath)
checkError(err)
// build a new template object with the contents of our file
t := template.New("main template")
t, err = t.Parse(string(contents[:]))
checkError(err)
// process the template given the JSON struct
var templateOutput bytes.Buffer
err = t.Execute(&templateOutput, r)
checkError(err)
// take the result of the template processing and write it to a file
filepathOut := PROJECTFOLDER + "out/PCHTemplate.xml.js"
err = ioutil.WriteFile(filepathOut, templateOutput.Bytes(), 0644)
checkError(err)
}
// Previous code we used to build a JSON response
// func sampleJSONResponse() []byte {
// v1 := VideoRecord{"123"}
// v2 := VideoRecord{"456"}
// r := Response{11, 1, []videoRecord{v1, v2}}
// b, _ := json.Marshal(r)
// return b
// }