Skip to content

Commit

Permalink
feat: add support for new tvdb map(#2)
Browse files Browse the repository at this point in the history
* feat: add tvdb map

* feat : add format command
  • Loading branch information
varoOP authored Sep 27, 2023
1 parent 21bdf8f commit 44f851a
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cmd/shinkrodb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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{}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/tvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ func GetTvdbIDs() {
}

StoreAnime(a, "./malid-anidbid-tvdbid.json")
}
}
33 changes: 33 additions & 0 deletions internal/format/format.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
152 changes: 152 additions & 0 deletions internal/tvdbmap/tvdbmap.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 44f851a

Please sign in to comment.