-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitchvhr.go
61 lines (56 loc) · 1.42 KB
/
twitchvhr.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
package twitchvhr
import (
"net/http"
"strconv"
"sync"
"time"
)
type Options struct {
Timeout int
AllowSource bool
Player string
AllowSpectre bool
AllowAudioOnly bool
IncludeFramerate bool
}
func Retrieve(vodIDs []int, options Options) []string {
client := http.Client{Timeout: time.Duration(options.Timeout) * time.Second}
feedsOptions := FeedsOption{
AllowSource: options.AllowSource,
Player: options.Player,
AllowSpectre: options.AllowSpectre,
AllowAudioOnly: options.AllowAudioOnly,
IncludeFramerate: options.IncludeFramerate,
}
newHosts := make(chan []string)
hosts := make(chan []string)
var wg sync.WaitGroup
defer close(hosts)
go CombineResults(newHosts, hosts, &wg)
for _, vod := range vodIDs {
wg.Add(1)
go RetrieveHosts(strconv.Itoa(vod), client, feedsOptions, newHosts, &wg)
}
wg.Wait()
close(newHosts)
results := <-hosts
return UniqueResults(results)
}
func CombineResults(newHosts chan []string, hosts chan []string, wg *sync.WaitGroup) {
results := make([]string, 0)
for host := range newHosts {
results = append(results, host...)
}
hosts <- results
}
func UniqueResults(results []string) []string {
occured := map[string]bool{}
uniqueResults := make([]string, 0)
for r := range results {
if occured[results[r]] != true {
occured[results[r]] = true
uniqueResults = append(uniqueResults, results[r])
}
}
return uniqueResults
}