forked from hr3lxphr6j/bililive-go
/
url.go
55 lines (44 loc) · 1.05 KB
/
url.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
package acfun
import (
"container/heap"
"encoding/json"
"net/url"
"github.com/luckycat0426/bililive-go/src/pkg/utils"
)
type representation struct {
Url string `json:"url"`
Level int `json:"level"`
}
type representations []representation
func (r representations) Len() int { return len(r) }
func (r representations) Less(i, j int) bool {
return r[i].Level > r[j].Level
}
func (r representations) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r *representations) Push(x interface{}) {
*r = append(*r, x.(representation))
}
func (r *representations) Pop() interface{} {
old := *r
n := len(old)
item := old[n-1]
*r = old[0 : n-1]
return item
}
func (r representations) GenUrls() ([]*url.URL, error) {
urls := make([]string, r.Len())
for idx, item := range r {
urls[idx] = item.Url
}
return utils.GenUrls(urls...)
}
func newRepresentationsFromJSON(s string) (representations, error) {
rs := make(representations, 0)
if err := json.Unmarshal([]byte(s), &rs); err != nil {
return nil, err
}
heap.Fix(&rs, rs.Len()-1)
return rs, nil
}