Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tvdbmap #2

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}