-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetadata.go
More file actions
374 lines (315 loc) · 8.71 KB
/
metadata.go
File metadata and controls
374 lines (315 loc) · 8.71 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
)
type Metadata struct {
TVShows []TVShowMetadata
Movies []MovieMetadata
Capacity string
}
type MovieMetadata struct {
TMDBId int
Name string
Overview string
Image string
Backdrop string
ReleaseDate string
Vote float32
Media string
}
type TVShowMetadata struct {
TVDBId string
Name string
Overview string
Image string
Backdrop string
FirstAirDate string
Seasons []TVSeasonMetadata
}
type TVSeasonMetadata struct {
TVDBId int
Season int
Name string
Image string
Episodes []TVEpisodeMetadata
}
type TVEpisodeMetadata struct {
TVDBId int
Episode int
Name string
Overview string
Image string
Media string
AirDate string
}
// Generates metadata for everything.
func generateMetadata(paths Paths) {
log.Println("Generating metadata")
shows := make([]TVShowMetadata, 0)
for _, showFolder := range directoriesIn(paths.TV) {
// Load the metadata.
var showDetails *TVDBSeries
if err := readAndUnmarshal(showFolder, metadataFilename, &showDetails); err != nil {
continue
}
// Create the model.
image, _ := filepath.Rel(paths.Root, filepath.Join(showFolder, imageFilename))
backdrop, _ := filepath.Rel(paths.Root, filepath.Join(showFolder, imageBackdropFilename))
show := TVShowMetadata{
TVDBId: showDetails.TVDBID,
Name: showDetails.Name,
Overview: showDetails.Overview,
Image: image,
Backdrop: backdrop,
FirstAirDate: showDetails.FirstAirDate,
}
// Find the seasons.
for _, seasonFolder := range directoriesIn(showFolder) {
var seasonDetails *TVDBSeason
if err := readAndUnmarshal(seasonFolder, metadataFilename, &seasonDetails); err != nil {
continue
}
image, _ := filepath.Rel(paths.Root, filepath.Join(seasonFolder, imageFilename))
season := TVSeasonMetadata{
TVDBId: seasonDetails.TVDBID,
Season: seasonDetails.Season,
Name: seasonDetails.Name,
Image: image,
}
// Find the episodes.
for _, epFolder := range directoriesIn(seasonFolder) {
var epDetails *TVDBEpisode
if err := readAndUnmarshal(epFolder, metadataFilename, &epDetails); err != nil {
continue
}
image, _ := filepath.Rel(paths.Root, filepath.Join(epFolder, imageFilename))
media, _ := filepath.Rel(paths.Root, filepath.Join(epFolder, hlsFilename))
episode := TVEpisodeMetadata{
TVDBId: epDetails.TVDBID,
Episode: epDetails.Episode,
Name: epDetails.Name,
Overview: epDetails.Overview,
Image: image,
Media: media,
AirDate: epDetails.AirDate,
}
season.Episodes = append(season.Episodes, episode)
}
sort.Sort(ByEpisode(season.Episodes))
show.Seasons = append(show.Seasons, season)
generateSeasonHTML(seasonFolder, season, paths)
}
sort.Sort(BySeason(show.Seasons))
shows = append(shows, show)
generateShowHTML(showFolder, show, paths)
}
// Find the movies.
movies := make([]MovieMetadata, 0)
for _, folder := range directoriesIn(paths.Movies) {
// Load the metadata.
var details *TmdbMovieSearchResult
if err := readAndUnmarshal(folder, metadataFilename, &details); err != nil {
continue
}
// Create the model.
image, _ := filepath.Rel(paths.Root, filepath.Join(folder, imageFilename))
backdrop, _ := filepath.Rel(paths.Root, filepath.Join(folder, imageBackdropFilename))
media, _ := filepath.Rel(paths.Root, filepath.Join(folder, hlsFilename))
movie := MovieMetadata{
TMDBId: details.Id,
Name: details.Title,
Overview: details.Overview,
Image: image,
Backdrop: backdrop,
ReleaseDate: details.ReleaseDate,
Vote: details.VoteAverage,
Media: media,
}
movies = append(movies, movie)
}
// Make the root metadata.
capacity := capacity(paths)
metadata := Metadata{
TVShows: shows,
Movies: movies,
Capacity: capacity,
}
// Save.
data, _ := json.MarshalIndent(metadata, "", " ")
ioutil.WriteFile(filepath.Join(paths.Root, metadataFilename), data, os.ModePerm)
generateRootHTML(capacity, paths)
}
// List of full directories (eg path + name).
func directoriesIn(path string) []string {
directories := make([]string, 0)
infos, _ := ioutil.ReadDir(path)
for _, info := range infos {
if info.IsDir() {
directory := filepath.Join(path, info.Name())
directories = append(directories, directory)
}
}
return directories
}
func readAndUnmarshal(folder string, file string, v interface{}) error {
path := filepath.Join(folder, file)
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
// Sorting.
type ByEpisode []TVEpisodeMetadata
func (a ByEpisode) Len() int { return len(a) }
func (a ByEpisode) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByEpisode) Less(i, j int) bool {
return a[i].Episode < a[j].Episode
}
type BySeason []TVSeasonMetadata
func (a BySeason) Len() int { return len(a) }
func (a BySeason) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BySeason) Less(i, j int) bool {
return a[i].Season < a[j].Season
}
/// Generate a season's html (which is the list of episodes in it).
func generateSeasonHTML(seasonPath string, season TVSeasonMetadata, paths Paths) {
html := htmlStart
isLeft := true
trOpen := false
for _, episode := range season.Episodes {
if isLeft {
html += "<tr>"
trOpen = true
}
linkPath, _ := filepath.Rel(seasonPath, filepath.Join(paths.Root, episode.Media))
imagePath, _ := filepath.Rel(seasonPath, filepath.Join(paths.Root, episode.Image))
name := fmt.Sprintf("S%02d E%02d %s", season.Season, episode.Episode, episode.Name)
h := htmlTd
h = strings.Replace(h, "LINK", linkPath, -1)
h = strings.Replace(h, "IMAGE", imagePath, -1)
h = strings.Replace(h, "NAME", name, -1)
html += h
if !isLeft {
html += "</tr>"
trOpen = false
}
isLeft = !isLeft
}
if trOpen {
html += "</tr>"
}
html += htmlEnd
// Save.
outPath := filepath.Join(seasonPath, indexHtml)
ioutil.WriteFile(outPath, []byte(html), os.ModePerm)
}
/// Generates the root index.html
func generateRootHTML(capacity string, paths Paths) {
html := htmlStart
isLeft := true
trOpen := false
// Movies.
movieFiles, _ := ioutil.ReadDir(paths.Movies)
for _, fileInfo := range movieFiles {
if fileInfo.IsDir() {
if isLeft {
html += "<tr>"
trOpen = true
}
linkPath := paths.MoviesRelativeToRoot + "/" + fileInfo.Name() + "/" + hlsFilename
imagePath := paths.MoviesRelativeToRoot + "/" + fileInfo.Name() + "/" + imageFilename
h := htmlTd
h = strings.Replace(h, "LINK", linkPath, -1)
h = strings.Replace(h, "IMAGE", imagePath, -1)
h = strings.Replace(h, "NAME", fileInfo.Name(), -1)
html += h
if !isLeft {
html += "</tr>"
trOpen = false
}
isLeft = !isLeft
}
}
// TV Shows.
tvFiles, _ := ioutil.ReadDir(paths.TV)
for _, fileInfo := range tvFiles {
if fileInfo.IsDir() {
if isLeft {
html += "<tr>"
trOpen = true
}
linkPath := paths.TVRelativeToRoot + "/" + fileInfo.Name()
imagePath := paths.TVRelativeToRoot + "/" + fileInfo.Name() + "/" + imageFilename
h := htmlTd
h = strings.Replace(h, "LINK", linkPath, -1)
h = strings.Replace(h, "IMAGE", imagePath, -1)
h = strings.Replace(h, "NAME", fileInfo.Name(), -1)
html += h
if !isLeft {
html += "</tr>"
trOpen = false
}
isLeft = !isLeft
}
}
if trOpen {
html += "</tr>"
}
// Add the html trailer.
end := strings.Replace(htmlEnd, "CAPACITY", capacity, -1)
html += end
// Save.
outPath := filepath.Join(paths.Root, indexHtml)
ioutil.WriteFile(outPath, []byte(html), os.ModePerm)
}
/// Generate a show's html (which is the list of seasons in it).
func generateShowHTML(showPath string, show TVShowMetadata, paths Paths) {
html := htmlStart
isLeft := true
trOpen := false
for _, season := range show.Seasons {
if isLeft {
html += "<tr>"
trOpen = true
}
linkPath := tvSeasonFolderNameFor(season.Season)
imagePath := linkPath + "/" + imageFilename
name := season.Name
h := htmlTd
h = strings.Replace(h, "LINK", linkPath, -1)
h = strings.Replace(h, "IMAGE", imagePath, -1)
h = strings.Replace(h, "NAME", name, -1)
html += h
if !isLeft {
html += "</tr>"
trOpen = false
}
isLeft = !isLeft
}
if trOpen {
html += "</tr>"
}
html += htmlEnd
// Save.
outPath := filepath.Join(showPath, indexHtml)
ioutil.WriteFile(outPath, []byte(html), os.ModePerm)
}
/// Figure out how much disk space is left.
func capacity(paths Paths) string {
cmd := exec.Command("df", "-h", paths.Root)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Run()
// TODO split and transpose
return strings.TrimSpace(out.String())
}