-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmovies.go
71 lines (60 loc) · 2.35 KB
/
movies.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
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
)
// Actually processses a file that's in the new folder.
func processMovie(folder string, file string, paths Paths, config Config) error {
log.Println("Processing", file)
// Parse the title.
fileTitle, year := titleAndYearFromFilename(file)
// Make the temporary output folder.
inPath := filepath.Join(folder, file)
stagingOutputFolder := filepath.Join(paths.Staging, fileTitle)
os.MkdirAll(stagingOutputFolder, os.ModePerm)
// Get the metadata.
tmdbMovie, tmdbErr := requestTmdbMovieSearch(fileTitle, year)
if tmdbErr != nil {
log.Println("Failed to find TMDB data for", fileTitle, "error:", tmdbErr)
failedPath := filepath.Join(paths.Failed, file) // Move to 'failed'.
os.Rename(inPath, failedPath)
os.RemoveAll(stagingOutputFolder) // Tidy up.
return tmdbErr
} else {
// Save the metadata.
metadata, _ := json.Marshal(tmdbMovie)
metadataPath := filepath.Join(stagingOutputFolder, metadataFilename)
ioutil.WriteFile(metadataPath, metadata, os.ModePerm)
}
// Get the image.
getMovieImageIfNeeded(tmdbMovie.PosterPath, "w780", stagingOutputFolder, imageFilename)
getMovieImageIfNeeded(tmdbMovie.BackdropPath, "w1280", stagingOutputFolder, imageBackdropFilename)
// Convert it.
convertErr := convertToHLSAppropriately(inPath, stagingOutputFolder, config)
// Fail! Move it to the failed folder.
if convertErr != nil {
switch err := convertErr.(type) {
case *convertRenamedError:
log.Println("Failed to convert", file, "; file renamed for user intervention, err:", err)
default:
log.Println("Failed to convert", file, "; moving to the Failed folder, err:", err)
failedPath := filepath.Join(paths.Failed, file) // Move it to 'failed'.
os.Rename(inPath, failedPath)
}
os.RemoveAll(stagingOutputFolder) // Tidy up.
return errors.New("Couldn't convert " + file)
}
// Success!
log.Println("Success! Removing original.")
goodTitle := sanitiseForFilesystem(tmdbMovie.Title) + " " + tmdbMovie.ReleaseDate[:4]
goodFolder := filepath.Join(paths.Movies, goodTitle)
os.Rename(stagingOutputFolder, goodFolder) // Move the HLS across.
os.Remove(inPath) // Remove the original file.
// Assumption is that the user made a backup of their original from their DVD so doesn't care to lose it.
generateMetadata(paths)
return nil
}