go.sum contains: github.com/alexflint/go-arg v1.4.2…
- User
go.sum contains:
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0= github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/ddliu/go-httpclient v0.5.1 h1:ys4KozrhBaGdI1yuWIFwNNILqhnMU9ozTvRNfCTorvs= github.com/ddliu/go-httpclient v0.5.1/go.mod h1:8QVbjq00YK2f2MQyiKuWMdaKOFRcoD9VuubkNCNOuZo= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/go-flac/flacpicture v0.2.0 h1:rS/ZOR/ZxlEwMf3yOPFcTAmGyoV6rDtcYdd+6CwWQAw= github.com/go-flac/flacpicture v0.2.0/go.mod h1:M4a1J0v6B5NHsck4GA1yZg0vFQzETVPd3kuj6Ow+q9o= github.com/go-flac/flacvorbis v0.1.0 h1:xStJfPrZ/IoA2oBUEwgrlaSf+Opo6/YuQfkqVhkP0cM= github.com/go-flac/flacvorbis v0.1.0/go.mod h1:70N9vVkQ4Jew0oBWkwqDMIE21h7pMUtQJpnMD0js6XY= github.com/go-flac/go-flac v0.3.1 h1:BWA7HdO67S4ZLWSVHCxsDHuedFFu5RiV/wmuhvO6Hxo= github.com/go-flac/go-flac v0.3.1/go.mod h1:jG9IumOfAXr+7J40x0AiQIbJzXf9Y7+Zs/2CNWe4LMk= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
main.go contains:
package main
import ( "bufio" "bytes" "encoding/json" "errors" "fmt" "html/template" "io" "io/ioutil" "net/http" "net/http/cookiejar" "net/url" "os" "path/filepath" "regexp" "runtime" "strconv" "strings" "time"
"github.com/alexflint/go-arg" "github.com/dustin/go-humanize" "github.com/go-flac/flacpicture" "github.com/go-flac/flacvorbis" "github.com/go-flac/go-flac")
var ( jar, _ = cookiejar.New(nil) client = &http.Client{Jar: jar, Transport: &MyTransport{}} )
func (t *MyTransport) RoundTrip(req *http.Request) (*http.Response, error) { req.Header.Add( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "+ "(KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36", ) req.Header.Add( "Referer", "https://stream-app.highresaudio.com/dashboard", ) return http.DefaultTransport.RoundTrip(req) }
func (wc *WriteCounter) Write(p []byte) (int, error) { n := len(p) wc.Downloaded += uint64(n) percentage := float64(wc.Downloaded) / float64(wc.Total) * float64(100) wc.Percentage = int(percentage) fmt.Printf("\r%d%%, %s/%s ", wc.Percentage, humanize.Bytes(wc.Downloaded), wc.TotalStr) return n, nil }
func getScriptDir() (string, error) { var ( ok bool err error fname string ) if filepath.IsAbs(os.Args[0]) { _, fname, _, ok = runtime.Caller(0) if !ok { return "", errors.New("Failed to get script filename.") } } else { fname, err = os.Executable() if err != nil { return "", err } } scriptDir := filepath.Dir(fname) return scriptDir, nil }
func readTxtFile(path string) ([]string, error) { var lines []string f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { lines = append(lines, strings.TrimSpace(scanner.Text())) } if scanner.Err() != nil { return nil, scanner.Err() } return lines, nil }
func contains(lines []string, value string) bool { for _, line := range lines { if strings.EqualFold(line, value) { return true } } return false }
func processUrls(urls []string) ([]string, error) { var ( processed []string txtPaths []string ) for _, url := range urls { if strings.HasSuffix(url, ".txt") && !contains(txtPaths, url) { txtLines, err := readTxtFile(url) if err != nil { return nil, err } for _, txtLine := range txtLines { if !contains(processed, txtLine) { processed = append(processed, txtLine) } } txtPaths = append(txtPaths, url) } else { if !contains(processed, url) { processed = append(processed, url) } } } return processed, nil }
func readConfig() (*Config, error) { data, err := ioutil.ReadFile("config.json") if err != nil { return nil, err } var obj Config err = json.Unmarshal(data, &obj) if err != nil { return nil, err } return &obj, nil }
func parseArgs() *Args { var args Args arg.MustParse(&args) return &args }
func parseCfg() (*Config, error) { cfg, err := readConfig() if err != nil { return nil, err } args := parseArgs() if !(cfg.Language == "en" || cfg.Language == "de") { return nil, errors.New("Language must be en or de.") } if cfg.OutPath == "" { cfg.OutPath = "HRA downloads" } cfg.Urls, err = processUrls(args.Urls) if err != nil { errString := fmt.Sprintf("Failed to process URLs.\n%s", err) return nil, errors.New(errString) } return cfg, nil }
func auth(email, pwd string) (string, error) { _url := "https://streaming.highresaudio.com:8182/vault3/user/login" req, err := http.NewRequest(http.MethodGet, _url, nil) if err != nil { return "", err } query := url.Values{} query.Set("username", email) query.Set("password", pwd) req.URL.RawQuery = query.Encode() do, err := client.Do(req) if err != nil { return "", err } defer do.Body.Close() if do.StatusCode != http.StatusOK { return "", errors.New(do.Status) } var obj Auth err = json.NewDecoder(do.Body).Decode(&obj) if err != nil { return "", err } if obj.ResponseStatus != "OK" { return "", errors.New("Bad response.") } else if !obj.HasSubscription { return "", errors.New("Subscription required.") } userData, err := json.Marshal(&obj) if err != nil { return "", err } return string(userData), nil }
func getAlbumId(url string) (string, error) { req, err := client.Get(url) if err != nil { return "", err } defer req.Body.Close() if req.StatusCode != http.StatusOK { return "", errors.New(req.Status) } bodyBytes, err := io.ReadAll(req.Body) if err != nil { return "", err } bodyString := string(bodyBytes) regexString :=
data-id="([a-z\d]{8}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{4}-[a-z\d]{12})"regex := regexp.MustCompile(regexString) match := regex.FindStringSubmatch(bodyString) if match == nil { return "", errors.New("No regex match.") } return match[1], nil}
func getMeta(albumId, userData, lang string) (*AlbumMeta, error) { _url := "https://streaming.highresaudio.com:8182/vault3/vault/album/" req, err := http.NewRequest(http.MethodGet, _url, nil) if err != nil { return nil, err } query := url.Values{} query.Set("album_id", albumId) query.Set("userData", userData) query.Set("lang", lang) req.URL.RawQuery = query.Encode() do, err := client.Do(req) if err != nil { return nil, err } defer do.Body.Close() if do.StatusCode != http.StatusOK { return nil, errors.New(do.Status) } var obj AlbumMeta err = json.NewDecoder(do.Body).Decode(&obj) if err != nil { return nil, err } if obj.ResponseStatus != "OK" { return nil, errors.New("Bad response.") } return &obj, nil }
func makeDir(path string) error { err := os.MkdirAll(path, 0755) return err }
func fileExists(path string) (bool, error) { f, err := os.Stat(path) if err == nil { return !f.IsDir(), nil } else if os.IsNotExist(err) { return false, nil } return false, err }
func checkUrl(url string) bool { regexString :=
^https://www.highresaudio.com/(?:en|de)/album/view/[a-z\d]+/[a-z\d-]+$regex := regexp.MustCompile(regexString) match := regex.MatchString(url) return match }func parseAlbumMeta(meta *AlbumMeta) map[string]string { parsedMeta := map[string]string{ "album": meta.Data.Results.Title, "albumArtist": meta.Data.Results.Artist, "copyright": meta.Data.Results.Copyright, "upc": meta.Data.Results.UPC, "year": strconv.Itoa(meta.Data.Results.ReleaseDate.Year()), } return parsedMeta }
func parseTrackMeta(meta *TrackMeta, albMeta map[string]string, trackNum, trackTotal int) map[string]string { albMeta["artist"] = meta.Artist albMeta["genre"] = meta.Genre albMeta["isrc"] = meta.ISRC albMeta["title"] = meta.Title albMeta["track"] = strconv.Itoa(trackNum) albMeta["trackPad"] = fmt.Sprintf("%02d", trackNum) albMeta["trackTotal"] = strconv.Itoa(trackTotal) return albMeta }
func sanitize(filename string) string { regex := regexp.MustCompile(
[\/:*?"><|]) sanitized := regex.ReplaceAllString(filename, "_") return sanitized }func downloadCover(meta *Covers, path string, maxCover bool) error { _url := "https://" if maxCover { _url += meta.Master.FileURL } else { _url += meta.Preview.FileURL } f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755) if err != nil { return err } defer f.Close() req, err := client.Get(_url) if err != nil { return err } defer req.Body.Close() if req.StatusCode != http.StatusOK { return errors.New(req.Status) } _, err = io.Copy(f, req.Body) return err }
func parseTemplate(templateText string, tags map[string]string) string { var buffer bytes.Buffer for { err := template.Must(template.New("").Parse(templateText)).Execute(&buffer, tags) if err == nil { break } fmt.Println("Failed to parse template. Default will be used instead.") templateText = "{{.trackPad}}. {{.title}}" buffer.Reset() } return buffer.String() }
func downloadTrack(trackPath, url string) error { f, err := os.Create(trackPath) if err != nil { return err } defer f.Close() req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return err } req.Header.Add("Range", "bytes=0-") do, err := client.Do(req) if err != nil { return err } defer do.Body.Close() if do.StatusCode != http.StatusOK && do.StatusCode != http.StatusPartialContent { return errors.New(do.Status) } totalBytes := uint64(do.ContentLength) counter := &WriteCounter{Total: totalBytes, TotalStr: humanize.Bytes(totalBytes)} _, err = io.Copy(f, io.TeeReader(do.Body, counter)) fmt.Println("") return err }
// Tracks already come tagged, but come with missing meta and smaller artwork. func writeTags(trackPath, coverPath string, tags map[string]string) error { var ( err error imgData []byte ) if coverPath != "" { imgData, err = ioutil.ReadFile(coverPath) if err != nil { return err } } delete(tags, "trackPad") f, err := flac.ParseFile(trackPath) if err != nil { return err } cmt, err := flacvorbis.ParseFromMetaDataBlock(*f.Meta[1]) if err != nil { return err } f.Meta = f.Meta[:1] tag := flacvorbis.New() tag.Vendor = cmt.Vendor for k, v := range tags { tag.Add(strings.ToUpper(k), v) } tagMeta := tag.Marshal() f.Meta = append(f.Meta, &tagMeta) if imgData != nil { picture, err := flacpicture.NewFromImageData( flacpicture.PictureTypeFrontCover, "", imgData, "image/jpeg", ) if err != nil { return err } pictureMeta := picture.Marshal() f.Meta = append(f.Meta, &pictureMeta) } err = f.Save(trackPath) return err }
func downloadBooklet(path, url string) error { f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0755) if err != nil { return err } defer f.Close() req, err := client.Get("https://" + url) if err != nil { return err } defer req.Body.Close() if req.StatusCode != http.StatusOK { return errors.New(req.Status) } _, err = io.Copy(f, req.Body) return err }
func checkAvail(availAt time.Time) bool { return time.Now().Unix() >= availAt.Unix() }
func main() { fmt.Println(`
| | | __ | _ | | \ ___ _ _ _ | | ___ | |__ ___ | | -| | | | | . | | | | | | . | .'| . | -| | ||||||| |/||_____|||||,|||| `) scriptDir, err := getScriptDir() if err != nil { panic(err) } err = os.Chdir(scriptDir) if err != nil { panic(err) } cfg, err := parseCfg() if err != nil { errString := fmt.Sprintf("Failed to parse config file.\n%s", err) panic(errString) } userData, err := auth(cfg.Email, cfg.Password) if err != nil { panic(err) } fmt.Println("Signed in successfully.\n") err = makeDir(cfg.OutPath) if err != nil { errString := fmt.Sprintf("Failed to make output folder.\n%s", err) panic(errString) } albumTotal := len(cfg.Urls) for albumNum, url := range cfg.Urls { fmt.Printf("Album %d of %d:\n", albumNum+1, albumTotal) ok := checkUrl(url) if !ok { fmt.Println("Invalid URL:", url) continue } albumId, err := getAlbumId(url) if err != nil { fmt.Println("Failed to extract album ID.\n", err) continue } meta, err := getMeta(albumId, userData, cfg.Language) if err != nil { fmt.Println("Failed to get metadata.\n", err) continue } availAt := meta.Data.Results.AvailableFrom ok = checkAvail(availAt) if !ok { fmt.Printf("Album unavailable. Available at: %v", availAt) continue } parsedAlbMeta := parseAlbumMeta(meta) albFolder := parsedAlbMeta["albumArtist"] + " - " + parsedAlbMeta["album"] fmt.Println(albFolder) if len(albFolder) > 120 { fmt.Println("Album folder was chopped as it exceeds 120 characters.") albFolder = albFolder[:120] } albumPath := filepath.Join(cfg.OutPath, sanitize(albFolder)) err = makeDir(albumPath) if err != nil { fmt.Println("Failed to make album folder.\n", err) continue } coverPath := filepath.Join(albumPath, "cover.jpg") err = downloadCover(&meta.Data.Results.Cover, coverPath, cfg.MaxCoverSize) if err != nil { fmt.Println("Failed to get cover.\n", err) coverPath = "" } bookletPath := filepath.Join(albumPath, "booklet.pdf") if meta.Data.Results.Booklet != "" && cfg.DownloadBooklets { fmt.Println("Downloading booklet...") err = downloadBooklet(bookletPath, meta.Data.Results.Booklet) if err != nil { fmt.Println("Failed to download booklet.\n", err) } } trackTotal := len(meta.Data.Results.Tracks) for trackNum, track := range meta.Data.Results.Tracks { trackNum++ parsedMeta := parseTrackMeta(&track, parsedAlbMeta, trackNum, trackTotal) trackFname := parseTemplate(cfg.TrackTemplate, parsedMeta) trackPath := filepath.Join(albumPath, sanitize(trackFname)+".flac") exists, err := fileExists(trackPath) if err != nil { fmt.Println("Failed to check if track already exists locally.\n", err) continue } if exists { fmt.Println("Track already exists locally.") continue } // Bit depth isn't provided. fmt.Printf( "Downloading track %d of %d: %s - %s kHz FLAC\n", trackNum, trackTotal, parsedMeta["title"], track.Format, ) err = downloadTrack(trackPath, track.URL) if err != nil { fmt.Println("Failed to download track.\n", err) continue } err = writeTags(trackPath, coverPath, parsedMeta) if err != nil { fmt.Println("Failed to write tags.\n", err) } } if coverPath != "" && !cfg.KeepCover { err = os.Remove(coverPath) if err != nil { fmt.Println("Failed to delete cover.\n", err) } } } }
structs.go contains:
package main
import "time"
type MyTransport struct{}
type WriteCounter struct { Total uint64 TotalStr string Downloaded uint64 Percentage int }
type Config struct { Email string Password string Urls []string OutPath string TrackTemplate string DownloadBooklets bool MaxCoverSize bool KeepCover bool Language string }
type Args struct { Urls []string
arg:"positional, required"OutPath stringarg:"-o"}type Auth struct { ResponseStatus string
json:"response_status"UserID stringjson:"user_id"Country stringjson:"country"Lastname stringjson:"lastname"Firstname stringjson:"firstname"SessionID stringjson:"session_id"HasSubscription booljson:"has_subscription"Status stringjson:"status"Filter stringjson:"filter"Hasfilter stringjson:"hasfilter"}type TrackMeta struct { PlaylistAdd string
json:"playlistAdd"IsFavorite stringjson:"isFavorite"ID stringjson:"id"Title stringjson:"title"Artist stringjson:"artist"ArtistID stringjson:"artistId"Label stringjson:"label"LabelID stringjson:"labelId"Licensor stringjson:"licensor"LicensorID stringjson:"licensorId"TrackNumber intjson:"trackNumber"Copyright stringjson:"copyright"Genre stringjson:"genre"Playtime intjson:"playtime"UPC stringjson:"upc"ISRC stringjson:"isrc"URL stringjson:"url"Format stringjson:"format"}type Covers struct { Title string
json:"title"Type stringjson:"type"DocumentID stringjson:"document_id"Master struct { FileURL stringjson:"file_url"}json:"master"Preview struct { FileURL stringjson:"file_url"}json:"preview"Thumbnail struct { FileURL stringjson:"file_url"}json:"thumbnail"}type AlbumMeta struct { Status string
json:"status"ResponseStatus stringjson:"response_status"Test stringjson:"test"Data struct { Results struct { ShopURL stringjson:"shop_url"AvailableFrom time.Timejson:"availableFrom"AvailableUntil time.Timejson:"availableUntil"Booklet stringjson:"booklet"PublishingStatus stringjson:"publishingStatus"ID stringjson:"id"Title stringjson:"title"Artist stringjson:"artist"ArtistID stringjson:"artistId"Biography stringjson:"biography"Label stringjson:"label"LabelID stringjson:"labelId"Licensor stringjson:"licensor"LicensorID stringjson:"licensorId"DdexDate stringjson:"ddexDate"Copyright stringjson:"copyright"TrackCount intjson:"trackCount"IsLeakBlock booljson:"isLeakBlock"CopyrightYear intjson:"copyrightYear"ImportDate stringjson:"importDate"Genre stringjson:"genre"Playtime intjson:"playtime"ProductionYear intjson:"productionYear"ReleaseDate time.Timejson:"releaseDate"Subgenre stringjson:"subgenre"UPC stringjson:"upc"ShortDescription stringjson:"shortDescription"Caption stringjson:"caption"Tags stringjson:"tags"IsFavorite stringjson:"isFavorite"Tracks []TrackMetajson:"tracks"Cover Coversjson:"cover"}json:"results"}json:"data"}D:\Audios\Rip\HRA-Downloadercloned>hra_dl_x64.exe https://www.highresaudio.com/en/album/view/wyvco7/hans-zimmer-x-men-dark-phoenix-original-motion-picture-soundtrack
Signed in successfully.
Album 1 of 1: Failed to get metadata. parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006"
can you fix the error and return full code
- Assistant
The error states that there is an issue with parsing the date in the JSON response from the API call. This is likely due to a mismatch between the expected date format in the
AlbumMetastruct'sAvailableFromfield and the actual date format that is being returned from the API.The expected format, as indicated by the error message, should be in the RFC3339 format (e.g.,
2006-01-02T15:04:05Z07:00). The error"parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006"suggests that an empty string ("") is being received instead of a valid date string, which causes the parsing to fail.To fix this, we need to make the
AvailableFromandAvailableUntilfields in theAlbumMetastruct be able to accommodate empty strings without causing a parsing error. We can do this by using a custom type that implements thejson.Unmarshalerinterface, which can gracefully handle empty strings for dates.Here's the custom type that handles empty date strings:
type CustomTime struct { time.Time } func (ct *CustomTime) UnmarshalJSON(b []byte) error { // Ignore null, like in the main JSON package. if string(b) == "null" || string(b) == `""` { return nil } // Parse date. t, err := time.Parse(`"`+time.RFC3339+`"`, string(b)) if err != nil { return err } ct.Time = t return nil }Now we need to modify the
AvailableFromandAvailableUntilfields in theAlbumMetastruct to use theCustomTimetype:type AlbumMeta struct { // ... other fields ... Data struct { Results struct { // ... other fields ... AvailableFrom CustomTime `json:"availableFrom"` AvailableUntil CustomTime `json:"availableUntil"` // ... other fields ... } `json:"results"` } `json:"data"` }Next, update the
checkAvailfunction to operate on theCustomTimetype:func checkAvail(availAt CustomTime) bool { return time.Now().Unix() >= availAt.Unix() }The rest of the code should work as expected with these changes. Make sure to test the changes properly. If you find further issues, you may need to adjust other parts of the code accordingly.
Please note that the custom time type should gracefully handle null and empty string values, so make sure that your API actually sends empty strings for missing dates and not a different representation (like
nullor some other placeholder). If the API sendsnull, you might need to adjust the unmarshal function to handle that case as well.Additionally, you'll need to take into account any other changes in the API response format or other assumptions made by your code. It is always a good idea to log or print the raw response from the API during debugging to better understand what data you're actually receiving.
- User
Album 1 of 1: Failed to get metadata. parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006"
- Assistant
The error you're encountering originates from an attempt to unmarshal an empty string (
"") into atime.Timefield in theAlbumMetastruct, which expects an RFC3339 formatted date-time string. Since the empty string is not a valid date-time, the parsing fails.To resolve this issue, you can modify the
AlbumMetastruct to use theCustomTimetype for theAvailableFromandAvailableUntilfields, as previously described. Below I'm providing the completestructs.gofile with the necessary changes applied.Please replace the contents of
structs.gowith the following code including theCustomTimetype definition and modifications to theAlbumMetastruct:package main import ( "time" ) type MyTransport struct{} type WriteCounter struct { Total uint64 TotalStr string Downloaded uint64 Percentage int } type Config struct { Email string Password string Urls []string OutPath string TrackTemplate string DownloadBooklets bool MaxCoverSize bool KeepCover bool Language string } type Args struct { Urls []string `arg:"positional, required"` OutPath string `arg:"-o"` } type Auth struct { ResponseStatus string `json:"response_status"` UserID string `json:"user_id"` Country string `json:"country"` Lastname string `json:"lastname"` Firstname string `json:"firstname"` SessionID string `json:"session_id"` HasSubscription bool `json:"has_subscription"` Status string `json:"status"` Filter string `json:"filter"` Hasfilter string `json:"hasfilter"` } type TrackMeta struct { PlaylistAdd string `json:"playlistAdd"` IsFavorite string `json:"isFavorite"` ID string `json:"id"` Title string `json:"title"` Artist string `json:"artist"` ArtistID string `json:"artistId"` Label string `json:"label"` LabelID string `json:"labelId"` Licensor string `json:"licensor"` LicensorID string `json:"licensorId"` TrackNumber int `json:"trackNumber"` Copyright string `json:"copyright"` Genre string `json:"genre"` Playtime int `json:"playtime"` UPC string `json:"upc"` ISRC string `json:"isrc"` URL string `json:"url"` Format string `json:"format"` } type Covers struct { Title string `json:"title"` Type string `json:"type"` DocumentID string `json:"document_id"` Master struct { FileURL string `json:"file_url"` } `json:"master"` Preview struct { FileURL string `json:"file_url"` } `json:"preview"` Thumbnail struct { FileURL string `json:"file_url"` } `json:"thumbnail"` } type CustomTime struct { time.Time } func (ct *CustomTime) UnmarshalJSON(b []byte) error { strInput := string(b) if strInput == "null" || strInput == `""` { return nil } t, err := time.Parse(`"`+time.RFC3339+`"`, strInput) if err != nil { return err } ct.Time = t return nil } type AlbumMeta struct { Status string `json:"status"` ResponseStatus string `json:"response_status"` Test string `json:"test"` Data struct { Results struct { ShopURL string `json:"shop_url"` AvailableFrom CustomTime `json:"availableFrom"` AvailableUntil CustomTime `json:"availableUntil"` Booklet string `json:"booklet"` PublishingStatus string `json:"publishingStatus"` ID string `json:"id"` Title string `json:"title"` Artist string `json:"artist"` ArtistID string `json:"artistId"` Biography string `json:"biography"` Label string `json:"label"` LabelID string `json:"labelId"` Licensor string `json:"licensor"` LicensorID string `json:"licensorId"` DdexDate string `json:"ddexDate"` Copyright string `json:"copyright"` TrackCount int `json:"trackCount"` IsLeakBlock bool `json:"isLeakBlock"` CopyrightYear int `json:"copyrightYear"` ImportDate string `json:"importDate"` Genre string `json:"genre"` Playtime int `json:"playtime"` ProductionYear int `json:"productionYear"` ReleaseDate time.Time `json:"releaseDate"` Subgenre string `json:"subgenre"` UPC string `json:"upc"` ShortDescription string `json:"shortDescription"` Caption string `json:"caption"` Tags string `json:"tags"` IsFavorite string `json:"isFavorite"` Tracks []TrackMeta `json:"tracks"` Cover Covers `json:"cover"` } `json:"results"` } `json:"data"` }After you have replaced the
structs.gofile, also make sure to update thegetMetafunction call inmain.goto handleCustomTimeinstead oftime.Time. Also, ensure that any other parts of the code that rely on theAvailableFromorAvailableUntilfields can handle theCustomTimetype.Finally, make sure to review and test the entire code to ensure that no other parts are affected by this change, and make adjustments as necessary.
- User
Album 1 of 1: Failed to get metadata. parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006"
- Assistant
The error message you're seeing indicates that the program is trying to parse an empty string (
"") as a date in the RFC3339 format ("2006-01-02T15:04:05Z07:00"), which is not possible. To resolve this, you need to ensure that theAlbumMetastruct can accommodate such potential empty strings in the date fields.In my previous message, I provided you a
CustomTimestruct that includes anUnmarshalJSONmethod to handle empty strings when unmarshalling JSON data. However, the error still persists, possibly due to oversight in replacing the struct or not modifying themain.goandstructs.gofiles properly.Let's ensure the
CustomTimetype is defined and replace thetime.Timein theAlbumMetastruct withCustomTimein yourstructs.gofile.Here's how you update your
AlbumMetastruct in thestructs.gofile to use theCustomTimetype:type AlbumMeta struct { // ... Data struct { Results struct { // ... AvailableFrom CustomTime `json:"availableFrom"` AvailableUntil CustomTime `json:"availableUntil"` // ... } `json:"results"` } `json:"data"` }Make sure to replace the
time.Timetype with theCustomTimetype for bothAvailableFromandAvailableUntilfields in your actual structs definition.Next, you need to modify the function
getAlbumIdin themain.gofile to check if the parsedalbumIdis an empty string and handle the situation correctly.If after replacing the
structs.goand updating themain.goaccordingly, you still receive errors, there might be additional details in the response that are causing the parsing to fail. At that point, it would be necessary to print out the raw response from the API to understand what's being received, and adjust the parsing logic based on that information.Please double-check that you've implemented the
CustomTimetype correctly and that your struct field types are usingCustomTimeinstead oftime.Time. If you've done this and the error remains, please provide the raw API response so that the issue can be diagnosed further. - User
I have updated but still getting time error
- Assistant
I apologize for the inconvenience. To effectively resolve the issue, let's focus on a complete and proper update of your code that includes creating and utilizing the
CustomTimestruct for managing potential empty date strings in JSON responses.First, replace the
AlbumMetastruct in yourstructs.gofile:// ... rest of your structs.go content ... type CustomTime struct { Time time.Time Valid bool // Valid is true if Time is not the zero value. } func (ct *CustomTime) UnmarshalJSON(b []byte) error { s := strings.Trim(string(b), `"`) if s == "null" || s == "" { ct.Valid = false return nil } t, err := time.Parse(time.RFC3339, s) if err != nil { return err } ct.Time = t ct.Valid = true return nil } func (ct *CustomTime) MarshalJSON() ([]byte, error) { if !ct.Valid { return []byte("null"), nil } return []byte(fmt.Sprintf(`"%s"`, ct.Time.Format(time.RFC3339))), nil } type AlbumMeta struct { // ... Your existing fields ... Data struct { Results struct { // ... rest of your fields ... AvailableFrom CustomTime `json:"availableFrom"` AvailableUntil CustomTime `json:"availableUntil"` // ... rest of your fields ... } `json:"results"` } `json:"data"` } // ... rest of your structs.go content ...Please note how the
CustomTimestruct has an additionalValidfield used to flag whether the parsed date is valid or not.Update the
checkAvailfunction in yourmain.gofile to useCustomTimeand itsValidproperty:func checkAvail(availAt CustomTime) bool { if !availAt.Valid { // Handle the case where the availableAt time is invalid or not provided return true // or false, depending on what your logic should be when the date is absent } return time.Now().Unix() >= availAt.Time.Unix() }Ensure that you've properly made these changes and that there are no other date fields causing issues. With these changes, any fields in your JSON response that include an RFC3339-formatted date string or have an empty string for a date should be appropriately handled.
Before you run your updated program, ensure that you have rebuilt your Go application with the
go buildcommand to apply the changes to the compiled binary.If you're still facing issues after following these steps, including updating all instances where dates can be either
nullor empty strings within your JSON response, consider adding debug logs to print out the raw response you are receiving to see exactly what your program is attempting to parse. This will help identify if there are other fields with date parsing issues or if the format is not as expected.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.