Skip to content

Commit

Permalink
feat(domain): set rootpath for all files
Browse files Browse the repository at this point in the history
  • Loading branch information
varoOP committed Nov 17, 2024
1 parent ba5576c commit 8a7a479
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cmd/shinkrodb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/dedupe.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func CheckDupes(a []Anime) int {
deduped = checkTitle(a, indexes)
}

StoreAnime(deduped, "./for-shinkro.json")
StoreAnime(deduped, shinkroPath)
return len(dupeanidb)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/domain/mal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
36 changes: 36 additions & 0 deletions internal/domain/path.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 2 additions & 2 deletions internal/domain/tmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/domain/tvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,5 +21,5 @@ func GetTvdbIDs() {
}
}

StoreAnime(a, "./malid-anidbid-tvdbid.json")
}
StoreAnime(a, TVDBIDPath)
}
40 changes: 40 additions & 0 deletions internal/domain/util.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion internal/tvdbmap/tvdbmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8a7a479

Please sign in to comment.