104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package ytdlp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"codeberg.org/danjones000/my-log/files"
|
|
"codeberg.org/danjones000/my-log/models"
|
|
ytd "github.com/lrstanley/go-ytdlp"
|
|
)
|
|
|
|
// Drop adds the URL specified by the url to the watched log.
|
|
func Drop(ctx context.Context, url string) (models.Entry, error) {
|
|
var ent models.Entry
|
|
if ctx.Err() != nil {
|
|
return ent, context.Cause(ctx)
|
|
}
|
|
|
|
info, err := Fetch(ctx, url)
|
|
if err != nil {
|
|
return ent, err
|
|
}
|
|
now := time.Now()
|
|
|
|
metas := &ent.Fields
|
|
metas.Set("id", getID(now, info))
|
|
|
|
source := FirstNonZero(info.ExtractorKey, info.WebpageURLDomain, new("Web Video"))
|
|
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 ent, err
|
|
}
|
|
|
|
return ent, 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 != "" {
|
|
metas.Set("note", desc)
|
|
}
|
|
}
|
|
|
|
func setArtist(metas *models.Metas, info *ytd.ExtractedInfo) {
|
|
art := FirstNonZero(info.Artist, info.AlbumArtist, info.Uploader)
|
|
if art != "" {
|
|
metas.Set("artist", art)
|
|
}
|
|
}
|
|
|
|
func setTags(metas *models.Metas, info *ytd.ExtractedInfo) {
|
|
tags := append(info.Tags, info.Categories...)
|
|
if len(tags) > 0 {
|
|
metas.Set("tags", tags)
|
|
}
|
|
}
|
|
|
|
func setDuration(metas *models.Metas, info *ytd.ExtractedInfo) {
|
|
dur := FirstNonZero(info.Duration)
|
|
if dur > 0 {
|
|
metas.Set("duration", dur)
|
|
}
|
|
}
|
|
|
|
func setUrl(metas *models.Metas, info *ytd.ExtractedInfo, origUrl string) {
|
|
url := FirstNonZero(info.WebpageURL, info.PageURL, info.URL, &origUrl)
|
|
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
|
|
}
|