forked from haofree/opendmm-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opendmm.go
78 lines (71 loc) · 1.69 KB
/
opendmm.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
package opendmm
import (
"sync"
mapset "github.com/deckarep/golang-set"
)
// MovieMeta contains meta data of movie
type MovieMeta struct {
Actresses []string
ActressTypes []string
Categories []string
Code string
CoverImage string
Description string
Directors []string
Genres []string
Label string
Maker string
MovieLength string
Page string
ReleaseDate string
SampleImages []string
Series string
Tags []string
ThumbnailImage string
Title string
}
type searchFunc func(string, *sync.WaitGroup, chan MovieMeta)
// Search for movies based on query and return a channel of MovieMeta
func Search(query string) chan MovieMeta {
out := make(chan MovieMeta)
go func(out chan MovieMeta) {
defer close(out)
batches := [][]searchFunc{[]searchFunc{
aveSearch,
dmmSearch,
niceageSearch,
}, []searchFunc{
javSearch,
mgsSearch,
}}
for _, engines := range batches {
batchOut := searchWithEngines(query, engines)
meta, ok := <-batchOut
if ok {
out <- meta
break
}
}
}(out)
return out
}
func searchWithEngines(query string, engines []searchFunc) chan MovieMeta {
wg := new(sync.WaitGroup)
out := make(chan MovieMeta, 100)
for _, engine := range engines {
engine(query, wg, out)
}
go func(wg *sync.WaitGroup, out chan MovieMeta) {
wg.Wait()
close(out)
}(wg, out)
return postprocess(out)
}
// Guess possible movie codes from query string
func Guess(query string) mapset.Set {
keywords := mapset.NewSet()
keywords = keywords.Union(aveGuess(query))
keywords = keywords.Union(dmmGuess(query))
keywords = keywords.Union(mgsGuess(query))
return keywords
}