♻️ Extract GetLog and update Drop function
This commit is contained in:
parent
b0f6628817
commit
b5c6c84a5d
2 changed files with 107 additions and 89 deletions
|
|
@ -2,9 +2,6 @@ package ytdlp
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/files"
|
||||
"codeberg.org/danjones000/my-log/models"
|
||||
|
|
@ -13,101 +10,20 @@ import (
|
|||
|
||||
// Drop adds the URL specified by the url to the watched log.
|
||||
func Drop(ctx context.Context, url string) (models.Log, error) {
|
||||
var ent models.Entry
|
||||
var log models.Log
|
||||
if ctx.Err() != nil {
|
||||
return log, context.Cause(ctx)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return models.Log{}, context.Cause(ctx)
|
||||
default:
|
||||
}
|
||||
|
||||
info, err := Fetch(ctx, url)
|
||||
log, err := GetLog(ctx, url)
|
||||
if err != nil {
|
||||
return log, err
|
||||
}
|
||||
now := time.Now()
|
||||
ent.Date = now
|
||||
|
||||
metas := &ent.Fields
|
||||
*metas = metas.Set("id", getID(now, info))
|
||||
|
||||
source := FirstNonZero(info.ExtractorKey, info.WebpageURLDomain, new("Web Video"))
|
||||
*metas = metas.Set("source", source)
|
||||
|
||||
ent.Title = FirstNonZero(info.Title, info.AltTitle, new(fmt.Sprintf("%s %s", source, info.ID)))
|
||||
|
||||
setNote(metas, info)
|
||||
setArtist(metas, info)
|
||||
setTags(metas, info)
|
||||
setUrl(metas, info, url)
|
||||
|
||||
// TODO Add show info
|
||||
|
||||
log = models.Log{Name: "watched", Entries: []models.Entry{ent}}
|
||||
if err := files.Append(log); err != nil {
|
||||
return log, err
|
||||
}
|
||||
|
||||
return log, nil
|
||||
}
|
||||
|
||||
func getID(now time.Time, info *ytd.ExtractedInfo) string {
|
||||
id := new(strings.Builder)
|
||||
id.WriteString("tag:")
|
||||
id.WriteString(FirstNonZero(info.WebpageURLDomain, new("goodevilgenius.org")))
|
||||
id.WriteByte(',')
|
||||
id.WriteString(now.Format("2006"))
|
||||
id.WriteRune(':')
|
||||
id.WriteString(info.ID)
|
||||
id.WriteRune('/')
|
||||
fmt.Fprintf(id, "%d", now.Unix())
|
||||
return id.String()
|
||||
}
|
||||
|
||||
func setNote(metas *models.Metas, info *ytd.ExtractedInfo) {
|
||||
desc := FirstNonZero(info.Description)
|
||||
if desc != "" {
|
||||
desc = strings.SplitN(desc, "\n", 2)[0]
|
||||
*metas = metas.Set("note", desc)
|
||||
}
|
||||
}
|
||||
|
||||
func setArtist(metas *models.Metas, info *ytd.ExtractedInfo) {
|
||||
art := FirstNonZero(info.Artist, info.AlbumArtist, info.Uploader)
|
||||
if art != "" {
|
||||
*metas = metas.Set("artist", art)
|
||||
}
|
||||
}
|
||||
|
||||
func setTags(metas *models.Metas, info *ytd.ExtractedInfo) {
|
||||
tags := make([]any, 0, len(info.Tags)+len(info.Categories))
|
||||
for _, t := range info.Tags {
|
||||
tags = append(tags, t)
|
||||
}
|
||||
for _, c := range info.Categories {
|
||||
tags = append(tags, c)
|
||||
}
|
||||
if len(tags) > 0 {
|
||||
*metas = metas.Set("tags", tags)
|
||||
}
|
||||
}
|
||||
|
||||
func setDuration(metas *models.Metas, info *ytd.ExtractedInfo) {
|
||||
dur := FirstNonZero(info.Duration)
|
||||
if dur > 0 {
|
||||
*metas = metas.Set("duration", dur)
|
||||
}
|
||||
}
|
||||
|
||||
func setUrl(metas *models.Metas, info *ytd.ExtractedInfo, origUrl string) {
|
||||
url := FirstNonZero(info.WebpageURL, info.PageURL, info.URL, &origUrl)
|
||||
*metas = metas.Set("url", url)
|
||||
}
|
||||
|
||||
func FirstNonZero[T comparable](vals ...*T) T {
|
||||
var z T
|
||||
for _, v := range vals {
|
||||
if v != nil && *v != z {
|
||||
return *v
|
||||
}
|
||||
}
|
||||
return z
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue