update README, lots of polish
This commit is contained in:
parent
4624c56e54
commit
ce836b2d46
11 changed files with 210 additions and 121 deletions
|
|
@ -11,7 +11,7 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
|
||||
"arimelody.space/live-vod-uploader/scanner"
|
||||
"arimelody.space/vodular/scanner"
|
||||
"golang.org/x/oauth2"
|
||||
"google.golang.org/api/option"
|
||||
"google.golang.org/api/youtube/v3"
|
||||
|
|
@ -69,7 +69,7 @@ func BuildVideo(metadata *scanner.Metadata) (*Video, error) {
|
|||
Category: category,
|
||||
Part: metadata.Part,
|
||||
Date: videoDate,
|
||||
Tags: DefaultTags,
|
||||
Tags: metadata.Tags,
|
||||
Filename: path.Join(
|
||||
metadata.FootageDir,
|
||||
fmt.Sprintf(
|
||||
|
|
@ -88,10 +88,16 @@ type (
|
|||
|
||||
Metadata struct {
|
||||
Title string
|
||||
Date string
|
||||
Date time.Time
|
||||
Category *MetaCategory
|
||||
Part int
|
||||
}
|
||||
|
||||
Template struct {
|
||||
Title *template.Template
|
||||
Description *template.Template
|
||||
Tags []string
|
||||
}
|
||||
)
|
||||
|
||||
var videoCategoryTypeStrings = map[CategoryType]string{
|
||||
|
|
@ -107,19 +113,90 @@ var videoYtCategory = map[CategoryType]string {
|
|||
CATEGORY_ENTERTAINMENT: YT_CATEGORY_ENTERTAINMENT,
|
||||
}
|
||||
|
||||
var DefaultTags []string
|
||||
func GetDefaultTags(filepath string) ([]string, error) {
|
||||
file, err := os.ReadFile(filepath)
|
||||
if err != nil { return nil, err }
|
||||
var templateDir = "templates"
|
||||
var tagsPath = path.Join(templateDir, "tags.txt")
|
||||
var titlePath = path.Join(templateDir, "title.txt")
|
||||
var descriptionPath = path.Join(templateDir, "description.txt")
|
||||
|
||||
tags := strings.Split(string(file), "\n")
|
||||
return tags, nil
|
||||
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)
|
||||
},
|
||||
}
|
||||
|
||||
var titleTemplate *template.Template = template.Must(
|
||||
template.ParseFiles("template/title.txt"),
|
||||
)
|
||||
func BuildTitle(video *Video) (string, error) {
|
||||
func FetchTemplates() (*Template, error) {
|
||||
tmpl := Template{}
|
||||
|
||||
// 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{}
|
||||
|
||||
var category *MetaCategory
|
||||
|
|
@ -131,36 +208,9 @@ func BuildTitle(video *Video) (string, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: give templates date format and lowercase functions
|
||||
// these should not be hard-coded!
|
||||
err := titleTemplate.Execute(out, Metadata{
|
||||
err := tmpl.Execute(out, Metadata{
|
||||
Title: video.Title,
|
||||
Date: strings.ToLower(video.Date.Format("02 Jan 2006")),
|
||||
Category: category,
|
||||
Part: video.Part,
|
||||
})
|
||||
|
||||
return strings.TrimSpace(out.String()), err
|
||||
}
|
||||
|
||||
var descriptionTemplate *template.Template = template.Must(
|
||||
template.ParseFiles("template/description.txt"),
|
||||
)
|
||||
func BuildDescription(video *Video) (string, error) {
|
||||
out := &bytes.Buffer{}
|
||||
|
||||
var category *MetaCategory
|
||||
if video.Category != nil {
|
||||
category = &MetaCategory{
|
||||
Name: video.Category.Name,
|
||||
Type: videoCategoryTypeStrings[video.Category.Type],
|
||||
Url: video.Category.Url,
|
||||
}
|
||||
}
|
||||
|
||||
err := descriptionTemplate.Execute(out, Metadata{
|
||||
Title: video.Title,
|
||||
Date: strings.ToLower(video.Date.Format("02 Jan 2006")),
|
||||
Date: video.Date,
|
||||
Category: category,
|
||||
Part: video.Part,
|
||||
})
|
||||
|
|
@ -172,15 +222,12 @@ func UploadVideo(
|
|||
ctx context.Context,
|
||||
tokenSource oauth2.TokenSource,
|
||||
video *Video,
|
||||
templates *Template,
|
||||
) (*youtube.Video, error) {
|
||||
title, err := BuildTitle(video)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build title: %v", err)
|
||||
}
|
||||
description, err := BuildDescription(video)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to build description: %v", err)
|
||||
}
|
||||
title, err := BuildTemplate(video, templates.Title)
|
||||
if err != nil { return nil, fmt.Errorf("failed to build title: %v", err) }
|
||||
description, err := BuildTemplate(video, templates.Description)
|
||||
if err != nil { return nil, fmt.Errorf("failed to build description: %v", err) }
|
||||
|
||||
service, err := youtube.NewService(
|
||||
ctx,
|
||||
|
|
@ -207,7 +254,7 @@ func UploadVideo(
|
|||
Snippet: &youtube.VideoSnippet{
|
||||
Title: title,
|
||||
Description: description,
|
||||
Tags: append(DefaultTags, video.Tags...),
|
||||
Tags: append(templates.Tags, video.Tags...),
|
||||
CategoryId: categoryId, // gaming
|
||||
},
|
||||
Status: &youtube.VideoStatus{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue