Retrieve upload URL

This commit is contained in:
Carl Pearson
2025-05-02 05:39:15 -06:00
parent ccfaefbf46
commit 9abe867c31
2 changed files with 39 additions and 13 deletions

View File

@@ -115,8 +115,9 @@ func downloadPostHandler(c echo.Context) error {
} }
type Meta struct { type Meta struct {
title string title string
artist string artist string
uploadDate time.Time
} }
func getYtdlpTitle(url string, args []string) (string, error) { func getYtdlpTitle(url string, args []string) (string, error) {
@@ -172,6 +173,24 @@ func getYtdlpArtist(url string, args []string) (string, error) {
return strings.TrimSpace(string(stdout)), nil return strings.TrimSpace(string(stdout)), nil
} }
func getYtdlpUploadDate(url string, args []string) (time.Time, error) {
args = append(args, "--simulate", "--print", "%(upload_date>%Y-%m-%d)s", url)
stdout, _, err := ytdlp.Run(args...)
if err != nil {
log.Errorln(err)
return time.Time{}, err
}
dateStr := strings.TrimSpace(string(stdout))
uploadDate, err := time.Parse("2006-01-02", dateStr)
if err != nil {
return time.Time{}, fmt.Errorf("failed to parse date '%s': %w", dateStr, err)
}
return uploadDate, nil
}
func getYtdlpMeta(url string, args []string) (Meta, error) { func getYtdlpMeta(url string, args []string) (Meta, error) {
meta := Meta{} meta := Meta{}
@@ -185,6 +204,10 @@ func getYtdlpMeta(url string, args []string) (Meta, error) {
if err != nil { if err != nil {
return meta, err return meta, err
} }
meta.uploadDate, err = getYtdlpUploadDate(url, args)
if err != nil {
return meta, err
}
return meta, nil return meta, nil
} }
@@ -546,8 +569,9 @@ func startDownload(originalID uint, videoURL string, audioOnly bool) {
} }
log.Debugf("original metadata %v", origMeta) log.Debugf("original metadata %v", origMeta)
err = db.Model(&originals.Original{}).Where("id = ?", originalID).Updates(map[string]interface{}{ err = db.Model(&originals.Original{}).Where("id = ?", originalID).Updates(map[string]interface{}{
"title": origMeta.title, "title": origMeta.title,
"artist": origMeta.artist, "artist": origMeta.artist,
"upload_date": origMeta.uploadDate,
}).Error }).Error
if err != nil { if err != nil {
log.Errorln("couldn't store metadata:", err) log.Errorln("couldn't store metadata:", err)

View File

@@ -2,6 +2,7 @@ package originals
import ( import (
"sync" "sync"
"time"
"ytdlp-site/database" "ytdlp-site/database"
"ytdlp-site/transcodes" "ytdlp-site/transcodes"
@@ -23,15 +24,16 @@ const (
type Original struct { type Original struct {
gorm.Model gorm.Model
UserID uint UserID uint
URL string URL string
Title string Title string
Artist string Artist string
Status Status UploadDate time.Time
Audio bool // video download requested Status Status
Video bool // audio download requested Audio bool // video download requested
Watched bool Video bool // audio download requested
Timestamp float64 Watched bool
Timestamp float64
Playlist bool // part of a playlist Playlist bool // part of a playlist
PlaylistID uint // Playlist.ID (if part of a playlist) PlaylistID uint // Playlist.ID (if part of a playlist)