-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for new tvdb map(#2)
* feat: add tvdb map * feat : add format command
- Loading branch information
Showing
4 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ func GetTvdbIDs() { | |
} | ||
|
||
StoreAnime(a, "./malid-anidbid-tvdbid.json") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |