ytva/substring.go

43 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-12-31 18:59:03 -05:00
package main
import (
"regexp"
"reflect"
"fmt"
//"github.com/charmbracelet/log"
"github.com/wader/goutubedl"
)
func fillPlaceholders(input string, videoInfo goutubedl.Info) (output string) {
re := regexp.MustCompile(`%([^%]+)%`)
return re.ReplaceAllStringFunc(input, func(match string) string {
groups := re.FindStringSubmatch(match)
if len(groups) < 1 {
// No capture group found; return original match
return match
}
key := groups[1] // the captured part
v := reflect.ValueOf(videoInfo)
if v.Kind() == reflect.Struct {
fieldVal := v.FieldByName(key)
if fieldVal.IsValid() && fieldVal.CanInterface() {
// Haha cheating :)
if fieldVal.Kind() == reflect.Float64 {
return fmt.Sprintf("%v", int(fieldVal.Float()))
} else {
return fmt.Sprintf("%v", fieldVal.Interface())
}
}
}
// Value not found; return original match
return match
})
}