HUGE refactor. working towards web UI
This commit is contained in:
parent
dd54e8cc49
commit
1f94eecca9
29 changed files with 1669 additions and 162 deletions
|
|
@ -11,7 +11,8 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
|
||||
"arimelody.space/vodular/scanner"
|
||||
"arimelody.space/melody-vod-manager/scanner"
|
||||
"arimelody.space/melody-vod-manager/templates"
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/youtube/v3"
|
||||
|
|
@ -47,11 +48,6 @@ type (
|
|||
)
|
||||
|
||||
func BuildVideo(metadata *scanner.Metadata) (*Video, error) {
|
||||
videoDate, err := time.Parse("2006-01-02", metadata.Date)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse date from metadata: %v", err)
|
||||
}
|
||||
|
||||
var category *Category = nil
|
||||
if metadata.Category != nil {
|
||||
category = &Category{
|
||||
|
|
@ -68,13 +64,13 @@ func BuildVideo(metadata *scanner.Metadata) (*Video, error) {
|
|||
Title: metadata.Title,
|
||||
Category: category,
|
||||
Part: metadata.Part,
|
||||
Date: videoDate,
|
||||
Date: metadata.Date.AsTime(time.UTC),
|
||||
Tags: metadata.Tags,
|
||||
Filename: path.Join(
|
||||
metadata.FootageDir,
|
||||
fmt.Sprintf(
|
||||
"%s-fullvod.mkv",
|
||||
videoDate.Format("2006-01-02"),
|
||||
metadata.Date.String(),
|
||||
)),
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -92,12 +88,6 @@ type (
|
|||
Category *MetaCategory
|
||||
Part int
|
||||
}
|
||||
|
||||
Template struct {
|
||||
Title *template.Template
|
||||
Description *template.Template
|
||||
Tags []string
|
||||
}
|
||||
)
|
||||
|
||||
var videoCategoryTypeStrings = map[CategoryType]string{
|
||||
|
|
@ -113,88 +103,6 @@ var videoYtCategory = map[CategoryType]string {
|
|||
CATEGORY_ENTERTAINMENT: YT_CATEGORY_ENTERTAINMENT,
|
||||
}
|
||||
|
||||
const defaultTitleTemplate =
|
||||
"{{.Title}} - {{FormatTime .Date \"02 Jan 2006\"}}"
|
||||
const defaultDescriptionTemplate =
|
||||
"Streamed on {{FormatTime .Date \"02 January 2006\"}}"
|
||||
|
||||
var templateFuncs = template.FuncMap{
|
||||
"FormatTime": func (time time.Time, format string) string {
|
||||
return time.Format(format)
|
||||
},
|
||||
"ToLower": func (str string) string {
|
||||
return strings.ToLower(str)
|
||||
},
|
||||
"ToUpper": func (str string) string {
|
||||
return strings.ToUpper(str)
|
||||
},
|
||||
}
|
||||
|
||||
func FetchTemplates(templateDir string) (*Template, error) {
|
||||
tmpl := Template{}
|
||||
|
||||
var tagsPath = path.Join(templateDir, "tags.txt")
|
||||
var titlePath = path.Join(templateDir, "title.txt")
|
||||
var descriptionPath = path.Join(templateDir, "description.txt")
|
||||
|
||||
// tags
|
||||
if tagsFile, err := os.ReadFile(tagsPath); err == nil {
|
||||
tmpl.Tags = strings.Split(string(tagsFile), "\n")
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
log.Fatalf(
|
||||
"%s not found. No default tags will be used.",
|
||||
tagsPath,
|
||||
)
|
||||
tmpl.Tags = []string{}
|
||||
}
|
||||
|
||||
// title
|
||||
titleTemplate := template.New("title").Funcs(templateFuncs)
|
||||
if titleFile, err := os.ReadFile(titlePath); err == nil {
|
||||
tmpl.Title, err = titleTemplate.Parse(string(titleFile))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse title template: %v", err)
|
||||
}
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
log.Fatalf(
|
||||
"%s not found. Falling back to default template:\n%s",
|
||||
titlePath,
|
||||
defaultTitleTemplate,
|
||||
)
|
||||
tmpl.Title, err = titleTemplate.Parse(defaultTitleTemplate)
|
||||
if err != nil { panic(err) }
|
||||
|
||||
os.WriteFile(titlePath, []byte(defaultTitleTemplate), 0644)
|
||||
}
|
||||
|
||||
// description
|
||||
descriptionTemplate := template.New("description").Funcs(templateFuncs,)
|
||||
if descriptionFile, err := os.ReadFile(descriptionPath); err == nil {
|
||||
tmpl.Description, err = descriptionTemplate.Parse(string(descriptionFile))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse description template: %v", err)
|
||||
}
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
log.Fatalf(
|
||||
"%s not found. Falling back to default template:\n%s",
|
||||
descriptionPath,
|
||||
defaultDescriptionTemplate,
|
||||
)
|
||||
tmpl.Description, err = descriptionTemplate.Parse(defaultDescriptionTemplate)
|
||||
if err != nil { panic(err) }
|
||||
|
||||
os.WriteFile(descriptionPath, []byte(defaultDescriptionTemplate), 0644)
|
||||
}
|
||||
|
||||
return &tmpl, nil
|
||||
}
|
||||
|
||||
func BuildTemplate(video *Video, tmpl *template.Template) (string, error) {
|
||||
out := &bytes.Buffer{}
|
||||
|
||||
|
|
@ -221,7 +129,7 @@ func UploadVideo(
|
|||
ctx context.Context,
|
||||
tokenSource oauth2.TokenSource,
|
||||
video *Video,
|
||||
templates *Template,
|
||||
templates *templates.TextTemplates,
|
||||
) (*youtube.Video, error) {
|
||||
title, err := BuildTemplate(video, templates.Title)
|
||||
if err != nil { return nil, fmt.Errorf("failed to build title: %v", err) }
|
||||
|
|
@ -253,7 +161,7 @@ func UploadVideo(
|
|||
Snippet: &youtube.VideoSnippet{
|
||||
Title: title,
|
||||
Description: description,
|
||||
Tags: append(templates.Tags, video.Tags...),
|
||||
Tags: append(templates.DefaultTags, video.Tags...),
|
||||
CategoryId: categoryId, // gaming
|
||||
},
|
||||
Status: &youtube.VideoStatus{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue