Add drop:yt command and refine ytdlp.Drop functionality

This commit is contained in:
Dan Jones 2026-02-28 23:36:08 -06:00
commit b0f6628817
3 changed files with 98 additions and 16 deletions

View file

@ -12,23 +12,25 @@ import (
)
// Drop adds the URL specified by the url to the watched log.
func Drop(ctx context.Context, url string) (models.Entry, error) {
func Drop(ctx context.Context, url string) (models.Log, error) {
var ent models.Entry
var log models.Log
if ctx.Err() != nil {
return ent, context.Cause(ctx)
return log, context.Cause(ctx)
}
info, err := Fetch(ctx, url)
if err != nil {
return ent, err
return log, err
}
now := time.Now()
ent.Date = now
metas := &ent.Fields
metas.Set("id", getID(now, info))
*metas = metas.Set("id", getID(now, info))
source := FirstNonZero(info.ExtractorKey, info.WebpageURLDomain, new("Web Video"))
metas.Set("source", source)
*metas = metas.Set("source", source)
ent.Title = FirstNonZero(info.Title, info.AltTitle, new(fmt.Sprintf("%s %s", source, info.ID)))
@ -39,12 +41,12 @@ func Drop(ctx context.Context, url string) (models.Entry, error) {
// TODO Add show info
log := models.Log{Name: "watched", Entries: []models.Entry{ent}}
log = models.Log{Name: "watched", Entries: []models.Entry{ent}}
if err := files.Append(log); err != nil {
return ent, err
return log, err
}
return ent, nil
return log, nil
}
func getID(now time.Time, info *ytd.ExtractedInfo) string {
@ -63,34 +65,41 @@ func getID(now time.Time, info *ytd.ExtractedInfo) string {
func setNote(metas *models.Metas, info *ytd.ExtractedInfo) {
desc := FirstNonZero(info.Description)
if desc != "" {
metas.Set("note", 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.Set("artist", art)
*metas = metas.Set("artist", art)
}
}
func setTags(metas *models.Metas, info *ytd.ExtractedInfo) {
tags := append(info.Tags, info.Categories...)
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.Set("tags", tags)
*metas = metas.Set("tags", tags)
}
}
func setDuration(metas *models.Metas, info *ytd.ExtractedInfo) {
dur := FirstNonZero(info.Duration)
if dur > 0 {
metas.Set("duration", dur)
*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.Set("url", url)
*metas = metas.Set("url", url)
}
func FirstNonZero[T comparable](vals ...*T) T {