-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
56 lines (40 loc) · 1.33 KB
/
main.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
package youtubego
import (
"errors"
"io"
"log"
"github.com/yigit433/youtube-go/models"
"github.com/yigit433/youtube-go/internal/httpClient"
"github.com/yigit433/youtube-go/internal/parser"
"github.com/yigit433/youtube-go/internal/utils"
)
func Search(query string, config models.SearchConfig) ([]models.SearchResult, error) {
searchType := utils.GetSearchType(config.SearchType)
params := map[string]string{
"search_query": query,
"sp": searchType,
}
url, err := utils.BuildURLWithQueryParams("http://youtube.com/results", params)
if err != nil {
log.Fatalf("Cannot build the URL: %v", err)
return nil, errors.New("cannot build the URL")
}
client := httpclient.New(config.Timeout)
response, err := client.Get(url)
if err != nil {
log.Fatalf("Cannot send the request to the URL: %v", err)
return nil, errors.New("cannot send the request to the URL")
}
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatalf("status code error: %d %s", response.StatusCode, response.Status)
return nil, errors.New("status code error: " + response.Status)
}
body, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalf("Cannot read the body stream.")
return nil, errors.New("cannot read the body stream")
}
results := parser.ParseHTML(string(body), config.MaxResults)
return results, nil
}