-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
86 lines (68 loc) · 1.48 KB
/
search.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
package search
import (
"errors"
"net/http"
"net/url"
"reflect"
"time"
)
var resultTypes = make(map[string]reflect.Type)
func init() {
RegisterResultType(&YoutubeResult{})
}
func RegisterResultType(r Result) {
resultTypes[ResultTypeName(r)] = reflect.TypeOf(r).Elem()
}
func ResultType(name string) Result {
return reflect.New(resultTypes[name]).Interface().(Result)
}
func ResultTypeName(r Result) string {
t := reflect.TypeOf(r).Elem()
return t.PkgPath() + "/" + t.Name()
}
type Format struct {
Resolution string
VideoEncoding string
AudioEncoding string
AudioBitrate int
}
type Info interface {
ID() string
PageURL() *url.URL
Title() string
Created() time.Time
Formats() []*Format
Author() string
Duration() time.Duration
}
type Engine interface {
Search(q string, page int) ([]Result, error)
Page(url string) ([]Result, error)
}
type Result interface {
ID() string
IsPlayList() bool
PlaylistResults(timeout time.Duration) ([]Result, error)
DownloadURLs() (URLs, error)
PageURL() *url.URL
Title() string
Info() (Info, error)
Marshal() (string, error)
Unmarshal(b string) error
}
type URLs []*url.URL
func (urls URLs) Find(maxURLsToTry int) (*url.URL, error) {
for i, u := range urls {
if i == maxURLsToTry {
break
}
res, err := http.Head(u.String())
if res != nil && res.Body != nil {
res.Body.Close()
}
if err == nil && res.StatusCode >= 200 && res.StatusCode < 300 {
return u, nil
}
}
return nil, errors.New("No suitable url found")
}