diff --git a/cmd/shinkrodb/main.go b/cmd/shinkrodb/main.go index f026e0d..aefe6b5 100644 --- a/cmd/shinkrodb/main.go +++ b/cmd/shinkrodb/main.go @@ -26,12 +26,13 @@ func main() { switch cmd := pflag.Arg(0); cmd { case "run": cfg := config.NewConfig() + domain.SetAnimePaths(rootPath) domain.CleanCache("./mal_cache") domain.GetMalIds(cfg) domain.ScrapeMal() domain.GetTvdbIDs() domain.GetTmdbIds(cfg, rootPath) - a := domain.GetAnime("./malid-anidbid-tvdbid-tmdbid.json") + a := domain.GetAnime(domain.TMDBIDPath) fmt.Println("Total number of dupes:", domain.CheckDupes(a)) unmapped := tvdbmap.CreateAnimeTVDBMap(rootPath) err := tvdbmap.UpdateMaster(unmapped, rootPath) diff --git a/internal/domain/dedupe.go b/internal/domain/dedupe.go index f069e36..fcc6805 100644 --- a/internal/domain/dedupe.go +++ b/internal/domain/dedupe.go @@ -61,7 +61,7 @@ func CheckDupes(a []Anime) int { deduped = checkTitle(a, indexes) } - StoreAnime(deduped, "./for-shinkro.json") + StoreAnime(deduped, shinkroPath) return len(dupeanidb) } diff --git a/internal/domain/domain.go b/internal/domain/domain.go index 56cbdfc..640c310 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -33,10 +33,10 @@ func NewAnimeService(c *colly.Collector) *AnimeService { } } -func GetAnime(path string) []Anime { +func GetAnime(path AnimePath) []Anime { a := []Anime{} - f, err := os.Open(path) + f, err := os.Open(string(path)) checkErr(err) defer f.Close() @@ -49,13 +49,13 @@ func GetAnime(path string) []Anime { return a } -func StoreAnime(a []Anime, path string) { +func StoreAnime(a []Anime, path AnimePath) { j, err := json.MarshalIndent(a, "", " ") if err != nil { log.Fatal(err) } - f, err := os.Create(path) + f, err := os.Create(string(path)) if err != nil { log.Fatal(err) } diff --git a/internal/domain/mal.go b/internal/domain/mal.go index 11f9c8b..3fc2466 100644 --- a/internal/domain/mal.go +++ b/internal/domain/mal.go @@ -55,6 +55,7 @@ func (c *clientIDTransport) RoundTrip(req *http.Request) (*http.Response, error) } func GetMalIds(cfg *Config) { + fmt.Println("Getting current ids from myanimelist..") c := &http.Client{ Transport: &clientIDTransport{ClientID: cfg.MalClientID}, } @@ -74,7 +75,9 @@ func GetMalIds(cfg *Config) { return a[i].MalID < a[j].MalID }) - StoreAnime(a, "./malid.json") + StoreAnime(a, MalIDPath) + fmt.Println("Stored malids to ", MalIDPath) + copyFileIfNotExist(MalIDPath, AniDBIDPath) } func storeAnimeID(c *http.Client, url string, a *[]Anime) string { @@ -124,7 +127,7 @@ func ScrapeMal() { //extensions.Referer(cc) as := NewAnimeService(cc) - a := GetAnime("./malid.json") + a := GetAnime(AniDBIDPath) as.AnimeSlice = a r := regexp.MustCompile(`aid=(\d+)`) as.c.OnHTML("a[href]", func(e *colly.HTMLElement) { @@ -156,8 +159,10 @@ func ScrapeMal() { for i, v := range as.AnimeSlice { gc = i - as.c.Visit(fmt.Sprintf("https://myanimelist.net/anime/%d", v.MalID)) + if as.AnimeSlice[gc].AnidbID <= 0 { + as.c.Visit(fmt.Sprintf("https://myanimelist.net/anime/%d", v.MalID)) + } } - StoreAnime(as.AnimeSlice, "./malid-anidbid.json") + StoreAnime(as.AnimeSlice, AniDBIDPath) } diff --git a/internal/domain/path.go b/internal/domain/path.go new file mode 100644 index 0000000..0c00062 --- /dev/null +++ b/internal/domain/path.go @@ -0,0 +1,36 @@ +package domain + +import "path/filepath" + +type AnimeFile string + +const malidFile AnimeFile = "malid.json" +const anidbFile AnimeFile = "malid-anidbid.json" +const tvdbFile AnimeFile = "malid-anidbid-tvdbid.json" +const tmdbFile AnimeFile = "malid-anidbid-tvdbid-tmdbid.json" +const shinkroFile AnimeFile = "for-shinkro.json" + +type AnimePath string + +var MalIDPath AnimePath +var AniDBIDPath AnimePath +var TVDBIDPath AnimePath +var TMDBIDPath AnimePath +var shinkroPath AnimePath + +func SetAnimePaths(rootDir string) { + rootDir = setshinkrodb(rootDir) + MalIDPath = makeAnimePath(rootDir, malidFile) + AniDBIDPath = makeAnimePath(rootDir, anidbFile) + TVDBIDPath = makeAnimePath(rootDir, tvdbFile) + TMDBIDPath = makeAnimePath(rootDir, tmdbFile) + shinkroPath = makeAnimePath(rootDir, shinkroFile) +} + +func makeAnimePath(rootDir string, af AnimeFile) AnimePath { + return AnimePath(filepath.Join(rootDir, string(af))) +} + +func setshinkrodb(rootDir string) string { + return filepath.Join(rootDir, "shinkrodb") +} diff --git a/internal/domain/tmdb.go b/internal/domain/tmdb.go index 5419822..e2b002c 100644 --- a/internal/domain/tmdb.go +++ b/internal/domain/tmdb.go @@ -62,7 +62,7 @@ type TMDBAPIResponse struct { } func GetTmdbIds(cfg *Config, rootPath string) { - a := GetAnime("./malid-anidbid-tvdbid.json") + a := GetAnime(TVDBIDPath) u := buildUrl(cfg.TmdbApiKey) am := &AnimeMovies{} noTmdbTotal := 0 @@ -122,7 +122,7 @@ func GetTmdbIds(cfg *Config, rootPath string) { } } - StoreAnime(a, "./malid-anidbid-tvdbid-tmdbid.json") + StoreAnime(a, TMDBIDPath) log.Println("Total number of movies", totalMovies) log.Println("Total number of movies with TMDBID", withTmdbTotal) log.Println("Total number of movies without TMDBID", noTmdbTotal) diff --git a/internal/domain/tvdb.go b/internal/domain/tvdb.go index 3b8a8bd..8c665b4 100644 --- a/internal/domain/tvdb.go +++ b/internal/domain/tvdb.go @@ -12,7 +12,7 @@ func GetTvdbIDs() { log.Fatal(err) } - a := GetAnime("./malid-anidbid.json") + a := GetAnime(AniDBIDPath) for i, anime := range a { if anime.Type == "tv" && anime.AnidbID > 0 { if tvdbid := al.GetTvdbID(anime.AnidbID); tvdbid > 0 { @@ -21,5 +21,5 @@ func GetTvdbIDs() { } } - StoreAnime(a, "./malid-anidbid-tvdbid.json") -} \ No newline at end of file + StoreAnime(a, TVDBIDPath) +} diff --git a/internal/domain/util.go b/internal/domain/util.go new file mode 100644 index 0000000..c722c93 --- /dev/null +++ b/internal/domain/util.go @@ -0,0 +1,40 @@ +package domain + +import ( + "fmt" + "io" + "os" +) + +func copyFileIfNotExist(srcPath, dstPath AnimePath) error { + // Check if the destination file exists + if _, err := os.Stat(string(dstPath)); err == nil { + fmt.Printf("File %s already exists, skipping copy.\n", dstPath) + return nil // File exists, no need to copy + } else if !os.IsNotExist(err) { + return fmt.Errorf("failed to check destination file: %v", err) + } + + // Open the source file + srcFile, err := os.Open(string(srcPath)) + if err != nil { + return fmt.Errorf("failed to open source file: %v", err) + } + defer srcFile.Close() + + // Create the destination file + dstFile, err := os.Create(string(dstPath)) + if err != nil { + return fmt.Errorf("failed to create destination file: %v", err) + } + defer dstFile.Close() + + // Copy the contents from src to dst + _, err = io.Copy(dstFile, srcFile) + if err != nil { + return fmt.Errorf("failed to copy content: %v", err) + } + + fmt.Printf("File copied from %s to %s successfully.\n", srcPath, dstPath) + return nil +} diff --git a/internal/tvdbmap/tvdbmap.go b/internal/tvdbmap/tvdbmap.go index 073d1b6..52c0c92 100644 --- a/internal/tvdbmap/tvdbmap.go +++ b/internal/tvdbmap/tvdbmap.go @@ -87,7 +87,7 @@ func GetAnimeTVDBMap(path string) (*AnimeTVDBMap, error) { func CreateAnimeTVDBMap(path string) *AnimeTVDBMap { am := &AnimeTVDBMap{} - a := domain.GetAnime("./malid.json") + a := domain.GetAnime(domain.MalIDPath) for _, anime := range a { am.Anime = append(am.Anime, Anime{ anime.MalID,