subsyt/internal/model/episode.go
Viktor Varland 09134c46c4
Some checks are pending
build / build (push) Waiting to run
refactor: split logic into distinct parts
2025-10-01 21:21:59 +02:00

75 lines
1.9 KiB
Go

package model
import (
"encoding/json"
"encoding/xml"
"fmt"
"os"
"time"
)
// Target Episode XML structure:
//
// <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
// <episodedetails>
// <title>#{safe(metadata["title"])}</title>
// <showtitle>#{safe(metadata["uploader"])}</showtitle>
// <uniqueid type="youtube" default="true">#{safe(metadata["id"])}</uniqueid>
// <plot>#{safe(metadata["description"])}</plot>
// <aired>#{safe(upload_date)}</aired>
// <season>#{safe(season)}</season>
// <episode>#{episode}</episode>
// <genre>YouTube</genre>
// </episodedetails>
type Episode struct {
XMLName xml.Name `xml:"episodedetails"`
Title string `json:"title" xml:"title"`
ShowTitle string `json:"channel" xml:"showtitle"`
Id string `json:"id" xml:"-"`
UniqueId UniqueId `json:"-" xml:"uniqueid"`
Plot string `json:"description" xml:"plot"`
Ext string `json:"ext" xml:"-"`
UploadDate string `json:"upload_date" xml:"-"`
Aired string `json:"-" xml:"aired"`
Season string `json:"-" xml:"season"`
Episode string `json:"-" xml:"episode"`
Genre string `json:"-" xml:"genre"`
InfoPath string `json:"-" xml:"-"`
}
func LoadEpisode(info_path string) Episode {
info, err := os.ReadFile(info_path)
if err != nil {
panic(err)
}
episode := Episode{}
err = json.Unmarshal(info, &episode)
if err != nil {
panic(err)
}
parsed, err := time.Parse("20060102", episode.UploadDate)
if err != nil {
panic(err)
}
episode.Aired = parsed.String()
episode.Season = fmt.Sprintf("s%d", parsed.Year())
episode.Episode = fmt.Sprintf("e%02d%02d", parsed.Month(), parsed.Day())
// these fields cannot be inferred
episode.Genre = "YouTube"
episode.UniqueId = UniqueId{
Default: "true",
Type: "youtube",
Text: episode.Id,
}
episode.InfoPath = info_path
return episode
}