From 44f851aa3e2b529cb3cc35f3c2f601c5b4b40771 Mon Sep 17 00:00:00 2001 From: Rohit Vardam <46842159+varoOP@users.noreply.github.com> Date: Wed, 27 Sep 2023 00:33:18 -0700 Subject: [PATCH] feat: add support for new tvdb map(#2) * feat: add tvdb map * feat : add format command --- cmd/shinkrodb/main.go | 15 ++++ internal/domain/tvdb.go | 2 +- internal/format/format.go | 33 ++++++++ internal/tvdbmap/tvdbmap.go | 152 ++++++++++++++++++++++++++++++++++++ 4 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 internal/format/format.go create mode 100644 internal/tvdbmap/tvdbmap.go diff --git a/cmd/shinkrodb/main.go b/cmd/shinkrodb/main.go index a4cffb0..5f2307d 100644 --- a/cmd/shinkrodb/main.go +++ b/cmd/shinkrodb/main.go @@ -8,6 +8,8 @@ import ( "github.com/spf13/pflag" "github.com/varoOP/shinkrodb/internal/config" "github.com/varoOP/shinkrodb/internal/domain" + "github.com/varoOP/shinkrodb/internal/format" + "github.com/varoOP/shinkrodb/internal/tvdbmap" ) var ( @@ -30,6 +32,11 @@ func main() { domain.GetTmdbIds(cfg, rootPath) a := domain.GetAnime("./malid-anidbid-tvdbid-tmdbid.json") fmt.Println("Total number of dupes:", domain.CheckDupes(a)) + unmapped := tvdbmap.CreateAnimeTVDBMap(rootPath) + err := tvdbmap.UpdateMaster(unmapped, rootPath) + if err != nil { + log.Fatal(err) + } case "genmap": am := &domain.AnimeMovies{} @@ -39,6 +46,14 @@ func main() { } domain.CreateMapping(am, path.Join(rootPath, "tmdb-mal.yaml")) + err = tvdbmap.GenerateAnimeTVDBMap(rootPath) + if err != nil { + log.Fatal(err) + } + + case "format": + format.FormatTMDB() + format.FormatTVDB() case "version": fmt.Printf("shinkrodb: %v\n", version) diff --git a/internal/domain/tvdb.go b/internal/domain/tvdb.go index 2fe2c11..3b8a8bd 100644 --- a/internal/domain/tvdb.go +++ b/internal/domain/tvdb.go @@ -22,4 +22,4 @@ func GetTvdbIDs() { } StoreAnime(a, "./malid-anidbid-tvdbid.json") -} +} \ No newline at end of file diff --git a/internal/format/format.go b/internal/format/format.go new file mode 100644 index 0000000..9bdfcf0 --- /dev/null +++ b/internal/format/format.go @@ -0,0 +1,33 @@ +package format + +import ( + "log" + + "github.com/varoOP/shinkrodb/internal/domain" + "github.com/varoOP/shinkrodb/internal/tvdbmap" +) + +const tmdbPath string = "./tmdb-mal-master.yaml" +const tvdbPath string = "./tvdb-mal-master.yaml" + +func FormatTMDB() { + tmdb := &domain.AnimeMovies{} + err := tmdb.Get(tmdbPath) + if err != nil { + log.Fatal(err) + } + + tmdb.Store(tmdbPath) +} + +func FormatTVDB() { + tvdb, err := tvdbmap.GetAnimeTVDBMap(tvdbPath) + if err != nil { + log.Fatal(err) + } + + err = tvdb.Store(tvdbPath) + if err != nil { + log.Fatal(err) + } +} diff --git a/internal/tvdbmap/tvdbmap.go b/internal/tvdbmap/tvdbmap.go new file mode 100644 index 0000000..2ba596a --- /dev/null +++ b/internal/tvdbmap/tvdbmap.go @@ -0,0 +1,152 @@ +package tvdbmap + +import ( + "io" + "os" + "path/filepath" + "strings" + + "github.com/varoOP/shinkrodb/internal/domain" + "gopkg.in/yaml.v3" +) + +type AnimeTVDBMap struct { + Anime []Anime `yaml:"AnimeMap"` +} + +type Anime struct { + Malid int `yaml:"malid"` + Title string `yaml:"title"` + Type string `yaml:"type"` + Tvdbid int `yaml:"tvdbid"` + TvdbSeason int `yaml:"tvdbseason"` + Start int `yaml:"start"` + UseMapping bool `yaml:"useMapping"` + AnimeMapping []AnimeMapping `yaml:"animeMapping"` +} + +type AnimeMapping struct { + TvdbSeason int `yaml:"tvdbseason"` + Start int `yaml:"start"` +} + +func (am *AnimeTVDBMap) Store(path string) error { + b, err := yaml.Marshal(am) + if err != nil { + return err + } + + f, err := os.Create(path) + if err != nil { + return err + } + + text := string(b) + lines := strings.Split(text, "\n") + malidFound := false + for i, line := range lines { + if strings.Contains(line, "malid") { + if malidFound { + lines[i-1] += "\n" + } else { + malidFound = true + } + } + } + + modifiedText := strings.Join(lines, "\n") + defer f.Close() + _, err = f.Write([]byte(modifiedText)) + if err != nil { + return err + } + + return nil +} + +func GetAnimeTVDBMap(path string) (*AnimeTVDBMap, error) { + am := &AnimeTVDBMap{} + f, err := os.Open(path) + if err != nil { + return nil, err + } + + defer f.Close() + b, err := io.ReadAll(f) + if err != nil { + return nil, err + } + + err = yaml.Unmarshal(b, am) + if err != nil { + return nil, err + } + + return am, nil +} + +func CreateAnimeTVDBMap(path string) *AnimeTVDBMap { + am := &AnimeTVDBMap{} + a := domain.GetAnime("./malid.json") + for _, anime := range a { + am.Anime = append(am.Anime, Anime{ + anime.MalID, + anime.MainTitle, + anime.Type, + 0, + 0, + 0, + false, + []AnimeMapping{}, + }) + } + + am.Store(filepath.Join(path, "tvdb-mal-unmapped.yaml")) + return am +} + +func UpdateMaster(unmapped *AnimeTVDBMap, path string) error { + master, err := GetAnimeTVDBMap(filepath.Join(path, "tvdb-mal-master.yaml")) + if err != nil { + return err + } + + masterMap := make(map[int]Anime) + for _, v := range master.Anime { + masterMap[v.Malid] = v + } + + for i, v := range unmapped.Anime { + if masterAnime, ok := masterMap[v.Malid]; ok { + unmapped.Anime[i].AnimeMapping = masterAnime.AnimeMapping + unmapped.Anime[i].Start = masterAnime.Start + unmapped.Anime[i].TvdbSeason = masterAnime.TvdbSeason + unmapped.Anime[i].Tvdbid = masterAnime.Tvdbid + unmapped.Anime[i].UseMapping = masterAnime.UseMapping + } + } + + err = master.Store(filepath.Join(path, "tvdb-mal-master.yaml")) + if err != nil { + return err + } + + return nil +} + +func GenerateAnimeTVDBMap(path string) error { + master, err := GetAnimeTVDBMap(filepath.Join(path, "tvdb-mal-master.yaml")) + if err != nil { + return err + } + + final := &AnimeTVDBMap{} + for _, anime := range master.Anime { + if anime.Tvdbid != 0 { + final.Anime = append(final.Anime, anime) + } + } + + final.Store(filepath.Join(path, "tvdb-mal.yaml")) + return nil +}