This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAniList.go
95 lines (76 loc) · 2.43 KB
/
AniList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package arn
import (
"fmt"
"github.com/animenotifier/anilist"
)
// AniListAnimeFinder holds an internal map of ID to anime mappings
// and is therefore very efficient to use when trying to find
// anime by a given service and ID.
type AniListAnimeFinder struct {
idToAnime map[string]*Anime
malIDToAnime map[string]*Anime
}
// NewAniListAnimeFinder creates a new finder for Anilist anime.
func NewAniListAnimeFinder() *AniListAnimeFinder {
finder := &AniListAnimeFinder{
idToAnime: map[string]*Anime{},
malIDToAnime: map[string]*Anime{},
}
for anime := range StreamAnime() {
id := anime.GetMapping("anilist/anime")
if id != "" {
finder.idToAnime[id] = anime
}
malID := anime.GetMapping("myanimelist/anime")
if malID != "" {
finder.malIDToAnime[malID] = anime
}
}
return finder
}
// GetAnime tries to find an AniList anime in our anime database.
func (finder *AniListAnimeFinder) GetAnime(id string, malID string) *Anime {
animeByID, existsByID := finder.idToAnime[id]
animeByMALID, existsByMALID := finder.malIDToAnime[malID]
// Add anilist mapping to the MAL mapped anime if it's missing
if existsByMALID && animeByMALID.GetMapping("anilist/anime") != id {
animeByMALID.SetMapping("anilist/anime", id)
animeByMALID.Save()
finder.idToAnime[id] = animeByMALID
}
// If both MAL ID and AniList ID are matched, but the matched anime are different,
// while the MAL IDs are different as well,
// then we're trusting the MAL ID matching more and deleting the incorrect mapping.
if existsByID && existsByMALID && animeByID.ID != animeByMALID.ID && animeByID.GetMapping("myanimelist/anime") != animeByMALID.GetMapping("myanimelist/anime") {
animeByID.RemoveMapping("anilist/anime")
animeByID.Save()
delete(finder.idToAnime, id)
fmt.Println("MAL / Anilist mismatch:")
fmt.Println(animeByID.ID, animeByID)
fmt.Println(animeByMALID.ID, animeByMALID)
}
if existsByID {
return animeByID
}
if existsByMALID {
return animeByMALID
}
return nil
}
// AniListAnimeListStatus returns the ARN version of the anime status.
func AniListAnimeListStatus(item *anilist.AnimeListItem) string {
switch item.Status {
case "CURRENT", "REPEATING":
return AnimeListStatusWatching
case "COMPLETED":
return AnimeListStatusCompleted
case "PLANNING":
return AnimeListStatusPlanned
case "PAUSED":
return AnimeListStatusHold
case "DROPPED":
return AnimeListStatusDropped
default:
return AnimeListStatusPlanned
}
}