forked from scakemyer/quasar
-
Notifications
You must be signed in to change notification settings - Fork 43
/
container.go
95 lines (82 loc) · 2.76 KB
/
container.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 uid
import (
"fmt"
"github.com/cespare/xxhash"
)
func NewLibraryContainer() *LibraryContainer {
return &LibraryContainer{
Items: map[uint64]struct{}{},
}
}
func (c *LibraryContainer) Add(media MediaItemType, uids *UniqueIDs, ids ...int) {
traktKey, tmdbKey, imdbKey := c.GetKeys(media, uids.Trakt, uids.TMDB, uids.IMDB, ids...)
if traktKey != 0 {
c.Items[traktKey] = struct{}{}
}
if tmdbKey != 0 {
c.Items[tmdbKey] = struct{}{}
}
if imdbKey != 0 {
c.Items[imdbKey] = struct{}{}
}
}
func (c *LibraryContainer) Has(media MediaItemType, uids *UniqueIDs, ids ...int) bool {
traktKey, tmdbKey, imdbKey := c.GetKeys(media, uids.Trakt, uids.TMDB, uids.IMDB, ids...)
for item := range c.Items {
if item == traktKey || item == tmdbKey || item == imdbKey {
return true
}
}
return false
}
func (c *LibraryContainer) Clear() {
c.Items = map[uint64]struct{}{}
}
func (c *LibraryContainer) HasWithType(media MediaItemType, scraperType ScraperType, id int) bool {
_, ok := c.Items[xxhash.Sum64String(fmt.Sprintf("%d_%d_%d", media, scraperType, id))]
return ok
}
func (c *LibraryContainer) GetKeys(media MediaItemType, traktID int, tmdbID int, imdbID string, ids ...int) (traktKey, tmdbKey, imdbKey uint64) {
if media == MovieType {
if traktID != 0 {
traktKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d", media, TraktScraper, traktID))
}
if tmdbID != 0 {
tmdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d", media, TMDBScraper, tmdbID))
}
if imdbID != "" {
imdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%s", media, IMDBScraper, imdbID))
}
} else if media == ShowType {
if traktID != 0 {
traktKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d", media, TraktScraper, traktID))
}
if tmdbID != 0 {
tmdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d", media, TMDBScraper, tmdbID))
}
if imdbID != "" {
imdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%s", media, IMDBScraper, imdbID))
}
} else if media == SeasonType && len(ids) > 0 {
if traktID != 0 {
tmdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d_%d", media, TraktScraper, traktID, ids[0]))
}
if tmdbID != 0 {
tmdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d_%d", media, TMDBScraper, tmdbID, ids[0]))
}
if imdbID != "" {
imdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%s_%d", media, IMDBScraper, imdbID, ids[0]))
}
} else if media == EpisodeType && len(ids) > 0 {
if traktID != 0 {
traktKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d_%d_%d", media, TraktScraper, traktID, ids[0], ids[1]))
}
if tmdbID != 0 {
tmdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%d_%d_%d", media, TMDBScraper, tmdbID, ids[0], ids[1]))
}
if imdbID != "" {
imdbKey = xxhash.Sum64String(fmt.Sprintf("%d_%d_%s_%d_%d", media, IMDBScraper, imdbID, ids[0], ids[1]))
}
}
return
}